1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Revert "Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once"

This reverts commit 51dd258843, reversing
changes made to ecb4e4b21b.

This broke Active Record tests
This commit is contained in:
Rafael Mendonça França 2015-09-07 17:16:54 -03:00
parent 51dd258843
commit 7b9b0b531f
5 changed files with 2 additions and 50 deletions

View file

@ -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*

View file

@ -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

View file

@ -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

View file

@ -54,7 +54,7 @@ module ActiveRecord
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
# some <tt>:on</tt> option will only run in the specified context.
def valid?(context = nil)
context ||= default_validation_context
context ||= (new_record? ? :create : :update)
output = super(context)
errors.empty? && output
end
@ -63,10 +63,6 @@ module ActiveRecord
protected
def default_validation_context
[new_record? ? :create : :update]
end
def raise_validation_error
raise(RecordInvalid.new(self))
end

View file

@ -52,13 +52,6 @@ class ValidationsTest < ActiveRecord::TestCase
assert r.valid?(:special_case)
end
def test_invalid_using_multiple_contexts
r = WrongReply.new(:title => 'Wrong Create')
assert r.invalid?([:special_case, :create])
assert_equal "Invalid", r.errors[:author_name].join
assert_equal "is Wrong Create", r.errors[:title].join
end
def test_validate
r = WrongReply.new