ruby on rails - How to decorate an object with another name in draper? -


this question

i have event model , user model.

an event has 1 creator of class user. used line in event model associate it:

belongs_to :creator, :class_name => "user" 

so can access creator through line:

event.creator 

my user decorator has line:

def full_name     "#{first_name} #{last_name}" end 

so can decorate user object , access user.full_name

but need decorate event, , use "decorates_association" decorate associated user. need call line:

event.creator.full_name 

i have tried this:

decorates_association :creator, {with: "userdecorator"} decorates_association :creator, {with: "user"} 

but throws , "undefined method `full_name'" error.

how can prevent error?

thank you!

in eventdecorator class can like:

class eventdecorator < draper::decorator   decorates_association :creator, with: userdecorator # no quotes   delegate :full_name, to: :creator, allow_nil: true, prefix: true end 

and user:

class userdecorator < draper::decorator   def full_name     "#{first_name} #{last_name}"   end end 

then in rails console do:

ed = event.last.decorate ed.creator_full_name # note underscores delegate  # can ed.creator.full_name 

in second example, if creator nil method not found error. in first, because of allow_nil option delegate method in eventdecorator won't error return nil.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -