aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/nested_attributes.rb
diff options
context:
space:
mode:
authorMike Breen <hardbap@gmail.com>2009-05-10 15:02:00 +1200
committerMichael Koziarski <michael@koziarski.com>2009-05-10 15:02:00 +1200
commit9010ed27559ed5ab89ea71b4b16f4c8e56d03dbb (patch)
treecc6198a10a89e8a7017f2f38368e94bdd8348ad6 /activerecord/lib/active_record/nested_attributes.rb
parent026b78f9076216990bddb1aa5d83d23a647c02a5 (diff)
downloadrails-9010ed27559ed5ab89ea71b4b16f4c8e56d03dbb.tar.gz
rails-9010ed27559ed5ab89ea71b4b16f4c8e56d03dbb.tar.bz2
rails-9010ed27559ed5ab89ea71b4b16f4c8e56d03dbb.zip
Allow you to pass :all_blank to :reject_if option to automatically create a Proc that will reject any record with blank attributes.
Diffstat (limited to 'activerecord/lib/active_record/nested_attributes.rb')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb11
1 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index e3122d195a..dfad2901c5 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -180,10 +180,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 +205,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)