Module: Sequel::Plugins::NestedAttributes::ClassMethods

Defined in:
lib/sequel/plugins/nested_attributes.rb

Instance Method Summary collapse

Instance Method Details

#freezeObject

Freeze nested_attributes_module when freezing model class.



94
95
96
97
98
# File 'lib/sequel/plugins/nested_attributes.rb', line 94

def freeze
  @nested_attributes_module.freeze if @nested_attributes_module

  super
end

#nested_attributes(*associations, &block) ⇒ Object

Allow nested attributes to be set for the given associations. Options:

:destroy

Allow destruction of nested records.

:fields

If provided, should be an Array or proc. If it is an array, restricts the fields allowed to be modified through the association_attributes= method to the specific fields given. If it is a proc, it will be called with the associated object and should return an array of the allowable fields.

:limit

For *_to_many associations, a limit on the number of records that will be processed, to prevent denial of service attacks.

:reject_if

A proc that is given each attribute hash before it is passed to its associated object. If the proc returns a truthy value, the attribute hash is ignored.

:remove

Allow disassociation of nested records (can remove the associated object from the parent object, but not destroy the associated object).

:transform

A proc to transform attribute hashes before they are passed to associated object. Takes two arguments, the parent object and the attribute hash. Uses the return value as the new attribute hash.

:unmatched_pk

Specify the action to be taken if a primary key is provided in a record, but it doesn’t match an existing associated object. Set to :create to create a new object with that primary key, :ignore to ignore the record, or :raise to raise an error. The default is :raise.

If a block is provided, it is used to set the :reject_if option.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sequel/plugins/nested_attributes.rb', line 124

def nested_attributes(*associations, &block)
  include(@nested_attributes_module ||= Module.new) unless @nested_attributes_module
  opts = associations.last.is_a?(Hash) ? associations.pop : {}
  reflections = associations.map{|a| association_reflection(a) || raise(Error, "no association named #{a} for #{self}")}
  reflections.each do |r|
    r[:nested_attributes] = opts
    r[:nested_attributes][:unmatched_pk] ||= (
      if opts.has_key?(:strict)
        Sequel::Deprecation.deprecate("The nested_attributes :strict option", "Use the :unmatched_pk option instead")
      end
      opts.delete(:strict) == false ? :ignore : :raise)
    r[:nested_attributes][:reject_if] ||= block
    def_nested_attribute_method(r)
  end
end

#nested_attributes_moduleObject



84
85
86
87
# File 'lib/sequel/plugins/nested_attributes.rb', line 84

def nested_attributes_module
  Sequel::Deprecation.deprecate('Sequel::Model.nested_attributes_module', 'There is no replacement')
  @nested_attributes_module
end

#nested_attributes_module=(v) ⇒ Object



88
89
90
91
# File 'lib/sequel/plugins/nested_attributes.rb', line 88

def nested_attributes_module=(v)
  Sequel::Deprecation.deprecate('Sequel::Model.nested_attributes_module=', 'There is no replacement')
  @nested_attributes_module = v
end