Skip to content
This repository
Browse code

Merge pull request #8118 from nashby/activemodel-errors

use Array() instead of flatten
  • Loading branch information...
commit ce9b6ad5ae8de8947bea8ba06dcad27e09a0dbac 2 parents b8b100a + 70ecf6c
Rafael Mendonça França rafaelfranca authored
4 activemodel/lib/active_model/errors.rb
@@ -308,7 +308,7 @@ def add(attribute, message = nil, options = {})
308 308 # person.errors.messages
309 309 # # => {:name=>["can't be empty"]}
310 310 def add_on_empty(attributes, options = {})
311   - [attributes].flatten.each do |attribute|
  311 + Array(attributes).each do |attribute|
312 312 value = @base.send(:read_attribute_for_validation, attribute)
313 313 is_empty = value.respond_to?(:empty?) ? value.empty? : false
314 314 add(attribute, :empty, options) if value.nil? || is_empty
@@ -322,7 +322,7 @@ def add_on_empty(attributes, options = {})
322 322 # person.errors.messages
323 323 # # => {:name=>["can't be blank"]}
324 324 def add_on_blank(attributes, options = {})
325   - [attributes].flatten.each do |attribute|
  325 + Array(attributes).each do |attribute|
326 326 value = @base.send(:read_attribute_for_validation, attribute)
327 327 add(attribute, :blank, options) if value.blank?
328 328 end
40 activemodel/test/cases/errors_test.rb
@@ -7,7 +7,7 @@ def initialize
7 7 @errors = ActiveModel::Errors.new(self)
8 8 end
9 9
10   - attr_accessor :name
  10 + attr_accessor :name, :age
11 11 attr_reader :errors
12 12
13 13 def validate!
@@ -201,5 +201,43 @@ def test_has_key?
201 201 person.errors.generate_message(:name, :blank)
202 202 }
203 203 end
  204 +
  205 + test "add_on_empty generates message" do
  206 + person = Person.new
  207 + person.errors.expects(:generate_message).with(:name, :empty, {})
  208 + person.errors.add_on_empty :name
  209 + end
  210 +
  211 + test "add_on_empty generates message for multiple attributes" do
  212 + person = Person.new
  213 + person.errors.expects(:generate_message).with(:name, :empty, {})
  214 + person.errors.expects(:generate_message).with(:age, :empty, {})
  215 + person.errors.add_on_empty [:name, :age]
  216 + end
  217 +
  218 + test "add_on_empty generates message with custom default message" do
  219 + person = Person.new
  220 + person.errors.expects(:generate_message).with(:name, :empty, {:message => 'custom'})
  221 + person.errors.add_on_empty :name, :message => 'custom'
  222 + end
  223 +
  224 + test "add_on_blank generates message" do
  225 + person = Person.new
  226 + person.errors.expects(:generate_message).with(:name, :blank, {})
  227 + person.errors.add_on_blank :name
  228 + end
  229 +
  230 + test "add_on_blank generates message for multiple attributes" do
  231 + person = Person.new
  232 + person.errors.expects(:generate_message).with(:name, :blank, {})
  233 + person.errors.expects(:generate_message).with(:age, :blank, {})
  234 + person.errors.add_on_blank [:name, :age]
  235 + end
  236 +
  237 + test "add_on_blank generates message with custom default message" do
  238 + person = Person.new
  239 + person.errors.expects(:generate_message).with(:name, :blank, {:message => 'custom'})
  240 + person.errors.add_on_blank :name, :message => 'custom'
  241 + end
204 242 end
205 243
20 activemodel/test/cases/validations/i18n_validation_test.rb
@@ -21,26 +21,6 @@ def teardown
21 21 I18n.backend = @old_backend
22 22 end
23 23
24   - def test_errors_add_on_empty_generates_message
25   - @person.errors.expects(:generate_message).with(:title, :empty, {})
26   - @person.errors.add_on_empty :title
27   - end
28   -
29   - def test_errors_add_on_empty_generates_message_with_custom_default_message
30   - @person.errors.expects(:generate_message).with(:title, :empty, {:message => 'custom'})
31   - @person.errors.add_on_empty :title, :message => 'custom'
32   - end
33   -
34   - def test_errors_add_on_blank_generates_message
35   - @person.errors.expects(:generate_message).with(:title, :blank, {})
36   - @person.errors.add_on_blank :title
37   - end
38   -
39   - def test_errors_add_on_blank_generates_message_with_custom_default_message
40   - @person.errors.expects(:generate_message).with(:title, :blank, {:message => 'custom'})
41   - @person.errors.add_on_blank :title, :message => 'custom'
42   - end
43   -
44 24 def test_full_message_encoding
45 25 I18n.backend.store_translations('en', :errors => {
46 26 :messages => { :too_short => '猫舌' }})

0 comments on commit ce9b6ad

Please sign in to comment.
Something went wrong with that request. Please try again.