aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-02-20 20:57:56 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-02-20 20:58:58 -0200
commitf55bfe726045594c78438841cdccd5843522deab (patch)
tree30bb111448809e786724bf3397a2c72af3ad9fda
parent08d9c7532c7081d6b5964b8edf802fab72224cd7 (diff)
downloadrails-f55bfe726045594c78438841cdccd5843522deab.tar.gz
rails-f55bfe726045594c78438841cdccd5843522deab.tar.bz2
rails-f55bfe726045594c78438841cdccd5843522deab.zip
Change the deprecation messages to show the preferred way to work with
ActiveModel::Errors
-rw-r--r--activemodel/README.rdoc8
-rw-r--r--activemodel/lib/active_model/errors.rb12
-rw-r--r--activemodel/lib/active_model/validator.rb2
-rw-r--r--guides/source/active_record_validations.md2
4 files changed, 12 insertions, 12 deletions
diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc
index 0985f56bfb..4920666f27 100644
--- a/activemodel/README.rdoc
+++ b/activemodel/README.rdoc
@@ -49,7 +49,7 @@ behavior out of the box:
send("#{attr}=", nil)
end
end
-
+
person = Person.new
person.clear_name
person.clear_age
@@ -132,7 +132,7 @@ behavior out of the box:
"Name"
end
end
-
+
person = Person.new
person.name = nil
person.validate!
@@ -216,10 +216,10 @@ behavior out of the box:
{Learn more}[link:classes/ActiveModel/Validations.html]
* Custom validators
-
+
class HasNameValidator < ActiveModel::Validator
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
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 166911f0fa..8334747615 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -114,9 +114,9 @@ module ActiveModel
# 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
+ 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
messages[key]
@@ -129,9 +129,9 @@ module ActiveModel
# person.errors.get(: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
+ 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
messages[key] = value
@@ -162,9 +162,9 @@ module ActiveModel
# 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
+ 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
messages[attribute.to_sym] << error
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
index 5752771d8c..1d2888a818 100644
--- a/activemodel/lib/active_model/validator.rb
+++ b/activemodel/lib/active_model/validator.rb
@@ -15,7 +15,7 @@ module ActiveModel
# class MyValidator < ActiveModel::Validator
# def validate(record)
# if some_complex_logic
- # record.errors.messages[:base] << "This record is invalid"
+ # record.errors.add(:base, "This record is invalid")
# end
# end
#
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md
index 31c5b07a05..cd7d349c82 100644
--- a/guides/source/active_record_validations.md
+++ b/guides/source/active_record_validations.md
@@ -1078,7 +1078,7 @@ Another way to do this is using `[]=` setter
```ruby
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
- errors.messages[:name] << "cannot contain the characters !@#%*()_-+="
+ errors.messages.add(:name, "cannot contain the characters !@#%*()_-+=")
end
end