Sha256: 84ee0171f542f16422125c224224b6ef0dfb75896c09ccf0029fd6dc2968dcf7

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

class ActiveRecord::Base 
  module ClassMethods
    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)
          #Removed autosave association
          #reflection.options[:autosave] = true
          add_autosave_association_callbacks(reflection)
          nested_attributes_options[association_name.to_sym] = options
          type = (reflection.collection? ? :collection : :one_to_one)

          # def pirate_attributes=(attributes)
          #   assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
          # end
          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_#{type}_association(:#{association_name}, attributes)
            end
          eoruby
        else
          raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
        end
      end
    end

  end
end
ActiveRecord::Base.extend ActiveRecord::Base::ClassMethods

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
saasaparilla-0.2.2 lib/extensions/active_record/nested_attributes.rb
saasaparilla-0.2.1 lib/extensions/active_record/nested_attributes.rb
saasaparilla-0.1.8 lib/extensions/active_record/nested_attributes.rb
saasaparilla-0.1.7 lib/extensions/active_record/nested_attributes.rb
saasaparilla-0.1.6 lib/extensions/active_record/nested_attributes.rb