aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb45
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb13
2 files changed, 40 insertions, 18 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 6e26ae6c29..557f8454be 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -573,8 +573,19 @@ module ActionView
# label(:post, :privacy, "Public Post", :value => "public")
# # => <label for="post_privacy_public">Public Post</label>
#
- def label(object_name, method, text = nil, options = {})
- InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options)
+ # label(:post, :terms) do
+ # 'Accept <a href="/terms">Terms</a>.'
+ # end
+ def label(object_name, method, content_or_options_with_block = nil, options = nil, &block)
+ if block_given?
+ options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
+ text = nil
+ else
+ text = content_or_options_with_block
+ end
+
+ options ||= {}
+ InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options, &block)
end
# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
@@ -823,7 +834,7 @@ module ActionView
module InstanceTagMethods #:nodoc:
extend ActiveSupport::Concern
- include Helpers::TagHelper, Helpers::FormTagHelper
+ include Helpers::CaptureHelper, Context, Helpers::TagHelper, Helpers::FormTagHelper
attr_reader :method_name, :object_name
@@ -844,7 +855,7 @@ module ActionView
end
end
- def to_label_tag(text = nil, options = {})
+ def to_label_tag(text = nil, options = {}, &block)
options = options.stringify_keys
tag_value = options.delete("value")
name_and_id = options.dup
@@ -853,19 +864,23 @@ module ActionView
options.delete("index")
options["for"] ||= name_and_id["id"]
- content = if text.blank?
- I18n.t("helpers.label.#{object_name}.#{method_name}", :default => "").presence
+ if block_given?
+ label_tag(name_and_id["id"], options, &block)
else
- text.to_s
- end
+ content = if text.blank?
+ I18n.t("helpers.label.#{object_name}.#{method_name}", :default => "").presence
+ else
+ text.to_s
+ end
- content ||= if object && object.class.respond_to?(:human_attribute_name)
- object.class.human_attribute_name(method_name)
- end
+ content ||= if object && object.class.respond_to?(:human_attribute_name)
+ object.class.human_attribute_name(method_name)
+ end
- content ||= method_name.humanize
+ content ||= method_name.humanize
- label_tag(name_and_id["id"], content, options)
+ label_tag(name_and_id["id"], content, options)
+ end
end
def to_input_field_tag(field_type, options = {})
@@ -1137,8 +1152,8 @@ module ActionView
@template.fields_for(name, *args, &block)
end
- def label(method, text = nil, options = {})
- @template.label(@object_name, method, text, objectify_options(options))
+ def label(method, text = nil, options = {}, &block)
+ @template.label(@object_name, method, text, objectify_options(options), &block)
end
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index b840f77fb5..9d15805d46 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -142,7 +142,7 @@ module ActionView
tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
end
- # Creates a label field
+ # Creates a label element. Accepts a block.
#
# ==== Options
# * Creates standard HTML attributes for the tag.
@@ -156,8 +156,15 @@ module ActionView
#
# label_tag 'name', nil, :class => 'small_label'
# # => <label for="name" class="small_label">Name</label>
- def label_tag(name, text = nil, options = {})
- content_tag :label, text || name.to_s.humanize, { "for" => sanitize_to_id(name) }.update(options.stringify_keys)
+ def label_tag(name = nil, content_or_options_with_block = nil, options = nil, &block)
+ if block_given?
+ options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
+ end
+
+ options ||= {}
+ options.stringify_keys!
+ options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
+ content_tag :label, content_or_options_with_block || name.to_s.humanize, options, &block
end
# Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or