Module: ActiveData::Model::NestedAttributes::ClassMethods

Defined in:
lib/active_data/model/nested_attributes.rb

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributes_for(*attr_names) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_data/model/nested_attributes.rb', line 12

def accepts_nested_attributes_for *attr_names
  attr_names.each do |association_name|
    reflection = reflect_on_association association_name
    type = (reflection.collection? ? :collection : :one_to_one)

    class_eval <<-EOS, __FILE__, __LINE__ + 1
      if method_defined?(:#{association_name}_attributes=)
        remove_method(:#{association_name}_attributes=)
      end
      def #{association_name}_attributes=(attributes)
        assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
      end
    EOS
  end
end

#nested_attributes?(association_name) ⇒ Boolean

Returns:



8
9
10
# File 'lib/active_data/model/nested_attributes.rb', line 8

def nested_attributes? association_name
  method_defined?(:"#{association_name}_attributes=")
end