diff options
author | Viktar Basharymau <viktar.basharymau@gmail.com> | 2017-08-18 14:35:43 +0300 |
---|---|---|
committer | Viktar Basharymau <viktar.basharymau@gmail.com> | 2017-08-18 17:36:59 +0300 |
commit | 53c98196e6cf809f5fdb69396e617fa6509085a3 (patch) | |
tree | 7b476d86a49c6e178ec4466047be085a29700500 /activemodel/lib | |
parent | e183dc195a965371b1edc3ea18c54910758c32e3 (diff) | |
download | rails-53c98196e6cf809f5fdb69396e617fa6509085a3.tar.gz rails-53c98196e6cf809f5fdb69396e617fa6509085a3.tar.bz2 rails-53c98196e6cf809f5fdb69396e617fa6509085a3.zip |
Simplify ActiveModel::Errors#generate_message
Besides making the code easier to read, this commit
also makes it faster:
* We don't eval `@base.class.respond_to?(:i18n_scope)` twice
* We only eval `@base.class.i18n_scope` once
* We don't call `flatten!` because it's not needed anymore
* We don't call `compact` because all elements are Symbols
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index c24ae4e430..971bdd08b1 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -398,21 +398,19 @@ module ActiveModel type = options.delete(:message) if options[:message].is_a?(Symbol) if @base.class.respond_to?(:i18n_scope) - defaults = @base.class.lookup_ancestors.map do |klass| - [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}", - :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ] + i18n_scope = @base.class.i18n_scope.to_s + defaults = @base.class.lookup_ancestors.flat_map do |klass| + [ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}", + :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ] end + defaults << :"#{i18n_scope}.errors.messages.#{type}" else defaults = [] end - defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}" if @base.class.respond_to?(:i18n_scope) defaults << :"errors.attributes.#{attribute}.#{type}" defaults << :"errors.messages.#{type}" - defaults.compact! - defaults.flatten! - key = defaults.shift defaults = options.delete(:message) if options[:message] value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil) |