Permalink
Browse files

Exclude _destroy parameter in :all_blank check (issue #2937)

  • Loading branch information...
Aaron Christy
Aaron Christy committed Oct 15, 2011
1 parent 098c95a commit 8510a0bb5a8a65e4bc067ee5a7d98aae965b47a5
View
@@ -29,6 +29,11 @@
[Andrés Mejía]
+* Fix nested attributes bug where _destroy parameter is taken into account
+ during :reject_if => :all_blank (fixes #2937)
+
+ [Aaron Christy]
+
*Rails 3.1.1 (October 7, 2011)*
* Add deprecation for the preload_associations method. Fixes #3022.
@@ -220,7 +220,7 @@ class TooManyRecords < ActiveRecordError
# validates_presence_of :member
# end
module ClassMethods
- REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |_, value| value.blank? } }
+ REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
# Defines an attributes writer for the specified association(s). If you
# are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>, then you
@@ -239,7 +239,8 @@ module ClassMethods
# is specified, a record will be built for all attribute hashes that
# do not have a <tt>_destroy</tt> value 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.
+ # that will reject a record where all the attributes are blank excluding
+ # any value for _destroy.
# [: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
@@ -45,6 +45,14 @@ def test_should_add_a_proc_to_nested_attributes_options
end
end
+ def test_should_not_build_a_new_record_using_reject_all_even_if_destroy_is_given
+ pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ pirate.birds_with_reject_all_blank_attributes = [{:name => '', :color => '', :_destroy => '0'}]
+ pirate.save!
+
+ assert pirate.birds_with_reject_all_blank.empty?
+ end
+
def test_should_not_build_a_new_record_if_reject_all_blank_returns_false
pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
pirate.birds_with_reject_all_blank_attributes = [{:name => '', :color => ''}]

0 comments on commit 8510a0b

Please sign in to comment.