aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorStephen Celis <stephen@stephencelis.com>2010-04-20 23:16:18 -0500
committerJosé Valim <jose.valim@gmail.com>2010-05-15 08:59:08 +0200
commit6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06 (patch)
treef3c036e8d4cfe261984bdba8d14cae2612dc9fd0 /actionpack/lib/action_view/helpers/form_helper.rb
parent35a114a8941cb22d29a536f1215a23a8cf7c4756 (diff)
downloadrails-6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06.tar.gz
rails-6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06.tar.bz2
rails-6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06.zip
Let label helpers accept blocks.
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb45
1 files changed, 30 insertions, 15 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")