Module: DataMapper::NestedAttributes::Model

Defined in:
lib/dm-accepts_nested_attributes/model.rb

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributes_for(association_name, options = {}) ⇒ void

This method returns an undefined value.

Allows an association to accept nested attributes.

Parameters:

  • association_name (Symbol, String)

    The name of the association that should accept nested attributes.

  • options (Hash?) (defaults to: {})

    List of resources to initialize the collection with.

Options Hash (options):

  • :reject_if (Symbol, String, #call)

    An instance method name or an object that respond_to?(:call), which stops a new record from being created, if it evaluates to true.

  • :allow_destroy (Boolean) — default: false

    If true, allows destroying the association via the generated writer. If false, prevents destroying the association via the generated writer.

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dm-accepts_nested_attributes/model.rb', line 58

def accepts_nested_attributes_for(association_name, options = {})

  # ----------------------------------------------------------------------------------
  #                      try to fail as early as possible
  # ----------------------------------------------------------------------------------

  unless relationship = relationships(repository_name)[association_name]
    raise(ArgumentError, "No relationship #{association_name.inspect} for '#{name}' in :#{repository_name} repository")
  end

  # raise InvalidOptions if the given options don't make sense
  assert_valid_options_for_nested_attributes(options)

  # by default, nested attributes can't be destroyed
  options = { :allow_destroy => false }.update(options)

  # ----------------------------------------------------------------------------------
  #                       should be safe to go from here
  # ----------------------------------------------------------------------------------

  options_for_nested_attributes[relationship.name] = options

  include ::DataMapper::NestedAttributes::Resource

  type = relationship.max > 1 ? :collection : :resource

  define_method "#{association_name}_attributes" do
    instance_variable_get("@#{association_name}_attributes")
  end

  define_method "#{association_name}_attributes=" do |attributes|
    attributes = sanitize_nested_attributes(attributes)
    instance_variable_set("@#{association_name}_attributes", attributes)
    send("assign_nested_attributes_for_related_#{type}", relationship, attributes)
  end

end

#options_for_nested_attributesHash{DataMapper::Associations::Relationship => Hash}

Returns a hash with the options for all associations (using the corresponding relationship as key) that accept nested attributes.

Returns:

  • (Hash{DataMapper::Associations::Relationship => Hash})


100
101
102
# File 'lib/dm-accepts_nested_attributes/model.rb', line 100

def options_for_nested_attributes
  @options_for_nested_attributes ||= DataMapper::NestedAttributes::BackwardsCompatibilityHash.new(self)
end