diff options
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 10 | ||||
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/error.rb | 95 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 97 | ||||
-rw-r--r-- | activemodel/lib/active_model/railtie.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/format.rb | 2 |
6 files changed, 108 insertions, 100 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 415f1f679b..1a4e0b8e59 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -352,11 +352,7 @@ module ActiveModel def attribute_method_matchers_matching(method_name) attribute_method_matchers_cache.compute_if_absent(method_name) do - # Bump plain matcher to last place so that only methods that do not - # match any other pattern match the actual attribute name. - # This is currently only needed to support legacy usage. - matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1) - matchers.map { |matcher| matcher.match(method_name) }.compact + attribute_method_matchers.map { |matcher| matcher.match(method_name) }.compact end end @@ -406,10 +402,6 @@ module ActiveModel def method_name(attr_name) @method_name % attr_name end - - def plain? - prefix.empty? && suffix.empty? - end end end diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 35a587658c..aaefe00c83 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -136,7 +136,7 @@ module ActiveModel @mutations_from_database = nil end - # Clears dirty data and moves +changes+ to +previously_changed+ and + # Clears dirty data and moves +changes+ to +previous_changes+ and # +mutations_from_database+ to +mutations_before_last_save+ respectively. def changes_applied unless defined?(@attributes) diff --git a/activemodel/lib/active_model/error.rb b/activemodel/lib/active_model/error.rb index f7267fc7bf..6deab3578d 100644 --- a/activemodel/lib/active_model/error.rb +++ b/activemodel/lib/active_model/error.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/core_ext/class/attribute" + module ActiveModel # == Active \Model \Error # @@ -8,6 +10,89 @@ module ActiveModel CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict] MESSAGE_OPTIONS = [:message] + class_attribute :i18n_customize_full_message, default: false + + def self.full_message(attribute, message, base_class) # :nodoc: + return message if attribute == :base + attribute = attribute.to_s + + if i18n_customize_full_message && base_class.respond_to?(:i18n_scope) + attribute = attribute.remove(/\[\d\]/) + parts = attribute.split(".") + attribute_name = parts.pop + namespace = parts.join("/") unless parts.empty? + attributes_scope = "#{base_class.i18n_scope}.errors.models" + + if namespace + defaults = base_class.lookup_ancestors.map do |klass| + [ + :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.attributes.#{attribute_name}.format", + :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.format", + ] + end + else + defaults = base_class.lookup_ancestors.map do |klass| + [ + :"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes.#{attribute_name}.format", + :"#{attributes_scope}.#{klass.model_name.i18n_key}.format", + ] + end + end + + defaults.flatten! + else + defaults = [] + end + + defaults << :"errors.format" + defaults << "%{attribute} %{message}" + + attr_name = attribute.tr(".", "_").humanize + attr_name = base_class.human_attribute_name(attribute, default: attr_name) + + I18n.t(defaults.shift, + default: defaults, + attribute: attr_name, + message: message) + end + + def self.generate_message(attribute, type, base, options) # :nodoc: + type = options.delete(:message) if options[:message].is_a?(Symbol) + value = (attribute != :base ? base.send(:read_attribute_for_validation, attribute) : nil) + + options = { + model: base.model_name.human, + attribute: base.class.human_attribute_name(attribute), + value: value, + object: base + }.merge!(options) + + if base.class.respond_to?(:i18n_scope) + 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}" + + catch(:exception) do + translation = I18n.translate(defaults.first, options.merge(default: defaults.drop(1), throw: true)) + return translation unless translation.nil? + end unless options[:message] + else + defaults = [] + end + + defaults << :"errors.attributes.#{attribute}.#{type}" + defaults << :"errors.messages.#{type}" + + key = defaults.shift + defaults = options.delete(:message) if options[:message] + options[:default] = defaults + + I18n.translate(key, options) + end + def initialize(base, attribute, type = :invalid, **options) @base = base @attribute = attribute @@ -28,7 +113,7 @@ module ActiveModel def message case raw_type when Symbol - base.errors.generate_message(attribute, raw_type, options.except(*CALLBACKS_OPTIONS)) + self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS)) else raw_type end @@ -39,7 +124,7 @@ module ActiveModel end def full_message - base.errors.full_message(attribute, message) + self.class.full_message(attribute, message, @base.class) end # See if error matches provided +attribute+, +type+ and +options+. @@ -58,9 +143,9 @@ module ActiveModel end def strict_match?(attribute, type, **options) - return false unless match?(attribute, type, **options) + return false unless match?(attribute, type) - full_message == Error.new(@base, attribute, type, **options).full_message + options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end def ==(other) @@ -74,7 +159,7 @@ module ActiveModel protected def attributes_for_hash - [@base, @attribute, @raw_type, @options] + [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)] end end end diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index f8ea21cf47..42c004ce31 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -4,7 +4,6 @@ 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/core_ext/string/filters" -require "active_support/deprecation" require "active_model/error" require "active_model/nested_error" require "forwardable" @@ -70,11 +69,6 @@ module ActiveModel LEGACY_ATTRIBUTES = [:messages, :details].freeze - class << self - attr_accessor :i18n_customize_full_message # :nodoc: - end - self.i18n_customize_full_message = false - attr_reader :errors alias :objects :errors @@ -199,7 +193,7 @@ module ActiveModel matches.each do |error| @errors.delete(error) end - matches.map(&:message) + matches.map(&:message).presence end # When passed a symbol or a name of a method, returns an array of errors @@ -226,7 +220,7 @@ module ActiveModel # # then yield :name and "must be specified" # end def each(&block) - if block.arity == 1 + if block.arity <= 1 @errors.each(&block) else ActiveSupport::Deprecation.warn(<<~MSG) @@ -309,6 +303,16 @@ module ActiveModel hash end + def to_h + ActiveSupport::Deprecation.warn(<<~EOM) + ActiveModel::Errors#to_h is deprecated and will be removed in Rails 6.2 + Please use `ActiveModel::Errors.to_hash` instead. The values in the hash + returned by `ActiveModel::Errors.to_hash` is an array of error messages. + EOM + + to_hash.transform_values { |values| values.last } + end + def messages DeprecationHandlingMessageHash.new(self) end @@ -468,47 +472,7 @@ module ActiveModel # # person.errors.full_message(:name, 'is invalid') # => "Name is invalid" def full_message(attribute, message) - return message if attribute == :base - attribute = attribute.to_s - - if self.class.i18n_customize_full_message && @base.class.respond_to?(:i18n_scope) - attribute = attribute.remove(/\[\d\]/) - parts = attribute.split(".") - attribute_name = parts.pop - namespace = parts.join("/") unless parts.empty? - attributes_scope = "#{@base.class.i18n_scope}.errors.models" - - if namespace - defaults = @base.class.lookup_ancestors.map do |klass| - [ - :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.attributes.#{attribute_name}.format", - :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.format", - ] - end - else - defaults = @base.class.lookup_ancestors.map do |klass| - [ - :"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes.#{attribute_name}.format", - :"#{attributes_scope}.#{klass.model_name.i18n_key}.format", - ] - end - end - - defaults.flatten! - else - defaults = [] - end - - defaults << :"errors.format" - defaults << "%{attribute} %{message}" - - attr_name = attribute.tr(".", "_").humanize - attr_name = @base.class.human_attribute_name(attribute, default: attr_name) - - I18n.t(defaults.shift, - default: defaults, - attribute: attr_name, - message: message) + Error.full_message(attribute, message, @base.class) end # Translates an error message in its default scope @@ -536,40 +500,7 @@ module ActiveModel # * <tt>errors.attributes.title.blank</tt> # * <tt>errors.messages.blank</tt> def generate_message(attribute, type = :invalid, options = {}) - type = options.delete(:message) if options[:message].is_a?(Symbol) - value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil) - - options = { - model: @base.model_name.human, - attribute: @base.class.human_attribute_name(attribute), - value: value, - object: @base - }.merge!(options) - - if @base.class.respond_to?(:i18n_scope) - 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}" - - catch(:exception) do - translation = I18n.translate(defaults.first, options.merge(default: defaults.drop(1), throw: true)) - return translation unless translation.nil? - end unless options[:message] - else - defaults = [] - end - - defaults << :"errors.attributes.#{attribute}.#{type}" - defaults << :"errors.messages.#{type}" - - key = defaults.shift - defaults = options.delete(:message) if options[:message] - options[:default] = defaults - - I18n.translate(key, options) + Error.generate_message(attribute, type, @base, options) end def marshal_load(array) # :nodoc: diff --git a/activemodel/lib/active_model/railtie.rb b/activemodel/lib/active_model/railtie.rb index eb7901c7e9..65e20b9791 100644 --- a/activemodel/lib/active_model/railtie.rb +++ b/activemodel/lib/active_model/railtie.rb @@ -14,7 +14,7 @@ module ActiveModel end initializer "active_model.i18n_customize_full_message" do - ActiveModel::Errors.i18n_customize_full_message = config.active_model.delete(:i18n_customize_full_message) || false + ActiveModel::Error.i18n_customize_full_message = config.active_model.delete(:i18n_customize_full_message) || false end end end diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index bea57415b0..a473440b4e 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -6,7 +6,7 @@ module ActiveModel def validate_each(record, attribute, value) if options[:with] regexp = option_call(record, :with) - record_error(record, attribute, :with, value) if value.to_s !~ regexp + record_error(record, attribute, :with, value) unless regexp.match?(value.to_s) elsif options[:without] regexp = option_call(record, :without) record_error(record, attribute, :without, value) if regexp.match?(value.to_s) |