Class: ActionView::Helpers::Tags::Label

Inherits:
Base show all
Defined in:
actionview/lib/action_view/helpers/tags/label.rb

Overview

:nodoc:

Constant Summary

Constants included from ActionView::Helpers::TagHelper

ActionView::Helpers::TagHelper::BOOLEAN_ATTRIBUTES, ActionView::Helpers::TagHelper::PRE_CONTENT_STRINGS

Constants included from UrlHelper

UrlHelper::BUTTON_TAG_METHOD_VERBS

Instance Attribute Summary

Attributes inherited from Base

#object

Instance Method Summary collapse

Methods included from FormOptionsHelper

#collection_check_boxes, #collection_radio_buttons, #collection_select, #grouped_collection_select, #grouped_options_for_select, #option_groups_from_collection_for_select, #options_for_select, #options_from_collection_for_select, #select, #time_zone_options_for_select, #time_zone_select

Methods included from ActionView::Helpers::TextHelper

#concat, #current_cycle, #cycle, #excerpt, #highlight, #pluralize, #reset_cycle, #safe_concat, #simple_format, #truncate, #word_wrap

Methods included from ActiveSupport::Concern

#append_features, extended, #included

Methods included from OutputSafetyHelper

#raw, #safe_join

Methods included from ActionView::Helpers::TagHelper

#cdata_section, #content_tag, #escape_once, #tag

Methods included from CaptureHelper

#capture, #content_for, #content_for?, #flush_output_buffer, #provide, #with_output_buffer

Methods included from SanitizeHelper

#sanitize, #sanitize_css, #strip_links, #strip_tags

Methods included from ActiveModelInstanceTag

#content_tag, #error_message, #error_wrapping, #object, #tag

Methods included from FormTagHelper

#button_tag, #check_box_tag, #color_field_tag, #date_field_tag, #datetime_field_tag, #datetime_local_field_tag, #email_field_tag, #field_set_tag, #file_field_tag, #form_tag, #hidden_field_tag, #image_submit_tag, #label_tag, #month_field_tag, #number_field_tag, #password_field_tag, #radio_button_tag, #range_field_tag, #search_field_tag, #select_tag, #submit_tag, #telephone_field_tag, #text_area_tag, #text_field_tag, #time_field_tag, #url_field_tag, #utf8_enforcer_tag, #week_field_tag

Methods included from UrlHelper

#button_to, #current_page?, #link_to, #link_to_if, #link_to_unless, #link_to_unless_current, #mail_to, #url_for

Constructor Details

#initialize(object_name, method_name, template_object, content_or_options = nil, options = nil) ⇒ Label

Returns a new instance of Label.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'actionview/lib/action_view/helpers/tags/label.rb', line 5

def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil)
  options ||= {}

  content_is_options = content_or_options.is_a?(Hash)
  if content_is_options
    options.merge! content_or_options
    @content = nil
  else
    @content = content_or_options
  end

  super(object_name, method_name, template_object, options)
end

Instance Method Details

#render(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'actionview/lib/action_view/helpers/tags/label.rb', line 19

def render(&block)
  options = @options.stringify_keys
  tag_value = options.delete("value")
  name_and_id = options.dup

  if name_and_id["for"]
    name_and_id["id"] = name_and_id["for"]
  else
    name_and_id.delete("id")
  end

  add_default_name_and_id_for_value(tag_value, name_and_id)
  options.delete("index")
  options.delete("namespace")
  options["for"] = name_and_id["id"] unless options.key?("for")

  if block_given?
    content = @template_object.capture(&block)
  else
    content = if @content.blank?
                @object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1')
                method_and_value = tag_value.present? ? "#{@method_name}.#{tag_value}" : @method_name

                if object.respond_to?(:to_model)
                  key = object.class.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_name)
                end

    content ||= @method_name.humanize
  end

  label_tag(name_and_id["id"], content, options)
end