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)


1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/action_view/helpers/form_helper.rb', line 1138

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)


1155
1156
1157
# File 'lib/action_view/helpers/form_helper.rb', line 1155

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

.value(object, method_name) ⇒ Object



1126
1127
1128
# File 'lib/action_view/helpers/form_helper.rb', line 1126

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

.value_before_type_cast(object, method_name) ⇒ Object



1130
1131
1132
1133
1134
1135
1136
# File 'lib/action_view/helpers/form_helper.rb', line 1130

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



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

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



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

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



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/action_view/helpers/form_helper.rb', line 1080

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



1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'lib/action_view/helpers/form_helper.rb', line 1058

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



1093
1094
1095
# File 'lib/action_view/helpers/form_helper.rb', line 1093

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



1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/action_view/helpers/form_helper.rb', line 1010

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
1001
1002
1003
1004
1005
1006
1007
1008
# 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?
      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



1024
1025
1026
1027
1028
1029
1030
# File 'lib/action_view/helpers/form_helper.rb', line 1024

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



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

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



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

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



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

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

#value_before_type_cast(object) ⇒ Object



1121
1122
1123
# File 'lib/action_view/helpers/form_helper.rb', line 1121

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