aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authorMaarten Claes <maartencls@gmail.com>2015-01-22 22:59:42 +0100
committerMaarten Claes <maartencls@gmail.com>2015-01-22 23:02:48 +0100
commitc946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d (patch)
tree5a84286d94acabd898b6ebd9916424a31ce08ee5 /actionview
parentee3dd2f0ff1820804888cb3a7475dd593deb0212 (diff)
downloadrails-c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d.tar.gz
rails-c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d.tar.bz2
rails-c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d.zip
Reduce duplication when generating translations
Diffstat (limited to 'actionview')
-rw-r--r--actionview/lib/action_view/helpers/tags.rb1
-rw-r--r--actionview/lib/action_view/helpers/tags/label.rb20
-rw-r--r--actionview/lib/action_view/helpers/tags/placeholderable.rb20
-rw-r--r--actionview/lib/action_view/helpers/tags/translator.rb40
4 files changed, 47 insertions, 34 deletions
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 ActionView
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 @@ module ActionView
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 @@ module ActionView
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