Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument to delegate method in Rails

I would like to have a Dashboard to display summary of multiple models, and I implemented it using Presenter without its own data. I use an ActiveModel class (without data table):

class Dashboard
  attr_accessor :user_id
  def initialize(id)
    self.user_id = id
  end

  delegate :username, :password, :to => :user 
  delegate :address,  :to => :account
  delegate :friends,   :to => :friendship

end 

By delegate, I want to be able to call Dashboard.address and get back Account.find_by_user_id(Dashboard.user_id).address.

If Dashboard was an ActiveRecord class, then I could have declared Dashboard#belongs_to :account and delegate would work automatically (i.e., Account would know it should return address attribute from account with user_id equals to user_id in Dashboard instance).

But Dashboard is not an ActiveRecord class, so I can't declare belongs_to. I need another way to tell Account to lookup the right record.

Is there a way to overcome this problem? (I know I can fake Dashboard to have an empty table, or I can rewrite User's instance methods to class methods that take argument. But these solutions are all hacks).

Thank you.

like image 509
AdamNYC Avatar asked Sep 24 '11 05:09

AdamNYC


People also ask

How does delegate work in Rails?

On the Profile side we use the delegate method to pass any class methods to the User model that we want access from our User model inside our Profile model. The delegate method allows you to optionally pass allow_nil and a prefix as well. Ultimately this allows us to query for data in custom ways.

How does delegate work in Ruby?

Delegation can be done explicitly, by passing the sending object to the receiving object, which can be done in any object-oriented language; or implicitly, by the member lookup rules of the language, which requires language support for the feature.

What is Simpledelegator?

A concrete implementation of Delegator , this class provides the means to delegate all supported method calls to the object passed into the constructor and even to change the object being delegated to at a later time with #__setobj__. class User def born_on Date.


1 Answers

When you write delegate :address, :to => :account, this creates a new address method on Dashboard which basically calls the account method on the same object and then calls address on the result of this account method. This is (very roughly) akin to writing:

class Dashboard
 ...
  def address
    self.account.address
  end
 ...
end

With your current class, all you have to do is to create an account method which returns the account with the correct user_id:

class Dashboard
  attr_accessor :user_id
  def initialize(id)
    self.user_id = id
  end

  delegate :username, :password, :to => :user 
  delegate :address,  :to => :account
  delegate :friends,   :to => :friendship

  def account
    @account ||= Account.find_by_user_id(self.user_id)
  end
end

This would allow you to access the address like this:

dashboard = Dashboard.new(1)
# the following returns Account.find_by_user_id(1).address
address = dashboard.address
like image 199
Benoit Garret Avatar answered Sep 22 '22 11:09

Benoit Garret