Skip to content

pahanix/delegate_belongs_to

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Delegates Attributes To

Simple association attributes delegator plugin. It delegates association attributes accessors to a model. It also supports ActiveRecord::Dirty check and auto saving for delegated attributes (see below).

Usage

class User
  belongs_to :contact
  # it delegates all association attribute accessors to :contact except created_at, updated_at, etc.
  delegates_attributes_to :contact
  
  # or shortly
  # delegate_belong_to :contact
  
  has_one :profile
  # it delegates only :about, :hobby attribute accessors to :profile
  delegates_attributes_to :profile, :about, :hobby
  
  # or shortly
  # delegate_has_one :profile, :about, :hobby
  
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
  self.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
  delegates_attributes_to :contact

  has_one :profile
  delegates_attributes_to :profile, :about
  ...
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.chaged?                 # => true

u = User.last

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

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

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

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.

Releases

No releases published

Packages

No packages published

Languages