1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Removed deprecated methods in ActiveModel::Errors

`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
This commit is contained in:
Rafael Mendonça França 2016-10-10 20:22:15 -03:00
parent d1fc0a5eb2
commit 9de6457ab0
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
3 changed files with 6 additions and 179 deletions

View file

@ -1,2 +1,8 @@
* Removed deprecated methods in `ActiveModel::Errors`.
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
*Rafael Mendonça França*
Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activemodel/CHANGELOG.md) for previous changes.

View file

@ -115,36 +115,6 @@ module ActiveModel
alias :has_key? :include?
alias :key? :include?
# Get messages for +key+.
#
# person.errors.messages # => {:name=>["cannot be nil"]}
# person.errors.get(:name) # => ["cannot be nil"]
# person.errors.get(:age) # => []
def get(key)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#get is deprecated and will be removed in Rails 5.1.
To achieve the same use model.errors[:#{key}].
MESSAGE
messages[key]
end
# Set messages for +key+ to +value+.
#
# person.errors[:name] # => ["cannot be nil"]
# person.errors.set(:name, ["can't be nil"])
# person.errors[:name] # => ["can't be nil"]
def set(key, value)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#set is deprecated and will be removed in Rails 5.1.
Use model.errors.add(:#{key}, #{value.inspect}) instead.
MESSAGE
messages[key] = value
end
# Delete messages for +key+. Returns the deleted messages.
#
# person.errors[:name] # => ["cannot be nil"]
@ -173,20 +143,6 @@ module ActiveModel
messages[attribute.to_sym]
end
# Adds to the supplied attribute the supplied error message.
#
# person.errors[:name] = "must be set"
# person.errors[:name] # => ['must be set']
def []=(attribute, error)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#[]= is deprecated and will be removed in Rails 5.1.
Use model.errors.add(:#{attribute}, #{error.inspect}) instead.
MESSAGE
messages[attribute.to_sym] << error
end
# Iterates through each error key, value pair in the error messages hash.
# Yields the attribute and the error for that attribute. If the attribute
# has more than one error message, yields once for each error message.
@ -338,49 +294,6 @@ module ActiveModel
messages[attribute.to_sym] << message
end
# Will add an error message to each of the attributes in +attributes+
# that is empty.
#
# person.errors.add_on_empty(:name)
# 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
add(attribute, :empty, options) if value.nil? || is_empty
end
end
# Will add an error message to each of the attributes in +attributes+ that
# is blank (using Object#blank?).
#
# person.errors.add_on_blank(:name)
# 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, :blank, 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?
end
end
# Returns +true+ if an error on the attribute with the given message is
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
#

View file

@ -79,24 +79,6 @@ class ErrorsTest < ActiveModel::TestCase
assert person.errors.empty?
end
test "get returns the errors for the provided key" do
errors = ActiveModel::Errors.new(self)
errors[:foo] << "omg"
assert_deprecated do
assert_equal ["omg"], errors.get(:foo)
end
end
test "sets the error with the provided key" do
errors = ActiveModel::Errors.new(self)
assert_deprecated do
errors.set(:foo, "omg")
end
assert_equal({ foo: "omg" }, errors.messages)
end
test "error access is indifferent" do
errors = ActiveModel::Errors.new(self)
errors[:foo] << "omg"
@ -142,14 +124,6 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal ["cannot be nil"], person.errors[:name]
end
test "assign error" do
person = Person.new
assert_deprecated do
person.errors[:name] = "should not be nil"
end
assert_equal ["should not be nil"], person.errors[:name]
end
test "add an error message on a specific attribute" do
person = Person.new
person.errors.add(:name, "cannot be blank")
@ -320,72 +294,6 @@ class ErrorsTest < ActiveModel::TestCase
}
end
test "add_on_empty generates message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
assert_deprecated do
person.errors.add_on_empty :name
end
end
end
test "add_on_empty generates message for multiple attributes" do
person = Person.new
expected_calls = [ [:name, :empty, {}], [:age, :empty, {}] ]
assert_called_with(person.errors, :generate_message, expected_calls) do
assert_deprecated do
person.errors.add_on_empty [:name, :age]
end
end
end
test "add_on_empty generates message with custom default message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :empty, { message: "custom" }]) do
assert_deprecated do
person.errors.add_on_empty :name, message: "custom"
end
end
end
test "add_on_empty generates message with empty string value" do
person = Person.new
person.name = ""
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
assert_deprecated do
person.errors.add_on_empty :name
end
end
end
test "add_on_blank generates message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :blank, {}]) do
assert_deprecated do
person.errors.add_on_blank :name
end
end
end
test "add_on_blank generates message for multiple attributes" do
person = Person.new
expected_calls = [ [:name, :blank, {}], [:age, :blank, {}] ]
assert_called_with(person.errors, :generate_message, expected_calls) do
assert_deprecated do
person.errors.add_on_blank [:name, :age]
end
end
end
test "add_on_blank generates message with custom default message" do
person = Person.new
assert_called_with(person.errors, :generate_message, [:name, :blank, { message: "custom" }]) do
assert_deprecated do
person.errors.add_on_blank :name, message: "custom"
end
end
end
test "details returns added error detail" do
person = Person.new
person.errors.add(:name, :invalid)