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') 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') 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') 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') 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 From 60da337a004faabf105639fd1b7f52fae0fc0f39 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Fri, 22 Aug 2014 15:19:57 -0400 Subject: Clarify that unknown keys will become HTML attrs Docs for all the other form tag helpers in this file already clarify this. [ci skip] --- actionview/lib/action_view/helpers/form_tag_helper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index b18f578183..050c27e239 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -39,6 +39,7 @@ module ActionView # * :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the # submit behavior. By default this behavior is an ajax submit. # * :enforce_utf8 - If set to false, a hidden input with name utf8 is not output. + # * Any other key creates standard HTML attributes for the tag. # # ==== Examples # form_tag('/posts') -- cgit v1.2.3 From 0387591c0528307cc664ad75697e683f2a3d9e80 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Fri, 22 Aug 2014 15:22:59 -0400 Subject: Remove erroneous form_tag option docs This is true of the first param (url_for_options), not of the second options param which is being documented here. [ci skip] --- actionview/lib/action_view/helpers/form_tag_helper.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 050c27e239..7d1cdc5a68 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -35,7 +35,6 @@ module ActionView # This is helpful when you're fragment-caching the form. Remote forms get the # authenticity token from the meta tag, so embedding is unnecessary unless you # support browsers without JavaScript. - # * A list of parameters to feed to the URL the form will be posted to. # * :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the # submit behavior. By default this behavior is an ajax submit. # * :enforce_utf8 - If set to false, a hidden input with name utf8 is not output. -- cgit v1.2.3 From 7a2622bcbd1937dd2862d27fa18258198de7a153 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 27 Aug 2014 17:58:59 -0700 Subject: remove dead code the ERB has already been compiled to Ruby code by the time we're calling module_eval. Nothing that module eval raises will be caught by a blank `rescue`, so I think we can remove this --- actionview/lib/action_view/template.rb | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 9d39d02a37..07522778b7 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -293,18 +293,8 @@ module ActionView raise WrongEncodingError.new(@source, Encoding.default_internal) end - begin - mod.module_eval(source, identifier, 0) - ObjectSpace.define_finalizer(self, Finalizer[method_name, mod]) - rescue => e # errors from template code - if logger = (view && view.logger) - logger.debug "ERROR: compiling #{method_name} RAISED #{e}" - logger.debug "Function body: #{source}" - logger.debug "Backtrace: #{e.backtrace.join("\n")}" - end - - raise ActionView::Template::Error.new(self, e) - end + mod.module_eval(source, identifier, 0) + ObjectSpace.define_finalizer(self, Finalizer[method_name, mod]) end def handle_render_error(view, e) #:nodoc: -- cgit v1.2.3 From cab85e9926c62094d1bada4437b6d1b6bc51e5e8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 27 Aug 2014 17:59:34 -0700 Subject: remove useless parameter --- actionview/lib/action_view/template.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionview/lib') diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 07522778b7..eb0f2e8a57 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -242,7 +242,7 @@ module ActionView end instrument("!compile_template") do - compile(view, mod) + compile(mod) end # Just discard the source if we have a virtual path. This @@ -264,7 +264,7 @@ module ActionView # encode the source into Encoding.default_internal. # In general, this means that templates will be UTF-8 inside of Rails, # regardless of the original source encoding. - def compile(view, mod) #:nodoc: + def compile(mod) #:nodoc: encode! method_name = self.method_name code = @handler.call(self) -- cgit v1.2.3