From ce8e4a43b353b7e36a1c9f98561dc7ed23e16c93 Mon Sep 17 00:00:00 2001 From: Alex Robbin Date: Tue, 12 Aug 2014 23:43:24 -0400 Subject: provide a builder for form labels to customize wrapping around I18n content --- actionview/lib/action_view/helpers/tags/label.rb | 68 ++++++++++++++++-------- 1 file changed, 46 insertions(+), 22 deletions(-) (limited to 'actionview/lib/action_view/helpers/tags') diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb index 39b2f48c39..08a23e497e 100644 --- a/actionview/lib/action_view/helpers/tags/label.rb +++ b/actionview/lib/action_view/helpers/tags/label.rb @@ -2,6 +2,39 @@ module ActionView module Helpers module Tags # :nodoc: class Label < Base # :nodoc: + class LabelBuilder # :nodoc: + attr_reader :object + + def initialize(template_object, object_name, method_name, object, tag_value) + @template_object = template_object + @object_name = object_name + @method_name = method_name + @object = object + @tag_value = tag_value + end + + 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 ||= @method_name.humanize + + content + end + end + def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil) options ||= {} @@ -32,33 +65,24 @@ module ActionView options.delete("namespace") options["for"] = name_and_id["id"] unless options.key?("for") - if block_given? - content = @template_object.capture(&block) - else - method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name - content = if @content.blank? - @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 ||= "" - I18n.t("#{@object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence - else - @content.to_s - end - - content ||= if object && object.class.respond_to?(:human_attribute_name) - object.class.human_attribute_name(method_and_value) - end + builder = LabelBuilder.new(@template_object, @object_name, @method_name, @object, tag_value) - content ||= @method_name.humanize + content = if block_given? + @template_object.capture(builder, &block) + elsif @content.present? + @content.to_s + else + render_component(builder) end label_tag(name_and_id["id"], content, options) end + + private + + def render_component(builder) + builder.translation + end end end end -- cgit v1.2.3 From 81b14b7c5c32ef1b21227f7f2548685801930201 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 21 Aug 2014 22:07:33 -0700 Subject: Revert "Add I18n support for `:placeholder` HTML option is passed to form fields" --- .../action_view/helpers/tags/placeholderable.rb | 32 ---------------------- .../lib/action_view/helpers/tags/text_area.rb | 4 --- .../lib/action_view/helpers/tags/text_field.rb | 4 --- 3 files changed, 40 deletions(-) delete mode 100644 actionview/lib/action_view/helpers/tags/placeholderable.rb (limited to 'actionview/lib/action_view/helpers/tags') diff --git a/actionview/lib/action_view/helpers/tags/placeholderable.rb b/actionview/lib/action_view/helpers/tags/placeholderable.rb deleted file mode 100644 index 313aa725c9..0000000000 --- a/actionview/lib/action_view/helpers/tags/placeholderable.rb +++ /dev/null @@ -1,32 +0,0 @@ -module ActionView - module Helpers - module Tags # :nodoc: - module Placeholderable # :nodoc: - def initialize(*) - super - - if tag_value = @options[:placeholder] - 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 ||= @method_name.humanize - - @options[:placeholder] = placeholder - end - end - end - end - end -end diff --git a/actionview/lib/action_view/helpers/tags/text_area.rb b/actionview/lib/action_view/helpers/tags/text_area.rb index 69038c1498..9ee83ee7c2 100644 --- a/actionview/lib/action_view/helpers/tags/text_area.rb +++ b/actionview/lib/action_view/helpers/tags/text_area.rb @@ -1,11 +1,7 @@ -require 'action_view/helpers/tags/placeholderable' - module ActionView module Helpers module Tags # :nodoc: class TextArea < Base # :nodoc: - include Placeholderable - def render options = @options.stringify_keys add_default_name_and_id(options) diff --git a/actionview/lib/action_view/helpers/tags/text_field.rb b/actionview/lib/action_view/helpers/tags/text_field.rb index 5c576a20ca..e0b80d81c2 100644 --- a/actionview/lib/action_view/helpers/tags/text_field.rb +++ b/actionview/lib/action_view/helpers/tags/text_field.rb @@ -1,11 +1,7 @@ -require 'action_view/helpers/tags/placeholderable' - module ActionView module Helpers module Tags # :nodoc: class TextField < Base # :nodoc: - include Placeholderable - def render options = @options.stringify_keys options["size"] = options["maxlength"] unless options.key?("size") -- cgit v1.2.3 From fea7fe1fd2c7dbd98cf8707f9ecd17d010777fc1 Mon Sep 17 00:00:00 2001 From: Alex Robbin Date: Sat, 9 Aug 2014 00:10:54 -0400 Subject: add I18n support for `:placeholder` HTML option is passed to form fields --- .../action_view/helpers/tags/placeholderable.rb | 32 ++++++++++++++++++++++ .../lib/action_view/helpers/tags/text_area.rb | 4 +++ .../lib/action_view/helpers/tags/text_field.rb | 4 +++ 3 files changed, 40 insertions(+) create mode 100644 actionview/lib/action_view/helpers/tags/placeholderable.rb (limited to 'actionview/lib/action_view/helpers/tags') diff --git a/actionview/lib/action_view/helpers/tags/placeholderable.rb b/actionview/lib/action_view/helpers/tags/placeholderable.rb new file mode 100644 index 0000000000..313aa725c9 --- /dev/null +++ b/actionview/lib/action_view/helpers/tags/placeholderable.rb @@ -0,0 +1,32 @@ +module ActionView + module Helpers + module Tags # :nodoc: + module Placeholderable # :nodoc: + def initialize(*) + super + + if tag_value = @options[:placeholder] + 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 ||= @method_name.humanize + + @options[:placeholder] = placeholder + end + end + end + end + end +end diff --git a/actionview/lib/action_view/helpers/tags/text_area.rb b/actionview/lib/action_view/helpers/tags/text_area.rb index 9ee83ee7c2..69038c1498 100644 --- a/actionview/lib/action_view/helpers/tags/text_area.rb +++ b/actionview/lib/action_view/helpers/tags/text_area.rb @@ -1,7 +1,11 @@ +require 'action_view/helpers/tags/placeholderable' + module ActionView module Helpers module Tags # :nodoc: class TextArea < Base # :nodoc: + include Placeholderable + def render options = @options.stringify_keys add_default_name_and_id(options) diff --git a/actionview/lib/action_view/helpers/tags/text_field.rb b/actionview/lib/action_view/helpers/tags/text_field.rb index e0b80d81c2..5c576a20ca 100644 --- a/actionview/lib/action_view/helpers/tags/text_field.rb +++ b/actionview/lib/action_view/helpers/tags/text_field.rb @@ -1,7 +1,11 @@ +require 'action_view/helpers/tags/placeholderable' + module ActionView module Helpers module Tags # :nodoc: class TextField < Base # :nodoc: + include Placeholderable + def render options = @options.stringify_keys options["size"] = options["maxlength"] unless options.key?("size") -- cgit v1.2.3 From f99e62e2d9e010102788f1e728f54b03f65115c5 Mon Sep 17 00:00:00 2001 From: Alex Robbin Date: Fri, 22 Aug 2014 09:54:54 -0400 Subject: just use the placeholder tag value if it is passed as a String --- actionview/lib/action_view/helpers/tags/placeholderable.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'actionview/lib/action_view/helpers/tags') diff --git a/actionview/lib/action_view/helpers/tags/placeholderable.rb b/actionview/lib/action_view/helpers/tags/placeholderable.rb index 313aa725c9..ae67bc13af 100644 --- a/actionview/lib/action_view/helpers/tags/placeholderable.rb +++ b/actionview/lib/action_view/helpers/tags/placeholderable.rb @@ -6,6 +6,8 @@ module ActionView super 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}" @@ -15,7 +17,7 @@ module ActionView end i18n_default ||= "" - placeholder = I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.placeholder").presence + 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) -- cgit v1.2.3