-
Notifications
You must be signed in to change notification settings - Fork 21.8k
Allow to override the full_message error format #32956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to override the full_message error format #32956
Conversation
daf2453
to
4b2b291
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good patch. I personally found myself having to find way to workaround this a few times. It'd be nice to be able to do this in i18n config.
wdyt @rafaelfranca @matthewd ?
@@ -7,8 +7,15 @@ module ActiveModel | |||
class Railtie < Rails::Railtie # :nodoc: | |||
config.eager_load_namespaces << ActiveModel | |||
|
|||
config.active_model = ActiveSupport::OrderedOptions.new | |||
config.app_generators.orm :active_model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird. Why did you need to add this? Active Model is not a ORM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
Looking the active_record and http://api.rubyonrails.org/classes/Rails/Railtie.html as examples, I though it was needed.
@@ -12,13 +12,17 @@ def setup | |||
I18n.load_path.clear | |||
I18n.backend = I18n::Backend::Simple.new | |||
I18n.backend.store_translations("en", errors: { messages: { custom: nil } }) | |||
|
|||
@original_i18n_full_message = ActiveModel::Errors.i18n_full_message | |||
ActiveModel::Errors.i18n_full_message = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document this option in the configuring guide https://github.com/rails/rails/blob/master/guides/source/configuring.md.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
4b2b291
to
32513c4
Compare
class << self | ||
attr_accessor :i18n_full_message # :nodoc: | ||
end | ||
self.i18n_full_message = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it added intentionally?
It seems extra since we set default value of i18n_full_message
in activemodel/lib/active_model/railtie.rb
.
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 56404a036c..7400bb5a6c 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -65,7 +65,6 @@ class Errors
class << self
attr_accessor :i18n_full_message # :nodoc:
end
- self.i18n_full_message = false
attr_reader :messages, :details
diff --git a/activemodel/lib/active_model/railtie.rb b/activemodel/lib/active_model/railtie.rb
index 0ed70bd473..ed70d65c8e 100644
--- a/activemodel/lib/active_model/railtie.rb
+++ b/activemodel/lib/active_model/railtie.rb
@@ -8,13 +8,14 @@ class Railtie < Rails::Railtie # :nodoc:
config.eager_load_namespaces << ActiveModel
config.active_model = ActiveSupport::OrderedOptions.new
+ config.active_model.i18n_full_message = false
initializer "active_model.secure_password" do
ActiveModel::SecurePassword.min_cost = Rails.env.test?
end
initializer "active_model.i18n_full_message" do
- ActiveModel::Errors.i18n_full_message = config.active_model.delete(:i18n_full_message) || false
+ ActiveModel::Errors.i18n_full_message = config.active_model.delete(:i18n_full_message)
end
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is intentional. We want a default value if the railtie is not loaded, for example if you use Active Model without Rails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense then. Thanks for explanation.
What do you think about making ActiveModel::Errors.i18n_full_message
as public API in order to allow users use this new feature if use Active Model without Rails?
- Fix indentation. - Add a missing dot to the end of the sentence. Related to rails#32956
Add mention about default value of `config.active_model.i18n_full_message`.
Add changelog for #32956 [ci skip]
…age` - I feel `i18n_customize_full_messages` explains the meaning of the config better. - Followup of rails#32956
Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
…`false` to `true` Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
…` to `true` Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover.
…` to `true` Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, by setting the default value to `false`, this feature was difficult to discover. `config.active_model.i18n_customize_full_message` defaults to `true` for `config.load_defaults 7.2` and above.
…` to `true` Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, because the default value was set to `false`, this feature was difficult to discover. `config.active_model.i18n_customize_full_message` defaults to `true` for `config.load_defaults 7.2` and above.
…` to `true` Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, because the default value was set to `false`, this feature was difficult to discover. `config.active_model.i18n_customize_full_message` defaults to `true` for `config.load_defaults 7.2` and above.
…` to `true` Follow-up to rails#32956 and rails#35789 The introduction of this configuration in rails#32956 stated that: > The goal of this PR is to make it easier for an app to transition from a #{attribute} #{message} to a #{message}, full_message error format. However, because the default value was set to `false`, this feature was difficult to discover. `config.active_model.i18n_customize_full_message` defaults to `true` for `config.load_defaults 7.2` and above.
Summary
The goal of this PR is to make it easier for an app to transition from a
#{attribute} #{message}
to a#{message}
,full_message
error format.full_message
formats error messages with a#{attribute} #{message}
format which generates error messages as “name cannot be nil”.It is possible to override the format with
:"errors.format"
but only language wide. https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L371 allows a language to define a custom format, but changing it forces to extract all messages, including the implicit ones as:blank
The
#{attribute} #{message}
format prevents languages to move the attribute name to somewhere else within the message, for example, in some cases it can be preferred to have error messages as“The person's name cannot be blank”
, so changing the format to#{message}
and including the attribute name in the message itself can be preferable.To make such transition easier, this PR allows to override
errors.format
at the attribute or model level, so an app can use either:How
This is based on
active_model/translation.rb#human_attribute_name
https://github.com/rails/rails/blob/master/activemodel/lib/active_model/translation.rb#L44
And
active_model/errors.rb#generate_message
https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L401
@base.class.respond_to?(:i18n_scope)
is needed for cases whereActiveModel::Errors
is used without including theTranslation
moduleBenchmark
Results
The result with the variance between runs