From ee3dd2f0ff1820804888cb3a7475dd593deb0212 Mon Sep 17 00:00:00 2001 From: Maarten Claes Date: Thu, 22 Jan 2015 21:38:29 +0100 Subject: [PATCH 1/2] Convert with `to_model` before calling ActiveModel methods on `object` --- actionview/lib/action_view/helpers/tags/label.rb | 10 +++++++--- .../lib/action_view/helpers/tags/placeholderable.rb | 12 +++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb index 08a23e497e..c44cc57468 100644 --- a/actionview/lib/action_view/helpers/tags/label.rb +++ b/actionview/lib/action_view/helpers/tags/label.rb @@ -18,15 +18,19 @@ def translation @object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1') if object.respond_to?(:to_model) - key = object.model_name.i18n_key + model = object.to_model + end + + if model + key = model.model_name.i18n_key i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] end i18n_default ||= "" content = I18n.t("#{@object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence - content ||= if object && object.class.respond_to?(:human_attribute_name) - object.class.human_attribute_name(method_and_value) + content ||= if model && model.class.respond_to?(:human_attribute_name) + model.class.human_attribute_name(method_and_value) end content ||= @method_name.humanize diff --git a/actionview/lib/action_view/helpers/tags/placeholderable.rb b/actionview/lib/action_view/helpers/tags/placeholderable.rb index ae67bc13af..690ba93ab5 100644 --- a/actionview/lib/action_view/helpers/tags/placeholderable.rb +++ b/actionview/lib/action_view/helpers/tags/placeholderable.rb @@ -12,17 +12,19 @@ def initialize(*) method_and_value = tag_value.is_a?(TrueClass) ? @method_name : "#{@method_name}.#{tag_value}" if object.respond_to?(:to_model) - key = object.class.model_name.i18n_key + model = object.to_model + end + + if model + key = model.model_name.i18n_key i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] end i18n_default ||= "" placeholder ||= I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.placeholder").presence - - placeholder ||= if object && object.class.respond_to?(:human_attribute_name) - object.class.human_attribute_name(method_and_value) + placeholder ||= if model && model.class.respond_to?(:human_attribute_name) + model.class.human_attribute_name(method_and_value) end - placeholder ||= @method_name.humanize @options[:placeholder] = placeholder From c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d Mon Sep 17 00:00:00 2001 From: Maarten Claes Date: Thu, 22 Jan 2015 22:59:42 +0100 Subject: [PATCH 2/2] Reduce duplication when generating translations --- actionview/lib/action_view/helpers/tags.rb | 1 + .../lib/action_view/helpers/tags/label.rb | 20 ++-------- .../helpers/tags/placeholderable.rb | 20 ++-------- .../action_view/helpers/tags/translator.rb | 40 +++++++++++++++++++ 4 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 actionview/lib/action_view/helpers/tags/translator.rb diff --git a/actionview/lib/action_view/helpers/tags.rb b/actionview/lib/action_view/helpers/tags.rb index 45c75d10c0..a4f6eb0150 100644 --- a/actionview/lib/action_view/helpers/tags.rb +++ b/actionview/lib/action_view/helpers/tags.rb @@ -5,6 +5,7 @@ module Tags #:nodoc: eager_autoload do autoload :Base + autoload :Translator autoload :CheckBox autoload :CollectionCheckBoxes autoload :CollectionRadioButtons diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb index c44cc57468..25f7cafcb8 100644 --- a/actionview/lib/action_view/helpers/tags/label.rb +++ b/actionview/lib/action_view/helpers/tags/label.rb @@ -15,24 +15,10 @@ def initialize(template_object, object_name, method_name, object, tag_value) def translation method_and_value = @tag_value.present? ? "#{@method_name}.#{@tag_value}" : @method_name - @object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1') - - if object.respond_to?(:to_model) - model = object.to_model - end - - if model - key = model.model_name.i18n_key - i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] - end - - i18n_default ||= "" - content = I18n.t("#{@object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence - - content ||= if model && model.class.respond_to?(:human_attribute_name) - model.class.human_attribute_name(method_and_value) - end + content ||= Translator + .new(object, @object_name, method_and_value, "helpers.label") + .call content ||= @method_name.humanize content diff --git a/actionview/lib/action_view/helpers/tags/placeholderable.rb b/actionview/lib/action_view/helpers/tags/placeholderable.rb index 690ba93ab5..0b15a11308 100644 --- a/actionview/lib/action_view/helpers/tags/placeholderable.rb +++ b/actionview/lib/action_view/helpers/tags/placeholderable.rb @@ -7,26 +7,12 @@ def initialize(*) if tag_value = @options[:placeholder] placeholder = tag_value if tag_value.is_a?(String) - - object_name = @object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1') method_and_value = tag_value.is_a?(TrueClass) ? @method_name : "#{@method_name}.#{tag_value}" - if object.respond_to?(:to_model) - model = object.to_model - end - - if model - key = model.model_name.i18n_key - i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] - end - - i18n_default ||= "" - placeholder ||= I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.placeholder").presence - placeholder ||= if model && model.class.respond_to?(:human_attribute_name) - model.class.human_attribute_name(method_and_value) - end + placeholder ||= Tags::Translator + .new(object, @object_name, method_and_value, "helpers.placeholder") + .call placeholder ||= @method_name.humanize - @options[:placeholder] = placeholder end end diff --git a/actionview/lib/action_view/helpers/tags/translator.rb b/actionview/lib/action_view/helpers/tags/translator.rb new file mode 100644 index 0000000000..b626dfab3b --- /dev/null +++ b/actionview/lib/action_view/helpers/tags/translator.rb @@ -0,0 +1,40 @@ +module ActionView + module Helpers + module Tags # :nodoc: + class Translator # :nodoc: + attr_reader :object, :object_name, :method_and_value, :i18n_scope + + def initialize(object, object_name, method_and_value, i18n_scope) + @object = object + @object_name = object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1') + @method_and_value = method_and_value + @i18n_scope = i18n_scope + end + + def call + placeholder ||= I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => i18n_scope).presence + placeholder || human_attribute_name + end + + def i18n_default + if model + key = model.model_name.i18n_key + ["#{key}.#{method_and_value}".to_sym, ""] + else + "" + end + end + + def human_attribute_name + if model && model.class.respond_to?(:human_attribute_name) + model.class.human_attribute_name(method_and_value) + end + end + + def model + @model ||= object.to_model if object.respond_to?(:to_model) + end + end + end + end +end