diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-02-19 14:14:10 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-02-19 14:16:47 +0100 |
commit | 259d33db8cae4f139c4646077f1637a8224dfdb2 (patch) | |
tree | 4c3e10f62ae17e8419578df9cd7ef306dea3dd32 /activemodel | |
parent | ad5834f20f3749cc0f6d512d23f644c64c6bcc2c (diff) | |
parent | fd38838f29ff0f4514b711c65cafe55c6f21ad2a (diff) | |
download | rails-259d33db8cae4f139c4646077f1637a8224dfdb2.tar.gz rails-259d33db8cae4f139c4646077f1637a8224dfdb2.tar.bz2 rails-259d33db8cae4f139c4646077f1637a8224dfdb2.zip |
Merge pull request #18996 from morgoth/deprecate-more-errors-methods
Deprecate `ActiveModel::Errors` `add_on_empty` and `add_on_blank` methods
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 16 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 28 | ||||
-rw-r--r-- | activemodel/test/cases/validations/i18n_generate_message_validation_test.rb | 2 |
4 files changed, 43 insertions, 8 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index a3e00e4dc9..c21083698e 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,8 @@ +* Deprecate `ActiveModel::Errors#add_on_empty` and `ActiveModel::Errors#add_on_blank` + with no replacement. + + *Wojciech Wnętrzak* + * Deprecate `ActiveModel::Errors#get`, `ActiveModel::Errors#set` and `ActiveModel::Errors#[]=` methods that have inconsistent behaviour. diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index b1d090b830..166911f0fa 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -347,6 +347,14 @@ module ActiveModel # person.errors.messages # # => {:name=>["can't be empty"]} def add_on_empty(attributes, options = {}) + ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) + ActiveModel::Errors#add_on_empty is deprecated and will be removed in Rails 5.1 + + To achieve the same use: + + errors.add(attribute, :empty, options) if value.nil? || value.empty? + MESSAGE + Array(attributes).each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) is_empty = value.respond_to?(:empty?) ? value.empty? : false @@ -361,6 +369,14 @@ module ActiveModel # person.errors.messages # # => {:name=>["can't be blank"]} def add_on_blank(attributes, options = {}) + ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) + ActiveModel::Errors#add_on_blank is deprecated and will be removed in Rails 5.1 + + To achieve the same use: + + errors.add(attribute, :empty, options) if value.blank? + MESSAGE + Array(attributes).each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) add(attribute, :blank, options) if value.blank? diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index e736ccf6a0..da142ea2c0 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -288,46 +288,60 @@ class ErrorsTest < ActiveModel::TestCase test "add_on_empty generates message" do person = Person.new person.errors.expects(:generate_message).with(:name, :empty, {}) - person.errors.add_on_empty :name + assert_deprecated do + person.errors.add_on_empty :name + end 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] + assert_deprecated do + person.errors.add_on_empty [:name, :age] + end 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' + assert_deprecated do + person.errors.add_on_empty :name, message: 'custom' + end end test "add_on_empty generates message with empty string value" do person = Person.new person.name = '' person.errors.expects(:generate_message).with(:name, :empty, {}) - person.errors.add_on_empty :name + assert_deprecated do + person.errors.add_on_empty :name + end 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 + assert_deprecated do + person.errors.add_on_blank :name + end 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] + assert_deprecated do + person.errors.add_on_blank [:name, :age] + end 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' + assert_deprecated do + person.errors.add_on_blank :name, message: 'custom' + end end test "details returns added error detail" do diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 3eeb80a48b..da63df9152 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -62,7 +62,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase assert_equal 'custom message', @person.errors.generate_message(:title, :empty, message: 'custom message') end - # add_on_blank: generate_message(attr, :blank, message: custom_message) + # validates_presence_of: generate_message(attr, :blank, message: custom_message) def test_generate_message_blank_with_default_message assert_equal "can't be blank", @person.errors.generate_message(:title, :blank) end |