Module: DelegateBelongsTo::ClassMethods

Defined in:
lib/spree/core/delegate_belongs_to.rb

Constant Summary collapse

@@default_rejected_delegate_columns =
['created_at','created_on','updated_at','updated_on','lock_version','type','id','position','parent_id','lft','rgt']

Instance Method Summary collapse

Instance Method Details

#delegate_belongs_to(association, *attrs) ⇒ Object

TODO:

Integrate this with ActiveRecord::Dirty, so if you set a property through one of these setters and then call save on this object, it will save the associated object automatically.

Creates methods for accessing and setting attributes on an association. Uses same default list of attributes as delegates_to_association. delegate_belongs_to :contact delegate_belongs_to :contact, [:defaults] ## same as above, and useless delegate_belongs_to :contact, [:defaults, :address, :fullname], :class_name => 'VCard'


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spree/core/delegate_belongs_to.rb', line 34

def delegate_belongs_to(association, *attrs)
  opts = attrs.extract_options!
  initialize_association :belongs_to, association, opts
  attrs = get_association_column_names(association) if attrs.empty?
  attrs.concat get_association_column_names(association) if attrs.delete :defaults
  attrs.each do |attr|
    class_def attr do |*args|
      if args.empty?
        send(:delegator_for, association).send(attr)
      else
        send(:delegator_for, association).send(attr, *args)
      end
    end
    class_def "#{attr}=" do |val|
      send(:delegator_for, association).send("#{attr}=", val)
    end
  end
end