aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorMikhail Dieterle <MikDiet@gmail.com>2012-08-26 03:02:05 +0300
committerMikhail Dieterle <MikDiet@gmail.com>2012-08-26 03:02:05 +0300
commit8159bc9cf5f68c7adedb22f86444ec2ae428434e (patch)
tree15c4bb83d1ffd234d2f90a389e599f3e049e1936 /activerecord/lib
parent67153430861ca1305d77a73e631a9038049fbfa9 (diff)
downloadrails-8159bc9cf5f68c7adedb22f86444ec2ae428434e.tar.gz
rails-8159bc9cf5f68c7adedb22f86444ec2ae428434e.tar.bz2
rails-8159bc9cf5f68c7adedb22f86444ec2ae428434e.zip
allow to pass Symbol or Proc into :limit option of #accepts_nested_attributes_for
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb16
1 files changed, 13 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index be013a068c..f91e41b535 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -245,7 +245,8 @@ module ActiveRecord
# 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
+ # can be processed with the nested attributes. Limit also can be specified as a
+ # Proc or a Symbol pointing to a method that should return number. If the size of the
# nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
# exception is raised. If omitted, any number associations can be processed.
# Note that the :limit option is only applicable to one-to-many associations.
@@ -388,8 +389,17 @@ module ActiveRecord
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
end
- if options[:limit] && attributes_collection.size > options[:limit]
- raise TooManyRecords, "Maximum #{options[:limit]} records are allowed. Got #{attributes_collection.size} records instead."
+ limit = case options[:limit]
+ when Symbol
+ send(options[:limit])
+ when Proc
+ options[:limit].call
+ else
+ options[:limit]
+ end
+
+ if limit && attributes_collection.size > limit
+ raise TooManyRecords, "Maximum #{limit} records are allowed. Got #{attributes_collection.size} records instead."
end
if attributes_collection.is_a? Hash