diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-05-15 16:47:15 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-05-15 16:47:15 +0200 |
commit | 59653101b8cef7915cb1fb4ad4b84f49ae0881e5 (patch) | |
tree | 9dacd7eaa05c7957b14389538a30d7ebf7f7e349 /activerecord/lib/active_record/nested_attributes.rb | |
parent | 26ad104e72e2b758815a043341dd83a1b02e8c7f (diff) | |
parent | eb021707f53be46140b55a48e5ef03ed0577a45c (diff) | |
download | rails-59653101b8cef7915cb1fb4ad4b84f49ae0881e5.tar.gz rails-59653101b8cef7915cb1fb4ad4b84f49ae0881e5.tar.bz2 rails-59653101b8cef7915cb1fb4ad4b84f49ae0881e5.zip |
Merge commit 'mainstream/master'
Conflicts:
actionpack/lib/action_view/helpers/form_helper.rb
Diffstat (limited to 'activerecord/lib/active_record/nested_attributes.rb')
-rw-r--r-- | activerecord/lib/active_record/nested_attributes.rb | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index f48ad8818b..500b9b8ae0 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -1,9 +1,13 @@ +require 'active_support/core_ext/hash/except' +require 'active_support/core_ext/object/try' + module ActiveRecord module NestedAttributes #:nodoc: - def self.included(base) - base.extend(ClassMethods) - base.class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false - base.reject_new_nested_attributes_procs = {} + extend ActiveSupport::DependencyModule + + included do + class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false + self.reject_new_nested_attributes_procs = {} end # == Nested Attributes @@ -180,10 +184,14 @@ module ActiveRecord # and the Proc should return either +true+ or +false+. When no Proc # is specified a record will be built for all attribute hashes that # do not have a <tt>_delete</tt> that evaluates to true. + # Passing <tt>:all_blank</tt> instead of a Proc will create a proc + # that will reject a record where all the attributes are blank. # # 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 def accepts_nested_attributes_for(*attr_names) @@ -201,7 +209,12 @@ module ActiveRecord end reflection.options[:autosave] = true - self.reject_new_nested_attributes_procs[association_name.to_sym] = options[:reject_if] + + self.reject_new_nested_attributes_procs[association_name.to_sym] = if options[:reject_if] == :all_blank + proc { |attributes| attributes.all? {|k,v| v.blank?} } + else + options[:reject_if] + end # def pirate_attributes=(attributes) # assign_nested_attributes_for_one_to_one_association(:pirate, attributes, false) |