Class: ActionView::Helpers::InstanceTag

Inherits:
Object
  • Object
show all
Includes:
ActiveModelInstanceTag, DateHelperInstanceTag, FormOptionsHelper, FormTagHelper, TagHelper
Defined in:
lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/form_options_helper.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_FIELD_OPTIONS =
{ "size" => 30 }
DEFAULT_RADIO_OPTIONS =
{ }
DEFAULT_TEXT_AREA_OPTIONS =
{ "cols" => 40, "rows" => 20 }

Constants included from TagHelper

TagHelper::BOOLEAN_ATTRIBUTES, TagHelper::PRE_CONTENT_STRINGS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FormOptionsHelper

#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 TextHelper

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

Methods included from 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

#error_message, #error_wrapping, #tag

Methods included from FormTagHelper

#button_tag, #check_box_tag, #email_field_tag, #field_set_tag, #file_field_tag, #form_tag, #hidden_field_tag, #image_submit_tag, #label_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, #url_field_tag, #utf8_enforcer_tag

Methods included from UrlHelper

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

Methods included from ActionDispatch::Routing::UrlFor

#url_for, #url_options

Methods included from ActionDispatch::Routing::PolymorphicRoutes

#polymorphic_path, #polymorphic_url

Methods included from DateHelperInstanceTag

#to_date_select_tag, #to_datetime_select_tag, #to_time_select_tag

Constructor Details

#initialize(object_name, method_name, template_object, object = nil) ⇒ InstanceTag

Returns a new instance of InstanceTag.



980
981
982
983
984
985
986
987
# File 'lib/action_view/helpers/form_helper.rb', line 980

def initialize(object_name, method_name, template_object, object = nil)
  @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
  @template_object = template_object

  @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]")
  @object = retrieve_object(object)
  @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



974
975
976
# File 'lib/action_view/helpers/form_helper.rb', line 974

def method_name
  @method_name
end

#objectObject (readonly)

Returns the value of attribute object.



974
975
976
# File 'lib/action_view/helpers/form_helper.rb', line 974

def object
  @object
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



974
975
976
# File 'lib/action_view/helpers/form_helper.rb', line 974

def object_name
  @object_name
end

Class Method Details

.check_box_checked?(value, checked_value) ⇒ Boolean

Returns:

  • (Boolean)


1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/action_view/helpers/form_helper.rb', line 1163

def check_box_checked?(value, checked_value)
  case value
  when TrueClass, FalseClass
    value
  when NilClass
    false
  when Integer
    value != 0
  when String
    value == checked_value
  when Array
    value.include?(checked_value)
  else
    value.to_i != 0
  end
end

.radio_button_checked?(value, checked_value) ⇒ Boolean

Returns:

  • (Boolean)


1180
1181
1182
# File 'lib/action_view/helpers/form_helper.rb', line 1180

def radio_button_checked?(value, checked_value)
  value.to_s == checked_value.to_s
end

.value(object, method_name) ⇒ Object



1151
1152
1153
# File 'lib/action_view/helpers/form_helper.rb', line 1151

def value(object, method_name)
  object.send method_name if object
end

.value_before_type_cast(object, method_name) ⇒ Object



1155
1156
1157
1158
1159
1160
1161
# File 'lib/action_view/helpers/form_helper.rb', line 1155

def value_before_type_cast(object, method_name)
  unless object.nil?
    object.respond_to?(method_name + "_before_type_cast") ?
    object.send(method_name + "_before_type_cast") :
    object.send(method_name)
  end
end

Instance Method Details

#retrieve_autoindex(pre_match) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/action_view/helpers/form_helper.rb', line 1133

def retrieve_autoindex(pre_match)
  object = self.object || @template_object.instance_variable_get("@#{pre_match}")
  if object && object.respond_to?(:to_param)
    object.to_param
  else
    raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
  end
end

#retrieve_object(object) ⇒ Object



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/action_view/helpers/form_helper.rb', line 1122

def retrieve_object(object)
  if object
    object
  elsif @template_object.instance_variable_defined?("@#{@object_name}")
    @template_object.instance_variable_get("@#{@object_name}")
  end
rescue NameError
  # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil.
  nil
end

#to_boolean_select_tag(options = {}) ⇒ Object



1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/action_view/helpers/form_helper.rb', line 1105

def to_boolean_select_tag(options = {})
  options = options.stringify_keys
  add_default_name_and_id(options)
  value = value(object)
  tag_text = "<select"
  tag_text << tag_options(options)
  tag_text << "><option value=\"false\""
  tag_text << " selected" if value == false
  tag_text << ">False</option><option value=\"true\""
  tag_text << " selected" if value
  tag_text << ">True</option></select>"
end

#to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/action_view/helpers/form_helper.rb', line 1083

def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
  options = options.stringify_keys
  options["type"]     = "checkbox"
  options["value"]    = checked_value
  if options.has_key?("checked")
    cv = options.delete "checked"
    checked = cv == true || cv == "checked"
  else
    checked = self.class.check_box_checked?(value(object), checked_value)
  end
  options["checked"] = "checked" if checked
  if options["multiple"]
    add_default_name_and_id_for_value(checked_value, options)
    options.delete("multiple")
  else
    add_default_name_and_id(options)
  end
  hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : ""
  checkbox = tag("input", options)
  (hidden + checkbox).html_safe
end

#to_collection_select_tag(collection, value_method, text_method, options, html_options) ⇒ Object



597
598
599
600
601
602
# File 'lib/action_view/helpers/form_options_helper.rb', line 597

def to_collection_select_tag(collection, value_method, text_method, options, html_options)
  selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
  (
    options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => options[:disabled]), options, html_options
  )
end

#to_content_tag(tag_name, options = {}) ⇒ Object



1118
1119
1120
# File 'lib/action_view/helpers/form_helper.rb', line 1118

def (tag_name, options = {})
  (tag_name, value(object), options)
end

#to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) ⇒ Object



604
605
606
607
608
# File 'lib/action_view/helpers/form_options_helper.rb', line 604

def to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
  (
    option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, value(object)), options, html_options
  )
end

#to_input_field_tag(field_type, options = {}) ⇒ Object



1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/action_view/helpers/form_helper.rb', line 1033

def to_input_field_tag(field_type, options = {})
  options = options.stringify_keys
  options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size")
  options = DEFAULT_FIELD_OPTIONS.merge(options)
  if field_type == "hidden"
    options.delete("size")
  end
  options["type"]  ||= field_type
  options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file"
  options["value"] &&= ERB::Util.html_escape(options["value"])
  add_default_name_and_id(options)
  tag("input", options)
end

#to_label_tag(text = nil, options = {}, &block) ⇒ Object



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/action_view/helpers/form_helper.rb', line 989

def to_label_tag(text = nil, options = {}, &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"]

  if block_given?
    @template_object.label_tag(name_and_id["id"], options, &block)
  else
    content = if text.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
      text.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

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

#to_number_field_tag(field_type, options = {}) ⇒ Object



1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/action_view/helpers/form_helper.rb', line 1047

def to_number_field_tag(field_type, options = {})
  options = options.stringify_keys
  options['size'] ||= nil

  if range = options.delete("in") || options.delete("within")
    options.update("min" => range.min, "max" => range.max)
  end
  to_input_field_tag(field_type, options)
end

#to_radio_button_tag(tag_value, options = {}) ⇒ Object



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/action_view/helpers/form_helper.rb', line 1057

def to_radio_button_tag(tag_value, options = {})
  options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
  options["type"]     = "radio"
  options["value"]    = tag_value
  if options.has_key?("checked")
    cv = options.delete "checked"
    checked = cv == true || cv == "checked"
  else
    checked = self.class.radio_button_checked?(value(object), tag_value)
  end
  options["checked"]  = "checked" if checked
  add_default_name_and_id_for_value(tag_value, options)
  tag("input", options)
end

#to_select_tag(choices, options, html_options) ⇒ Object



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/action_view/helpers/form_options_helper.rb', line 579

def to_select_tag(choices, options, html_options)
  selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
  choices = choices.to_a if choices.is_a?(Range)

  # Grouped choices look like this:
  #
  #   [nil, []]
  #   { nil => [] }
  #
  if !choices.empty? && choices.first.respond_to?(:last) && Array === choices.first.last
    option_tags = grouped_options_for_select(choices, :selected => selected_value, :disabled => options[:disabled])
  else
    option_tags = options_for_select(choices, :selected => selected_value, :disabled => options[:disabled])
  end

  (option_tags, options, html_options)
end

#to_text_area_tag(options = {}) ⇒ Object



1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/action_view/helpers/form_helper.rb', line 1072

def to_text_area_tag(options = {})
  options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys)
  add_default_name_and_id(options)

  if size = options.delete("size")
    options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
  end

  ("textarea", options.delete('value') || value_before_type_cast(object), options)
end

#to_time_zone_select_tag(priority_zones, options, html_options) ⇒ Object



610
611
612
613
614
# File 'lib/action_view/helpers/form_options_helper.rb', line 610

def to_time_zone_select_tag(priority_zones, options, html_options)
  (
      time_zone_options_for_select(value(object) || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone), options, html_options
  )
end

#value(object) ⇒ Object



1142
1143
1144
# File 'lib/action_view/helpers/form_helper.rb', line 1142

def value(object)
  self.class.value(object, @method_name)
end

#value_before_type_cast(object) ⇒ Object



1146
1147
1148
# File 'lib/action_view/helpers/form_helper.rb', line 1146

def value_before_type_cast(object)
  self.class.value_before_type_cast(object, @method_name)
end