From 36ee0fff7a8333c25f62b24a1f7889db41519c2b Mon Sep 17 00:00:00 2001 From: Obie Fernandez Date: Mon, 20 Sep 2010 21:35:41 -0400 Subject: [PATCH] Better shortcut options for custom validators --- .../lib/active_model/validations/validates.rb | 16 +++++++++++----- .../test/cases/validations/validates_test.rb | 9 +++++++++ activemodel/test/models/person_with_validator.rb | 13 +++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index f6349d5..e4db8e8 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -55,14 +55,20 @@ module ActiveModel # validates :name, :title => true # end # - # The validators hash can also handle regular expressions, ranges and arrays: + # The validators hash can also handle regular expressions, ranges, + # arrays and strings in shortcut form, e.g. # # validates :email, :format => /@/ # validates :gender, :inclusion => %w(male female) # validates :password, :length => 6..20 + # validates :name, :like => "Report" # - # Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can be given - # to one specific validator: + # When using shortcut form, ranges and arrays are passed to your + # validator's initializer as +options[:in]+ while other types including + # regular expressions and strings are passed as +options[:with]+ + # + # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+ and +:allow_nil+ can be given + # to one specific validator, as a hash: # # validates :password, :presence => { :if => :password_required? }, :confirmation => true # @@ -99,10 +105,10 @@ module ActiveModel {} when Hash options - when Regexp - { :with => options } when Range, Array { :in => options } + else + { :with => options } end end end diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index db023f6..745018c 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -111,4 +111,13 @@ class ValidatesTest < ActiveModel::TestCase person.valid? assert_equal ['Local validator please'], person.errors[:title] end + + def test_validates_with_included_validator_and_wildcard_shortcut + # Shortcut for PersonWithValidator.validates :title, :like => { :with => "Mr." } + PersonWithValidator.validates :title, :like => "Mr." + person = PersonWithValidator.new + person.title = "Ms. Pacman" + person.valid? + assert_equal ['does not appear to be like Mr.'], person.errors[:title] + end end diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb index f6f665c..f487f77 100644 --- a/activemodel/test/models/person_with_validator.rb +++ b/activemodel/test/models/person_with_validator.rb @@ -6,6 +6,19 @@ class PersonWithValidator record.errors[attribute] << "Local validator#{options[:custom]}" if value.blank? end end + + class LikeValidator < ActiveModel::EachValidator + def initialize(options) + @with = options[:with] + super + end + + def validate_each(record, attribute, value) + unless value[@with] + record.errors.add attribute, "does not appear to be like #{@with}" + end + end + end attr_accessor :title, :karma end -- 1.7.1.1