Skip to content

Commit

Permalink
Removed deprecated methods in ActiveModel::Errors
Browse files Browse the repository at this point in the history
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
  • Loading branch information
rafaelfranca committed Oct 10, 2016
1 parent d1fc0a5 commit 9de6457
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 179 deletions.
6 changes: 6 additions & 0 deletions activemodel/CHANGELOG.md
@@ -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.
87 changes: 0 additions & 87 deletions activemodel/lib/active_model/errors.rb
Expand Up @@ -115,36 +115,6 @@ def include?(attribute)
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"]
Expand Down Expand Up @@ -173,20 +143,6 @@ def [](attribute)
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.
Expand Down Expand Up @@ -338,49 +294,6 @@ def add(attribute, message = :invalid, options = {})
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+.
#
Expand Down
92 changes: 0 additions & 92 deletions activemodel/test/cases/errors_test.rb
Expand Up @@ -79,24 +79,6 @@ def test_no_key
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"
Expand Down Expand Up @@ -142,14 +124,6 @@ def test_no_key
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")
Expand Down Expand Up @@ -320,72 +294,6 @@ def test_no_key
}
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)
Expand Down

0 comments on commit 9de6457

Please sign in to comment.