/delegates_attributes_to

A rails plugin to allow delegation to ActiveRecord associations with dirty check and auto saving.

Primary LanguageRuby

Delegate Attributes

Association attributes delegator plugin. It delegates association attributes accessors to a model. It also supports ActiveRecord::Dirty check, multiparamenter attributes (1i, 2i, 3i) assigning, auto saving for delegated attributes (see below).

Usage

class User
  belongs_to :contact
  # it delegates all association attribute accessors to :contact 
  # except service attributes like created_at, updated_at, etc.
  delegate_attributes :to => :contact
  
  has_one :profile
  # it delegates only :about, :hobby attribute accessors to :profile
  delegate_attributes :about, :hobby, :to => :profile
end

Special service attributed are not delegated

Attributes like id, created_at, updated_at, type, etc are ignored by default. You can specify which attributes are ignored by changing class inheritable accessor default_rejected_delegate_columns for each class

class Person
  default_rejected_delegate_columns << [:commentable_id, :commentable_type]
  ...
end

Autosave for delegated attributes

When ActiveRecord saves a main model it doesn't save associated models by default (unless both are new records). This is not desired behavior for delegated attributes. They should behave like other native attributes in the model. For this reason ::delegates_attributes_to adds to association reflection :autosave option and sets it to true unless the reflection has :autosave option already. So if you do not want to autosave delegated attributes for some reason, just add :autosave => false to your reflection explicitly and ::delegates_attributes_to won't change it.

Important. Side effect for that behavior is that it also saves not delegated attributes of the associated model.

Dirty check changes are tracked for delegated attributes

When association attribute is modified an association model becomes dirty but it takes no effect on a main model. It doesn't become dirty. To be more like native attributes this behavior was changed for delegated attributes. All changes are tracked even if association attributes were changed directly in association.

Important. It tracks for changes only chosen delegated attributes and do not track others.

class User

  belongs_to :contact
  delegate_attributes :to => :contact

  has_one :profile
  delegate_attribute :about, :to => :profile
  ...
end

u = User.first

u.firstname               # => "Jonh"
u.firstname_changed?      # => false
u.changed?                # => false
u.firstname = 'Bob'       
u.firstname               # => "Bob"
u.firstname_was           # => "Jonh"
u.firstname_changed?      # => true
u.changed?                # => true
u.contact.changed?        # => true

# it also tracks changes even if association attribute was changed directly
u = User.last

u.changed?                # => false
u.profile.about = "Bob"
u.changed?                # => true

u = User.last

u.changed?                # => false
u.about = "Bob"
u.changed?                # => true
u.profile.changed?        # => true

# it doesn't track chages to non delegated attribute
u = User.last

u.changed?                # => false
u.profile.hobby = "Basketball"
u.changed?                # => false
u.profile.changed?        # => true

Multiparameter attributes assigning

Multiparameter attributes that need more than one constructor parameter also can be assigned. More frequently example is saving a form with multi select date/time attribute through #date_select helper. (It produces params like 'written_on(1i)'='2010', 'written_on(2i)'='3', 'written_on(3i)'='7') Any attribute which is built through composed_of also can be delegated.

It is backward compatible with David Faber's delegate_belong_to

Original idea belongs to David Faber. Project was forked from Faber's delegate_belongs_to repo, refactored and significally improved. Also ::delegate_has_one method was implemented.

Copyright

Copiright © 2010 Pavel Gorbokon, released under the MIT license.