module ActiveFedora::NestedAttributes::ClassMethods

def accepts_nested_attributes_for(*attr_names)

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
      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

def generate_association_writer(association_name, type)

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

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

could generate the following:
accessing the objects in the association. For example, this method
Generates a writer method for this association. Serves as a point for
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

def generate_property_writer(association_name, _type)

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

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

could generate the following:
accessing the objects in the association. For example, this method
Generates a writer method for this association. Serves as a point for
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