module ActiveFedora::NestedAttributes::ClassMethods

Defines an attributes writer for the specified association(s). If you are using attr_protected or attr_accessible, then you will need to add the attribute writer to the allowed list.

Supported options:

:allow_destroy

If true, destroys any members from the attributes hash with a _destroy key and a value that evaluates to true (eg. 1, ‘1’, true, or ‘true’). This option is off by default.

:reject_if

Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash. The hash is passed to the supplied Proc or the method and it should return either true or false. When no :reject_if is specified, a record will be built for all attribute hashes that do not have a _destroy value that evaluates to true. Passing :all_blank instead of a Proc will create a proc that will reject a record where all the attributes are blank.

:limit

Allows you to specify the maximum number of the associated records that can be processed with the nested attributes. If the size of the nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords exception is raised. If omitted, any number associations can be processed. Note that the :limit option is only applicable to one-to-many associations.

:update_only

Allows you to specify that an existing record may only be updated. A new record may only be created when there is no existing record. This option only works for one-to-one associations and is ignored for collection associations. This option is off by default.

Examples:

# creates avatar_attributes=
accepts_nested_attributes_for :avatar, :reject_if => proc { |attributes| attributes['name'].blank? }
# creates avatar_attributes=
accepts_nested_attributes_for :avatar, :reject_if => :all_blank
# creates avatar_attributes= and posts_attributes=
accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true

Constants

REJECT_ALL_BLANK_PROC

Public Instance Methods

accepts_nested_attributes_for(*attr_names) click to toggle source
# File lib/active_fedora/nested_attributes.rb, line 56
      def accepts_nested_attributes_for(*attr_names)
        options = { allow_destroy: false, update_only: false }
        options.update(attr_names.extract_options!)
        options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only)
        options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank

        attr_names.each do |association_name|
          if reflection = _reflect_on_association(association_name)
            reflection.autosave = true
            define_autosave_association_callbacks(reflection)
            ## TODO this ought to work, but doesn't seem to do the class inheritance right
            # See https://github.com/samvera/active_fedora/issues/1343

            nested_attributes_options = self.nested_attributes_options.dup
            nested_attributes_options[association_name.to_sym] = options
            self.nested_attributes_options = nested_attributes_options

            type = (reflection.collection? ? :collection : :one_to_one)
            generate_association_writer(association_name, type)

            class_eval <<-eoruby, __FILE__, __LINE__ + 1
              remove_possible_method(:#{association_name}_attributes=)

              def #{association_name}_attributes=(attributes)
                assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
              end
            eoruby
          elsif reflect_on_property(association_name)
            resource_class.accepts_nested_attributes_for(association_name, options)
            generate_property_writer(association_name, type)

            # Delegate the setter to the resource.
            class_eval <<-eoruby, __FILE__, __LINE__ + 1
              remove_possible_method(:#{association_name}_attributes=)

              def #{association_name}_attributes=(attributes)
                attribute_will_change!(:#{association_name})
                resource.#{association_name}_attributes=(attributes)
              end
            eoruby
          else
            raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
          end
        end
      end

Private Instance Methods

generate_association_writer(association_name, type) click to toggle source

Generates a writer method for this association. Serves as a point for accessing the objects in the association. For example, this method could generate the following:

def pirate_attributes=(attributes)
  assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
end

This redirects the attempts to write objects in an association through the helper methods defined below. Makes it seem like the nested associations are just regular associations.

# File lib/active_fedora/nested_attributes.rb, line 115
        def generate_association_writer(association_name, type)
          generated_association_methods.module_eval <<-eoruby, __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
          eoruby
        end
generate_property_writer(association_name, _type) click to toggle source

Generates a writer method for this association. Serves as a point for accessing the objects in the association. For example, this method could generate the following:

def pirate_attributes=(attributes)
  assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
end

This redirects the attempts to write objects in an association through the helper methods defined below. Makes it seem like the nested associations are just regular associations.

# File lib/active_fedora/nested_attributes.rb, line 137
        def generate_property_writer(association_name, _type)
          generated_association_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1
            if method_defined?(:#{association_name}_attributes=)
              remove_method(:#{association_name}_attributes=)
            end
            def #{association_name}_attributes=(attributes)
              attribute_will_change!(:#{association_name})
              resource.#{association_name}_attributes=(attributes)
            end
          eoruby
        end