Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8118 from nashby/activemodel-errors

use Array() instead of flatten
  • Loading branch information...
commit ce9b6ad5ae8de8947bea8ba06dcad27e09a0dbac 2 parents b8b100a + 70ecf6c
@rafaelfranca rafaelfranca authored
View
4 activemodel/lib/active_model/errors.rb
@@ -308,7 +308,7 @@ def add(attribute, message = nil, options = {})
# 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 @@ def add_on_empty(attributes, options = {})
# 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
View
40 activemodel/test/cases/errors_test.rb
@@ -7,7 +7,7 @@ def initialize
@errors = ActiveModel::Errors.new(self)
end
- attr_accessor :name
+ attr_accessor :name, :age
attr_reader :errors
def validate!
@@ -201,5 +201,43 @@ def test_has_key?
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
View
20 activemodel/test/cases/validations/i18n_validation_test.rb
@@ -21,26 +21,6 @@ def teardown
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 => '猫舌' }})
Please sign in to comment.
Something went wrong with that request. Please try again.