Merge pull request #18647 from mcls/placeholderable-to-model

The `model_name` method should be called on `to_model`
This commit is contained in:
Rafael Mendonça França 2015-02-05 15:16:57 -02:00
commit ed85348877
4 changed files with 47 additions and 28 deletions

@ -5,6 +5,7 @@ module Tags #:nodoc:
eager_autoload do
autoload :Base
autoload :Translator
autoload :CheckBox
autoload :CollectionCheckBoxes
autoload :CollectionRadioButtons

@ -15,20 +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)
key = object.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)
end
content ||= Translator
.new(object, @object_name, method_and_value, "helpers.label")
.call
content ||= @method_name.humanize
content

@ -7,24 +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)
key = object.class.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)
end
placeholder ||= Tags::Translator
.new(object, @object_name, method_and_value, "helpers.placeholder")
.call
placeholder ||= @method_name.humanize
@options[:placeholder] = placeholder
end
end

@ -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