diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations/with.rb | 8 | ||||
-rw-r--r-- | activemodel/lib/active_model/validator.rb | 7 | ||||
-rw-r--r-- | activemodel/test/cases/validations/with_validation_test.rb | 12 |
3 files changed, 12 insertions, 15 deletions
diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb index edc2133ddc..8313aded11 100644 --- a/activemodel/lib/active_model/validations/with.rb +++ b/activemodel/lib/active_model/validations/with.rb @@ -49,12 +49,10 @@ module ActiveModel # end # def validates_with(*args) - configuration = args.extract_options! + options = args.extract_options! - validate configuration do |record| - args.each do |klass| - klass.new(record, configuration.except(:on, :if, :unless)).validate - end + args.each do |klass| + validate klass.new(options.except(:on, :if, :unless)), options end end end diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 09de72b757..80a538fcb6 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -54,14 +54,13 @@ module ActiveModel #:nodoc: # end # class Validator - attr_reader :record, :options + attr_reader :options - def initialize(record, options) - @record = record + def initialize(options) @options = options end - def validate + def validate(record) raise "You must override this method" end end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index fae87a6188..4350ad69ec 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -14,24 +14,24 @@ class ValidatesWithTest < ActiveRecord::TestCase OTHER_ERROR_MESSAGE = "Validation error from other validator" class ValidatorThatAddsErrors < ActiveModel::Validator - def validate() + def validate(record) record.errors[:base] << ERROR_MESSAGE end end class OtherValidatorThatAddsErrors < ActiveModel::Validator - def validate() + def validate(record) record.errors[:base] << OTHER_ERROR_MESSAGE end end class ValidatorThatDoesNotAddErrors < ActiveModel::Validator - def validate() + def validate(record) end end class ValidatorThatValidatesOptions < ActiveModel::Validator - def validate() + def validate(record) if options[:field] == :first_name record.errors[:base] << ERROR_MESSAGE end @@ -101,8 +101,8 @@ class ValidatesWithTest < ActiveRecord::TestCase test "passes all non-standard configuration options to the validator class" do topic = Topic.new validator = mock() - validator.expects(:new).with(topic, {:foo => :bar}).returns(validator) - validator.expects(:validate) + validator.expects(:new).with({:foo => :bar}).returns(validator) + validator.expects(:validate).with(topic) Topic.validates_with(validator, :if => "1 == 1", :foo => :bar) assert topic.valid? |