From 7b9b0b531f9cb6a64737efc5c46890b4609847bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 7 Sep 2015 17:16:54 -0300 Subject: Revert "Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once" This reverts commit 51dd2588433457960cca592d5b5dac6e0537feac, reversing changes made to ecb4e4b21b3222b823fa24d4a0598b1f2f63ecfb. This broke Active Record tests --- activemodel/CHANGELOG.md | 18 ------------------ activemodel/lib/active_model/validations.rb | 2 +- .../cases/validations/validations_context_test.rb | 19 ------------------- 3 files changed, 1 insertion(+), 38 deletions(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 769c0d6291..54fe5794e1 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,21 +1,3 @@ -* Validate multiple contexts on `valid?` and `invalid?` at once. - - Example: - - class Person - include ActiveModel::Validations - - attr_reader :name, :title - validates_presence_of :name, on: :create - validates_presence_of :title, on: :update - end - - person = Person.new - person.valid?([:create, :update]) # => true - person.errors.messages # => {:name=>["can't be blank"], :title=>["can't be blank"]} - - *Dmitry Polushkin* - * Add case_sensitive option for confirmation validator in models. *Akshat Sharma* diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index f23c920d87..5f1dde4aa3 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -162,7 +162,7 @@ module ActiveModel options = options.dup options[:if] = Array(options[:if]) options[:if].unshift ->(o) { - !(Array(options[:on]) & Array(o.validation_context)).empty? + Array(options[:on]).include?(o.validation_context) } end diff --git a/activemodel/test/cases/validations/validations_context_test.rb b/activemodel/test/cases/validations/validations_context_test.rb index b901a1523e..150dce379f 100644 --- a/activemodel/test/cases/validations/validations_context_test.rb +++ b/activemodel/test/cases/validations/validations_context_test.rb @@ -8,7 +8,6 @@ class ValidationsContextTest < ActiveModel::TestCase end ERROR_MESSAGE = "Validation error from validator" - ANOTHER_ERROR_MESSAGE = "Another validation error from validator" class ValidatorThatAddsErrors < ActiveModel::Validator def validate(record) @@ -16,12 +15,6 @@ class ValidationsContextTest < ActiveModel::TestCase end end - class AnotherValidatorThatAddsErrors < ActiveModel::Validator - def validate(record) - record.errors[:base] << ANOTHER_ERROR_MESSAGE - end - end - test "with a class that adds errors on create and validating a new model with no arguments" do Topic.validates_with(ValidatorThatAddsErrors, on: :create) topic = Topic.new @@ -53,16 +46,4 @@ class ValidationsContextTest < ActiveModel::TestCase assert topic.invalid?(:context2), "Validation did not run on context2 when 'on' is set to context1 and context2" assert topic.errors[:base].include?(ERROR_MESSAGE) end - - test "with a class that validating a model for a multiple contexts" do - Topic.validates_with(ValidatorThatAddsErrors, on: :context1) - Topic.validates_with(AnotherValidatorThatAddsErrors, on: :context2) - - topic = Topic.new - assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2" - - assert topic.invalid?([:context1, :context2]), "Validation did not run on context1 when 'on' is set to context1 and context2" - assert topic.errors[:base].include?(ERROR_MESSAGE) - assert topic.errors[:base].include?(ANOTHER_ERROR_MESSAGE) - end end -- cgit v1.2.3