Skip to content

Commit 9de6457

Browse files
committed
Removed deprecated methods in ActiveModel::Errors
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
1 parent d1fc0a5 commit 9de6457

File tree

3 files changed

+6
-179
lines changed

3 files changed

+6
-179
lines changed

activemodel/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
* Removed deprecated methods in `ActiveModel::Errors`.
2+
3+
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
4+
5+
*Rafael Mendonça França*
6+
17

28
Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activemodel/CHANGELOG.md) for previous changes.

activemodel/lib/active_model/errors.rb

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -115,36 +115,6 @@ def include?(attribute)
115115
alias :has_key? :include?
116116
alias :key? :include?
117117

118-
# Get messages for +key+.
119-
#
120-
# person.errors.messages # => {:name=>["cannot be nil"]}
121-
# person.errors.get(:name) # => ["cannot be nil"]
122-
# person.errors.get(:age) # => []
123-
def get(key)
124-
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
125-
ActiveModel::Errors#get is deprecated and will be removed in Rails 5.1.
126-
127-
To achieve the same use model.errors[:#{key}].
128-
MESSAGE
129-
130-
messages[key]
131-
end
132-
133-
# Set messages for +key+ to +value+.
134-
#
135-
# person.errors[:name] # => ["cannot be nil"]
136-
# person.errors.set(:name, ["can't be nil"])
137-
# person.errors[:name] # => ["can't be nil"]
138-
def set(key, value)
139-
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
140-
ActiveModel::Errors#set is deprecated and will be removed in Rails 5.1.
141-
142-
Use model.errors.add(:#{key}, #{value.inspect}) instead.
143-
MESSAGE
144-
145-
messages[key] = value
146-
end
147-
148118
# Delete messages for +key+. Returns the deleted messages.
149119
#
150120
# person.errors[:name] # => ["cannot be nil"]
@@ -173,20 +143,6 @@ def [](attribute)
173143
messages[attribute.to_sym]
174144
end
175145

176-
# Adds to the supplied attribute the supplied error message.
177-
#
178-
# person.errors[:name] = "must be set"
179-
# person.errors[:name] # => ['must be set']
180-
def []=(attribute, error)
181-
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
182-
ActiveModel::Errors#[]= is deprecated and will be removed in Rails 5.1.
183-
184-
Use model.errors.add(:#{attribute}, #{error.inspect}) instead.
185-
MESSAGE
186-
187-
messages[attribute.to_sym] << error
188-
end
189-
190146
# Iterates through each error key, value pair in the error messages hash.
191147
# Yields the attribute and the error for that attribute. If the attribute
192148
# has more than one error message, yields once for each error message.
@@ -338,49 +294,6 @@ def add(attribute, message = :invalid, options = {})
338294
messages[attribute.to_sym] << message
339295
end
340296

341-
# Will add an error message to each of the attributes in +attributes+
342-
# that is empty.
343-
#
344-
# person.errors.add_on_empty(:name)
345-
# person.errors.messages
346-
# # => {:name=>["can't be empty"]}
347-
def add_on_empty(attributes, options = {})
348-
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
349-
ActiveModel::Errors#add_on_empty is deprecated and will be removed in Rails 5.1.
350-
351-
To achieve the same use:
352-
353-
errors.add(attribute, :empty, options) if value.nil? || value.empty?
354-
MESSAGE
355-
356-
Array(attributes).each do |attribute|
357-
value = @base.send(:read_attribute_for_validation, attribute)
358-
is_empty = value.respond_to?(:empty?) ? value.empty? : false
359-
add(attribute, :empty, options) if value.nil? || is_empty
360-
end
361-
end
362-
363-
# Will add an error message to each of the attributes in +attributes+ that
364-
# is blank (using Object#blank?).
365-
#
366-
# person.errors.add_on_blank(:name)
367-
# person.errors.messages
368-
# # => {:name=>["can't be blank"]}
369-
def add_on_blank(attributes, options = {})
370-
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
371-
ActiveModel::Errors#add_on_blank is deprecated and will be removed in Rails 5.1.
372-
373-
To achieve the same use:
374-
375-
errors.add(attribute, :blank, options) if value.blank?
376-
MESSAGE
377-
378-
Array(attributes).each do |attribute|
379-
value = @base.send(:read_attribute_for_validation, attribute)
380-
add(attribute, :blank, options) if value.blank?
381-
end
382-
end
383-
384297
# Returns +true+ if an error on the attribute with the given message is
385298
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
386299
#

activemodel/test/cases/errors_test.rb

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,6 @@ def test_no_key
7979
assert person.errors.empty?
8080
end
8181

82-
test "get returns the errors for the provided key" do
83-
errors = ActiveModel::Errors.new(self)
84-
errors[:foo] << "omg"
85-
86-
assert_deprecated do
87-
assert_equal ["omg"], errors.get(:foo)
88-
end
89-
end
90-
91-
test "sets the error with the provided key" do
92-
errors = ActiveModel::Errors.new(self)
93-
assert_deprecated do
94-
errors.set(:foo, "omg")
95-
end
96-
97-
assert_equal({ foo: "omg" }, errors.messages)
98-
end
99-
10082
test "error access is indifferent" do
10183
errors = ActiveModel::Errors.new(self)
10284
errors[:foo] << "omg"
@@ -142,14 +124,6 @@ def test_no_key
142124
assert_equal ["cannot be nil"], person.errors[:name]
143125
end
144126

145-
test "assign error" do
146-
person = Person.new
147-
assert_deprecated do
148-
person.errors[:name] = "should not be nil"
149-
end
150-
assert_equal ["should not be nil"], person.errors[:name]
151-
end
152-
153127
test "add an error message on a specific attribute" do
154128
person = Person.new
155129
person.errors.add(:name, "cannot be blank")
@@ -320,72 +294,6 @@ def test_no_key
320294
}
321295
end
322296

323-
test "add_on_empty generates message" do
324-
person = Person.new
325-
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
326-
assert_deprecated do
327-
person.errors.add_on_empty :name
328-
end
329-
end
330-
end
331-
332-
test "add_on_empty generates message for multiple attributes" do
333-
person = Person.new
334-
expected_calls = [ [:name, :empty, {}], [:age, :empty, {}] ]
335-
assert_called_with(person.errors, :generate_message, expected_calls) do
336-
assert_deprecated do
337-
person.errors.add_on_empty [:name, :age]
338-
end
339-
end
340-
end
341-
342-
test "add_on_empty generates message with custom default message" do
343-
person = Person.new
344-
assert_called_with(person.errors, :generate_message, [:name, :empty, { message: "custom" }]) do
345-
assert_deprecated do
346-
person.errors.add_on_empty :name, message: "custom"
347-
end
348-
end
349-
end
350-
351-
test "add_on_empty generates message with empty string value" do
352-
person = Person.new
353-
person.name = ""
354-
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
355-
assert_deprecated do
356-
person.errors.add_on_empty :name
357-
end
358-
end
359-
end
360-
361-
test "add_on_blank generates message" do
362-
person = Person.new
363-
assert_called_with(person.errors, :generate_message, [:name, :blank, {}]) do
364-
assert_deprecated do
365-
person.errors.add_on_blank :name
366-
end
367-
end
368-
end
369-
370-
test "add_on_blank generates message for multiple attributes" do
371-
person = Person.new
372-
expected_calls = [ [:name, :blank, {}], [:age, :blank, {}] ]
373-
assert_called_with(person.errors, :generate_message, expected_calls) do
374-
assert_deprecated do
375-
person.errors.add_on_blank [:name, :age]
376-
end
377-
end
378-
end
379-
380-
test "add_on_blank generates message with custom default message" do
381-
person = Person.new
382-
assert_called_with(person.errors, :generate_message, [:name, :blank, { message: "custom" }]) do
383-
assert_deprecated do
384-
person.errors.add_on_blank :name, message: "custom"
385-
end
386-
end
387-
end
388-
389297
test "details returns added error detail" do
390298
person = Person.new
391299
person.errors.add(:name, :invalid)

0 commit comments

Comments
 (0)