Module: Humanoid::Attributes::ClassMethods

Defined in:
lib/humanoid/attributes.rb

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributes_for(*args) ⇒ Object

Defines attribute setters for the associations specified by the names. This will work for a has one or has many association.

Example:

class Person
  include Humanoid::Document
  has_one :name
  has_many :addresses

  accepts_nested_attributes_for :name, :addresses
end


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/humanoid/attributes.rb', line 166

def accepts_nested_attributes_for(*args)
  associations = args.flatten
  options = associations.last.is_a?(Hash) ? associations.pop : {}
  associations.each do |name|
    define_method("#{name}_attributes=") do |attrs|
      reject(attrs, options)
      association = send(name)
      if association
        update(association, true)
        association.nested_build(attrs)
      else
        send("build_#{name}", attrs)
      end
    end
  end
end