Code Monkey home page Code Monkey logo

delegates_attributes_to's Introduction

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.

delegates_attributes_to's People

Contributors

mguterl avatar pabloh avatar pahanix avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.