Skip to content

Commit

Permalink
Revert the change at ActiveModel::Errors#add_on_blank and fix in the
Browse files Browse the repository at this point in the history
right place.

The EachValidator#validate already handle :allow_blank and :allow_nil,
correctly.

Closes #8622.

Fix #8621.
  • Loading branch information
rafaelfranca committed Dec 26, 2012
1 parent d4c30a0 commit 78fd14c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
8 changes: 1 addition & 7 deletions activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,9 @@ def add_on_empty(attributes, options = {})
# person.errors.messages
# # => {:name=>["can't be blank"]}
def add_on_blank(attributes, options = {})
return if options[:allow_blank]

Array(attributes).each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
if value.nil?
add(attribute, :blank, options) unless options[:allow_nil]
elsif value.blank?
add(attribute, :blank, options)
end
add(attribute, :blank, options) if value.blank?
end
end

Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module ActiveModel

module Validations
class PresenceValidator < EachValidator # :nodoc:
def validate(record)
record.errors.add_on_blank(attributes, options)
def validate_each(record, attr_name, value)
record.errors.add(attr_name, :blank, options) if value.blank?
end
end

Expand Down
10 changes: 5 additions & 5 deletions activemodel/test/cases/validations/presence_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_accepts_array_arguments
end

def test_validates_acceptance_of_with_custom_error_using_quotes
Person.validates_presence_of :karma, :message => "This string contains 'single' and \"double\" quotes"
Person.validates_presence_of :karma, message: "This string contains 'single' and \"double\" quotes"
p = Person.new
assert p.invalid?
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
Expand Down Expand Up @@ -72,9 +72,9 @@ def test_validates_presence_of_for_ruby_class_with_custom_reader
end

def test_allow_nil
Topic.validates_presence_of(:title, :allow_nil => true)
Topic.validates_presence_of(:title, allow_nil: true)

t = Topic.new(:title => "something")
t = Topic.new(title: "something")
assert t.valid?, t.errors.full_messages

t.title = ""
Expand All @@ -90,9 +90,9 @@ def test_allow_nil
end

def test_allow_blank
Topic.validates_presence_of(:title, :allow_blank => true)
Topic.validates_presence_of(:title, allow_blank: true)

t = Topic.new(:title => "something")
t = Topic.new(title: "something")
assert t.valid?, t.errors.full_messages

t.title = ""
Expand Down

0 comments on commit 78fd14c

Please sign in to comment.