module ActiveTriples::NestedAttributes::ClassMethods

Public Instance Methods

accepts_nested_attributes_for(*attr_names) click to toggle source
# File lib/active_triples/nested_attributes.rb, line 91
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|
    nested_attributes_options = self.nested_attributes_options.dup
    nested_attributes_options[association_name] = options
    self.nested_attributes_options = nested_attributes_options

    generate_association_writer(association_name)
  end
end

Private Instance Methods

generate_association_writer(association_name) 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_collection_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_triples/nested_attributes.rb, line 119
      def generate_association_writer(association_name)
        class_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_collection_association(:#{association_name}, attributes)
              ## in lieu of autosave_association_callbacks just save all of em.
              send(:#{association_name}).each {|obj| obj.marked_for_destruction? ? obj.destroy : nil}
            end
          eoruby
      end