Class: ActionView::Helpers::InstanceTag

Inherits:
Object
  • Object
show all
Includes:
Context, ActiveModelInstanceTag, CaptureHelper, FormOptionsHelper, FormTagHelper, TagHelper
Defined in:
lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/active_model_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

Instance Attribute Summary collapse

Attributes included from Context

#output_buffer, #view_flow

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 SanitizeHelper

#sanitize, #sanitize_css, #strip_links, #strip_tags

Methods included from ActiveModelInstanceTag

#error_message, #error_wrapping, #tag

Methods included from CaptureHelper

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

Methods included from Context

#_layout_for, #_prepare_context

Methods included from TagHelper

#cdata_section, #content_tag, #escape_once, #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

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

Constructor Details

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

Returns a new instance of InstanceTag.



959
960
961
962
963
964
965
# File 'lib/action_view/helpers/form_helper.rb', line 959

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.



953
954
955
# File 'lib/action_view/helpers/form_helper.rb', line 953

def method_name
  @method_name
end

#objectObject (readonly)

Returns the value of attribute object.



953
954
955
# File 'lib/action_view/helpers/form_helper.rb', line 953

def object
  @object
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



953
954
955
# File 'lib/action_view/helpers/form_helper.rb', line 953

def object_name
  @object_name
end

Class Method Details

.check_box_checked?(value, checked_value) ⇒ Boolean

Returns:

  • (Boolean)


1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
# File 'lib/action_view/helpers/form_helper.rb', line 1130

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)


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

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

.value(object, method_name) ⇒ Object



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

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

.value_before_type_cast(object, method_name) ⇒ Object



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

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



1100
1101
1102
1103
1104
1105
1106
1107
# File 'lib/action_view/helpers/form_helper.rb', line 1100

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



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'lib/action_view/helpers/form_helper.rb', line 1089

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



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

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



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/action_view/helpers/form_helper.rb', line 1050

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 = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
  checkbox = tag("input", options)
  (hidden + checkbox).html_safe
end

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



563
564
565
566
567
568
569
570
571
572
# File 'lib/action_view/helpers/form_options_helper.rb', line 563

def to_collection_select_tag(collection, value_method, text_method, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  value = value(object)
  disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
  selected_value = options.has_key?(:selected) ? options[:selected] : value
  (
    "select", add_options(options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => disabled_value), options, value), html_options
  )
end

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



1085
1086
1087
# File 'lib/action_view/helpers/form_helper.rb', line 1085

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

#to_date_select_tag(options = {}, html_options = {}) ⇒ Object



970
971
972
# File 'lib/action_view/helpers/date_helper.rb', line 970

def to_date_select_tag(options = {}, html_options = {})
  datetime_selector(options, html_options).select_date.html_safe
end

#to_datetime_select_tag(options = {}, html_options = {}) ⇒ Object



978
979
980
# File 'lib/action_view/helpers/date_helper.rb', line 978

def to_datetime_select_tag(options = {}, html_options = {})
  datetime_selector(options, html_options).select_datetime.html_safe
end

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



574
575
576
577
578
579
580
581
# File 'lib/action_view/helpers/form_options_helper.rb', line 574

def to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  value = value(object)
  (
    "select", add_options(option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, value), options, value), html_options
  )
end

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



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/action_view/helpers/form_helper.rb', line 1002

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



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File 'lib/action_view/helpers/form_helper.rb', line 967

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["for"] ||= name_and_id["id"]

  if block_given?
    label_tag(name_and_id["id"], options, &block)
  else
    content = if text.blank?
      method_and_value = tag_value.present? ? "#{method_name}.#{tag_value}" : method_name
      I18n.t("helpers.label.#{object_name}.#{method_and_value}", :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 ||= method_name.humanize

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

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



1016
1017
1018
1019
1020
1021
1022
# File 'lib/action_view/helpers/form_helper.rb', line 1016

def to_number_field_tag(field_type, options = {})
  options = options.stringify_keys
  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



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/action_view/helpers/form_helper.rb', line 1024

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



554
555
556
557
558
559
560
561
# File 'lib/action_view/helpers/form_options_helper.rb', line 554

def to_select_tag(choices, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  value = value(object)
  selected_value = options.has_key?(:selected) ? options[:selected] : value
  disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
  ("select", add_options(options_for_select(choices, :selected => selected_value, :disabled => disabled_value), options, selected_value), html_options)
end

#to_text_area_tag(options = {}) ⇒ Object



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/action_view/helpers/form_helper.rb', line 1039

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", ERB::Util.html_escape(options.delete('value') || value_before_type_cast(object)), options)
end

#to_time_select_tag(options = {}, html_options = {}) ⇒ Object



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

def to_time_select_tag(options = {}, html_options = {})
  datetime_selector(options, html_options).select_time.html_safe
end

#to_time_zone_select_tag(priority_zones, options, html_options) ⇒ Object



583
584
585
586
587
588
589
590
591
592
593
# File 'lib/action_view/helpers/form_options_helper.rb', line 583

def to_time_zone_select_tag(priority_zones, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  value = value(object)
  ("select",
    add_options(
      time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone),
      options, value
    ), html_options
  )
end

#value(object) ⇒ Object



1109
1110
1111
# File 'lib/action_view/helpers/form_helper.rb', line 1109

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

#value_before_type_cast(object) ⇒ Object



1113
1114
1115
# File 'lib/action_view/helpers/form_helper.rb', line 1113

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