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

Change the deprecation messages to show the preferred way to work with

ActiveModel::Errors
This commit is contained in:
Rafael Mendonça França 2015-02-20 20:57:56 -02:00
parent 08d9c7532c
commit f55bfe7260
4 changed files with 12 additions and 12 deletions

View file

@ -49,7 +49,7 @@ behavior out of the box:
send("#{attr}=", nil) send("#{attr}=", nil)
end end
end end
person = Person.new person = Person.new
person.clear_name person.clear_name
person.clear_age person.clear_age
@ -132,7 +132,7 @@ behavior out of the box:
"Name" "Name"
end end
end end
person = Person.new person = Person.new
person.name = nil person.name = nil
person.validate! person.validate!
@ -216,10 +216,10 @@ behavior out of the box:
{Learn more}[link:classes/ActiveModel/Validations.html] {Learn more}[link:classes/ActiveModel/Validations.html]
* Custom validators * Custom validators
class HasNameValidator < ActiveModel::Validator class HasNameValidator < ActiveModel::Validator
def validate(record) def validate(record)
record.errors.messages[:name] << "must exist" if record.name.blank? record.errors.add(:name, "must exist") if record.name.blank?
end end
end end

View file

@ -114,9 +114,9 @@ module ActiveModel
# person.errors.get(:age) # => [] # person.errors.get(:age) # => []
def get(key) def get(key)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#get is deprecated and will be removed in Rails 5.1 ActiveModel::Errors#get is deprecated and will be removed in Rails 5.1.
To achieve the same use messages[:#{key}] To achieve the same use model.errors[:#{key}].
MESSAGE MESSAGE
messages[key] messages[key]
@ -129,9 +129,9 @@ module ActiveModel
# person.errors.get(:name) # => ["can't be nil"] # person.errors.get(:name) # => ["can't be nil"]
def set(key, value) def set(key, value)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#set is deprecated and will be removed in Rails 5.1 ActiveModel::Errors#set is deprecated and will be removed in Rails 5.1.
To achieve the same use messages[:#{key}] = "#{value}" Use model.errors.add(:#{key}, #{value.inspect}) instead.
MESSAGE MESSAGE
messages[key] = value messages[key] = value
@ -162,9 +162,9 @@ module ActiveModel
# person.errors[:name] # => ['must be set'] # person.errors[:name] # => ['must be set']
def []=(attribute, error) def []=(attribute, error)
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
ActiveModel::Errors#[]= is deprecated and will be removed in Rails 5.1 ActiveModel::Errors#[]= is deprecated and will be removed in Rails 5.1.
To achieve the same use messages[:#{attribute}] << "#{error}" Use model.errors.add(:#{attribute}, #{error.inspect}) instead.
MESSAGE MESSAGE
messages[attribute.to_sym] << error messages[attribute.to_sym] << error

View file

@ -15,7 +15,7 @@ module ActiveModel
# class MyValidator < ActiveModel::Validator # class MyValidator < ActiveModel::Validator
# def validate(record) # def validate(record)
# if some_complex_logic # if some_complex_logic
# record.errors.messages[:base] << "This record is invalid" # record.errors.add(:base, "This record is invalid")
# end # end
# end # end
# #

View file

@ -1078,7 +1078,7 @@ Another way to do this is using `[]=` setter
```ruby ```ruby
class Person < ActiveRecord::Base class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes def a_method_used_for_validation_purposes
errors.messages[:name] << "cannot contain the characters !@#%*()_-+=" errors.messages.add(:name, "cannot contain the characters !@#%*()_-+=")
end end
end end