Module: ActionView::Helpers::InstanceTagMethods

Extended by:
ActiveSupport::Concern
Includes:
Context, CaptureHelper, FormTagHelper, TagHelper
Included in:
InstanceTag
Defined in:
lib/action_view/helpers/form_helper.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

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

Constants included from TagHelper

TagHelper::BOOLEAN_ATTRIBUTES

Instance Attribute Summary collapse

Attributes included from Context

#output_buffer

Instance Method Summary collapse

Methods included from CaptureHelper

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

Methods included from Context

#convert_to_model

Methods included from TagHelper

#cdata_section, #content_tag, #escape_once, #tag

Methods included from FormTagHelper

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

#auto_link, #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 UrlHelper

#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

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



857
858
859
# File 'lib/action_view/helpers/form_helper.rb', line 857

def method_name
  @method_name
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



857
858
859
# File 'lib/action_view/helpers/form_helper.rb', line 857

def object_name
  @object_name
end

Instance Method Details

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



863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/action_view/helpers/form_helper.rb', line 863

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 = object
  if @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]")
    if (object ||= @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}")) && object.respond_to?(:to_param)
      @auto_index = 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
end

#objectObject



997
998
999
1000
1001
1002
1003
# File 'lib/action_view/helpers/form_helper.rb', line 997

def object
  @object || @template_object.instance_variable_get("@#{@object_name}")
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



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

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



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# File 'lib/action_view/helpers/form_helper.rb', line 958

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_content_tag(tag_name, options = {}) ⇒ Object



993
994
995
# File 'lib/action_view/helpers/form_helper.rb', line 993

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

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



910
911
912
913
914
915
916
917
918
919
920
921
922
# File 'lib/action_view/helpers/form_helper.rb', line 910

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"] &&= html_escape(options["value"])
  add_default_name_and_id(options)
  tag("input", options)
end

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



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'lib/action_view/helpers/form_helper.rb', line 876

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?
      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 ||= method_name.humanize

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

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



924
925
926
927
928
929
930
# File 'lib/action_view/helpers/form_helper.rb', line 924

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



932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'lib/action_view/helpers/form_helper.rb', line 932

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_text_area_tag(options = {}) ⇒ Object



947
948
949
950
951
952
953
954
955
956
# File 'lib/action_view/helpers/form_helper.rb', line 947

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

#value(object) ⇒ Object



1005
1006
1007
# File 'lib/action_view/helpers/form_helper.rb', line 1005

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

#value_before_type_cast(object) ⇒ Object



1009
1010
1011
# File 'lib/action_view/helpers/form_helper.rb', line 1009

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