aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/lib/active_model/errors.rb8
-rw-r--r--activemodel/lib/active_model/validations/presence.rb4
-rw-r--r--activemodel/test/cases/validations/presence_validation_test.rb10
3 files changed, 8 insertions, 14 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 92c2512aa4..963e52bff3 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -322,15 +322,9 @@ module ActiveModel
# 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
diff --git a/activemodel/lib/active_model/validations/presence.rb b/activemodel/lib/active_model/validations/presence.rb
index ae84c376b9..ab8c8359fc 100644
--- a/activemodel/lib/active_model/validations/presence.rb
+++ b/activemodel/lib/active_model/validations/presence.rb
@@ -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
diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb
index fe2ee7b409..f77d4da29f 100644
--- a/activemodel/test/cases/validations/presence_validation_test.rb
+++ b/activemodel/test/cases/validations/presence_validation_test.rb
@@ -41,7 +41,7 @@ class PresenceValidationTest < ActiveModel::TestCase
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
@@ -72,9 +72,9 @@ class PresenceValidationTest < ActiveModel::TestCase
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 = ""
@@ -90,9 +90,9 @@ class PresenceValidationTest < ActiveModel::TestCase
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 = ""