aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations/validates.rb
diff options
context:
space:
mode:
authorSteve Purcell <steve@sanityinc.com>2012-05-28 14:39:09 +0100
committerSteve Purcell <steve@sanityinc.com>2012-05-28 15:02:02 +0100
commitb3ccd7b27afabacba5f537ef5c428314df25eae3 (patch)
tree2e1de9537b20a29e24d0447f7ce5d2175f8cbc9d /activemodel/lib/active_model/validations/validates.rb
parent5acb10d6c359f9b715e5b5a5d26a524581a9ac41 (diff)
downloadrails-b3ccd7b27afabacba5f537ef5c428314df25eae3.tar.gz
rails-b3ccd7b27afabacba5f537ef5c428314df25eae3.tar.bz2
rails-b3ccd7b27afabacba5f537ef5c428314df25eae3.zip
Don't enable validations when passing false hash values to ActiveModel.validates
Passing a falsey option value for a validator currently causes that validator to be enabled, just like "true": ActiveModel.validates :foo, :presence => false This is rather counterintuitive, and makes it inconvenient to wrap `validates` in methods which may conditionally enable different validators. As an example, one is currently forced to write: def has_slug(source_field, options={:unique => true}) slugger = Proc.new { |r| r[:slug] = self.class.sluggify(r[source_field]) if r[:slug].blank? } before_validation slugger validations = { :presence => true, :slug => true } if options[:unique] validations[:uniqueness] = true end validates :slug, validations end because the following reasonable-looking alternative fails to work as expected: def has_slug(source_field, options={:unique => true}) slugger = Proc.new { |r| r[:slug] = self.class.sluggify(r[source_field]) if r[:slug].blank? } before_validation slugger validates :slug, :presence => true, :slug => true, :uniqueness => options[:unique] end (This commit includes a test, and all activemodel and activerecord tests pass as before.)
Diffstat (limited to 'activemodel/lib/active_model/validations/validates.rb')
-rw-r--r--activemodel/lib/active_model/validations/validates.rb1
1 files changed, 1 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb
index d94c4e3f4f..6c13d2b4a2 100644
--- a/activemodel/lib/active_model/validations/validates.rb
+++ b/activemodel/lib/active_model/validations/validates.rb
@@ -88,6 +88,7 @@ module ActiveModel
defaults.merge!(:attributes => attributes)
validations.each do |key, options|
+ next unless options
key = "#{key.to_s.camelize}Validator"
begin