diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-18 18:58:28 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-18 18:58:28 -0200 |
commit | 3c750c4c6c4d3c2e67865bc43a99010fa8b6c7a6 (patch) | |
tree | a5deb56efdc54e870c54d1ee6753b4de9cfe4022 /activemodel/lib/active_model | |
parent | c407693a41608eeea1354b71357a5283152d0f9b (diff) | |
parent | 6ec8ba16d85d5feaccb993c9756c1edcbbf0ba13 (diff) | |
download | rails-3c750c4c6c4d3c2e67865bc43a99010fa8b6c7a6.tar.gz rails-3c750c4c6c4d3c2e67865bc43a99010fa8b6c7a6.tar.bz2 rails-3c750c4c6c4d3c2e67865bc43a99010fa8b6c7a6.zip |
Merge pull request #18634 from morgoth/deprecate-some-errors-methods
Deprecate `ActiveModel::Errors` `get`, `set` and `[]=` methods.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index a809c72ccd..b1d090b830 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -3,6 +3,7 @@ require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/string/inflections' require 'active_support/core_ext/object/deep_dup' +require 'active_support/deprecation' module ActiveModel # == Active \Model \Errors @@ -72,7 +73,7 @@ module ActiveModel # end def initialize(base) @base = base - @messages = {} + @messages = Hash.new { |messages, attribute| messages[attribute] = [] } @details = Hash.new { |details, attribute| details[attribute] = [] } end @@ -110,8 +111,14 @@ module ActiveModel # # person.errors.messages # => {:name=>["cannot be nil"]} # person.errors.get(:name) # => ["cannot be nil"] - # person.errors.get(:age) # => 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 messages[:#{key}] + MESSAGE + messages[key] end @@ -121,6 +128,12 @@ module ActiveModel # person.errors.set(:name, ["can't be nil"]) # 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 + + To achieve the same use messages[:#{key}] = "#{value}" + MESSAGE + messages[key] = value end @@ -128,7 +141,7 @@ module ActiveModel # # person.errors.get(:name) # => ["cannot be nil"] # person.errors.delete(:name) # => ["cannot be nil"] - # person.errors.get(:name) # => nil + # person.errors.get(:name) # => [] def delete(key) messages.delete(key) details.delete(key) @@ -140,7 +153,7 @@ module ActiveModel # person.errors[:name] # => ["cannot be nil"] # person.errors['name'] # => ["cannot be nil"] def [](attribute) - get(attribute.to_sym) || set(attribute.to_sym, []) + messages[attribute.to_sym] end # Adds to the supplied attribute the supplied error message. @@ -148,7 +161,13 @@ module ActiveModel # person.errors[:name] = "must be set" # person.errors[:name] # => ['must be set'] def []=(attribute, error) - self[attribute] << error + ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) + ActiveModel::Errors#[]= is deprecated and will be removed in Rails 5.1 + + To achieve the same use messages[:#{attribute}] << "#{error}" + MESSAGE + + messages[attribute.to_sym] << error end # Iterates through each error key, value pair in the error messages hash. @@ -167,7 +186,7 @@ module ActiveModel # end def each messages.each_key do |attribute| - self[attribute].each { |error| yield attribute, error } + messages[attribute].each { |error| yield attribute, error } end end @@ -317,8 +336,8 @@ module ActiveModel raise exception, full_message(attribute, message) end - details[attribute.to_sym] << detail - self[attribute] << message + details[attribute.to_sym] << detail + messages[attribute.to_sym] << message end # Will add an error message to each of the attributes in +attributes+ @@ -384,7 +403,7 @@ module ActiveModel # person.errors.full_messages_for(:name) # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"] def full_messages_for(attribute) - (get(attribute) || []).map { |message| full_message(attribute, message) } + messages[attribute].map { |message| full_message(attribute, message) } end # Returns a full message for a given attribute. |