提交 fdc0d934 编写于 作者: R Rafael Mendonça França

Merge branch 'rm-use-active-model-correctly'

Closes #18646
......@@ -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, scope: "helpers.label")
.translate
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, scope: "helpers.placeholder")
.translate
placeholder ||= @method_name.humanize
@options[:placeholder] = placeholder
end
end
......
module ActionView
module Helpers
module Tags # :nodoc:
class Translator # :nodoc:
def initialize(object, object_name, method_and_value, scope:)
@object_name = object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1')
@method_and_value = method_and_value
@scope = scope
@model = object.respond_to?(:to_model) ? object.to_model : object
end
def translate
translated_attribute = I18n.t("#{object_name}.#{method_and_value}", default: i18n_default, scope: scope).presence
translated_attribute || human_attribute_name
end
private
attr_reader :object_name, :method_and_value, :scope, :model
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
end
end
end
end
......@@ -54,6 +54,22 @@ def comments_attributes=(attributes); end
def tags_attributes=(attributes); end
end
class PostDelegator < Post
def to_model
PostDelegate.new
end
end
class PostDelegate < Post
def self.human_attribute_name(attribute)
"Delegate #{super}"
end
def model_name
ActiveModel::Name.new(self.class)
end
end
class Comment
extend ActiveModel::Naming
include ActiveModel::Conversion
......
......@@ -40,6 +40,9 @@ def form_for(*)
},
tag: {
value: "Tag"
},
post_delegate: {
title: 'Delegate model_name title'
}
}
}
......@@ -81,6 +84,9 @@ def form_for(*)
body: "Write body here"
}
},
post_delegate: {
title: 'Delegate model_name title'
},
tag: {
value: "Tag"
}
......@@ -117,6 +123,10 @@ def @post.to_param; '123'; end
@post.tags = []
@post.tags << Tag.new
@post_delegator = PostDelegator.new
@post_delegator.title = 'Hello World'
@car = Car.new("#000FFF")
end
......@@ -337,6 +347,22 @@ def test_label_with_block_in_erb
)
end
def test_label_with_to_model
assert_dom_equal(
%{<label for="post_delegator_title">Delegate Title</label>},
label(:post_delegator, :title)
)
end
def test_label_with_to_model_and_overriden_model_name
with_locale :label do
assert_dom_equal(
%{<label for="post_delegator_title">Delegate model_name title</label>},
label(:post_delegator, :title)
)
end
end
def test_text_field_placeholder_without_locales
with_locale :placeholder do
assert_dom_equal('<input id="post_body" name="post[body]" placeholder="Body" type="text" value="Back to the hill and over it again!" />', text_field(:post, :body, placeholder: true))
......@@ -349,12 +375,28 @@ def test_text_field_placeholder_with_locales
end
end
def test_text_field_placeholder_with_locales_and_to_model
with_locale :placeholder do
assert_dom_equal(
'<input id="post_delegator_title" name="post_delegator[title]" placeholder="Delegate model_name title" type="text" value="Hello World" />',
text_field(:post_delegator, :title, placeholder: true)
)
end
end
def test_text_field_placeholder_with_human_attribute_name
with_locale :placeholder do
assert_dom_equal('<input id="post_cost" name="post[cost]" placeholder="Total cost" type="text" />', text_field(:post, :cost, placeholder: true))
end
end
def test_text_field_placeholder_with_human_attribute_name_and_to_model
assert_dom_equal(
'<input id="post_delegator_title" name="post_delegator[title]" placeholder="Delegate Title" type="text" value="Hello World" />',
text_field(:post_delegator, :title, placeholder: true)
)
end
def test_text_field_placeholder_with_string_value
with_locale :placeholder do
assert_dom_equal('<input id="post_cost" name="post[cost]" placeholder="HOW MUCH?" type="text" />', text_field(:post, :cost, placeholder: "HOW MUCH?"))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册