Merge pull request #8118 from nashby/activemodel-errors
use Array() instead of flatten
This commit is contained in:
commit
ce9b6ad5ae
|
@ -308,7 +308,7 @@ module ActiveModel
|
|||
# person.errors.messages
|
||||
# # => {:name=>["can't be empty"]}
|
||||
def add_on_empty(attributes, options = {})
|
||||
[attributes].flatten.each do |attribute|
|
||||
Array(attributes).each do |attribute|
|
||||
value = @base.send(:read_attribute_for_validation, attribute)
|
||||
is_empty = value.respond_to?(:empty?) ? value.empty? : false
|
||||
add(attribute, :empty, options) if value.nil? || is_empty
|
||||
|
@ -322,7 +322,7 @@ module ActiveModel
|
|||
# person.errors.messages
|
||||
# # => {:name=>["can't be blank"]}
|
||||
def add_on_blank(attributes, options = {})
|
||||
[attributes].flatten.each do |attribute|
|
||||
Array(attributes).each do |attribute|
|
||||
value = @base.send(:read_attribute_for_validation, attribute)
|
||||
add(attribute, :blank, options) if value.blank?
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ class ErrorsTest < ActiveModel::TestCase
|
|||
@errors = ActiveModel::Errors.new(self)
|
||||
end
|
||||
|
||||
attr_accessor :name
|
||||
attr_accessor :name, :age
|
||||
attr_reader :errors
|
||||
|
||||
def validate!
|
||||
|
@ -201,5 +201,43 @@ class ErrorsTest < ActiveModel::TestCase
|
|||
person.errors.generate_message(:name, :blank)
|
||||
}
|
||||
end
|
||||
|
||||
test "add_on_empty generates message" do
|
||||
person = Person.new
|
||||
person.errors.expects(:generate_message).with(:name, :empty, {})
|
||||
person.errors.add_on_empty :name
|
||||
end
|
||||
|
||||
test "add_on_empty generates message for multiple attributes" do
|
||||
person = Person.new
|
||||
person.errors.expects(:generate_message).with(:name, :empty, {})
|
||||
person.errors.expects(:generate_message).with(:age, :empty, {})
|
||||
person.errors.add_on_empty [:name, :age]
|
||||
end
|
||||
|
||||
test "add_on_empty generates message with custom default message" do
|
||||
person = Person.new
|
||||
person.errors.expects(:generate_message).with(:name, :empty, {:message => 'custom'})
|
||||
person.errors.add_on_empty :name, :message => 'custom'
|
||||
end
|
||||
|
||||
test "add_on_blank generates message" do
|
||||
person = Person.new
|
||||
person.errors.expects(:generate_message).with(:name, :blank, {})
|
||||
person.errors.add_on_blank :name
|
||||
end
|
||||
|
||||
test "add_on_blank generates message for multiple attributes" do
|
||||
person = Person.new
|
||||
person.errors.expects(:generate_message).with(:name, :blank, {})
|
||||
person.errors.expects(:generate_message).with(:age, :blank, {})
|
||||
person.errors.add_on_blank [:name, :age]
|
||||
end
|
||||
|
||||
test "add_on_blank generates message with custom default message" do
|
||||
person = Person.new
|
||||
person.errors.expects(:generate_message).with(:name, :blank, {:message => 'custom'})
|
||||
person.errors.add_on_blank :name, :message => 'custom'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -21,26 +21,6 @@ class I18nValidationTest < ActiveModel::TestCase
|
|||
I18n.backend = @old_backend
|
||||
end
|
||||
|
||||
def test_errors_add_on_empty_generates_message
|
||||
@person.errors.expects(:generate_message).with(:title, :empty, {})
|
||||
@person.errors.add_on_empty :title
|
||||
end
|
||||
|
||||
def test_errors_add_on_empty_generates_message_with_custom_default_message
|
||||
@person.errors.expects(:generate_message).with(:title, :empty, {:message => 'custom'})
|
||||
@person.errors.add_on_empty :title, :message => 'custom'
|
||||
end
|
||||
|
||||
def test_errors_add_on_blank_generates_message
|
||||
@person.errors.expects(:generate_message).with(:title, :blank, {})
|
||||
@person.errors.add_on_blank :title
|
||||
end
|
||||
|
||||
def test_errors_add_on_blank_generates_message_with_custom_default_message
|
||||
@person.errors.expects(:generate_message).with(:title, :blank, {:message => 'custom'})
|
||||
@person.errors.add_on_blank :title, :message => 'custom'
|
||||
end
|
||||
|
||||
def test_full_message_encoding
|
||||
I18n.backend.store_translations('en', :errors => {
|
||||
:messages => { :too_short => '猫舌' }})
|
||||
|
|
Loading…
Reference in New Issue