123456789_123456789_123456789_123456789_123456789_

Class: ActionView::Helpers::Tags::Base

Do not use. This class is for internal use only.

Constant Summary

::ActionView::Helpers::TagHelper - Included

ARIA_PREFIXES, BOOLEAN_ATTRIBUTES, DATA_PREFIXES, PRE_CONTENT_STRINGS, TAG_TYPES

::ActionView::Helpers::ContentExfiltrationPreventionHelper - Included

CLOSE_CDATA_COMMENT, CLOSE_FORM_TAG, CLOSE_OPTION_TAG, CLOSE_QUOTES_COMMENT, CONTENT_EXFILTRATION_PREVENTION_MARKUP

::ActionView::Helpers::UrlHelper - Included

BUTTON_TAG_METHOD_VERBS, STRINGIFIED_COMMON_METHODS

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::ActionView::Helpers::ActiveModelInstanceTag - Included

::ActionView::Helpers::FormTagHelper - Included

#button_tag

Creates a button element that defines a submit button, reset button or a generic button which can be used in JavaScript, for example.

#check_box_tag

Creates a check box form input tag.

#color_field_tag

Creates a text field of type “color”.

#date_field_tag

Creates a text field of type “date”.

#datetime_field_tag

Creates a text field of type “datetime-local”.

#datetime_local_field_tag
#email_field_tag

Creates a text field of type “email”.

#field_id

Generate an HTML id attribute value for the given name and field combination.

#field_name

Generate an HTML name attribute value for the given name and field combination.

#field_set_tag

Creates a field set for grouping HTML form elements.

#fieldset_tag
#file_field_tag

Creates a file upload field.

#form_tag

Starts a form tag that points the action to a URL configured with url_for_options just like ActionController::Base#url_for.

#hidden_field_tag

Creates a hidden form input field used to transmit data that would be lost due to HTTP’s statelessness or data that should be hidden from the user.

#image_submit_tag

Displays an image which when clicked will submit the form.

#label_tag

Creates a label element.

#month_field_tag

Creates a text field of type “month”.

#number_field_tag

Creates a number field.

#password_field_tag

Creates a password field, a masked text field that will hide the users input behind a mask character.

#phone_field_tag
#radio_button_tag

Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.

#range_field_tag

Creates a range form element.

#search_field_tag

Creates a text field of type “search”.

#select_tag

Creates a dropdown selection box, or if the :multiple option is set to true, a multiple choice selection box.

#submit_tag

Creates a submit button with the text #value as the caption.

#telephone_field_tag

Creates a text field of type “tel”.

#text_area_tag

Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.

#text_field_tag

Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.

#time_field_tag

Creates a text field of type “time”.

#url_field_tag

Creates a text field of type “url”.

#utf8_enforcer_tag

Creates the hidden UTF-8 enforcer tag.

#week_field_tag

Creates a text field of type “week”.

#convert_direct_upload_option_to_url, #extra_tags_for_form, #form_tag_html, #form_tag_with_body, #html_options_for_form,
#sanitize_to_id
#set_default_disable_with

::ActionView::Helpers::TextHelper - Included

#concat

The preferred method of outputting text in your views is to use the <%= "text" %> eRuby syntax.

#current_cycle

Returns the current cycle string after a cycle has been started.

#cycle

Creates a Cycle object whose to_s method cycles through elements of an array every time it is called.

#excerpt

Extracts the first occurrence of phrase plus surrounding text from text.

#highlight

Highlights occurrences of phrases in text by formatting them with a highlighter string.

#pluralize

Attempts to pluralize the singular word unless count is 1.

#reset_cycle

Resets a cycle so that it starts from the first element the next time it is called.

#safe_concat,
#simple_format

Returns text transformed into HTML using simple formatting rules.

#truncate

Truncates text if it is longer than a specified :length.

#word_wrap

Wraps the text into lines no longer than line_width width.

#cut_excerpt_part,
#get_cycle

The cycle helpers need to store the cycles in a place that is guaranteed to be reset every time a page is rendered, so it uses an instance variable of ::ActionView::Base.

#set_cycle, #split_paragraphs

::ActionView::Helpers::SanitizeHelper - Included

#sanitize

Sanitizes HTML input, stripping all but known-safe tags and attributes.

#sanitize_css

Sanitizes a block of CSS code.

#strip_links

Strips all link tags from html leaving just the link text.

#strip_tags

Strips all HTML tags from html, including comments and special characters.

::ActionView::Helpers::UrlHelper - Included

#button_to

Generates a form containing a single button that submits to the URL created by the set of options.

#current_page?

True if the current request URI was generated by the given options.

#link_to

Creates an anchor element of the given name using a URL created by the set of options.

#link_to_if

Creates a link tag of the given name using a URL created by the set of options if condition is true, otherwise only the name is returned.

#link_to_unless

Creates a link tag of the given name using a URL created by the set of options unless condition is true, in which case only the name is returned.

#link_to_unless_current

Creates a link tag of the given name using a URL created by the set of options unless the current request URI is the same as the links, in which case only the name is returned (or the given block is yielded, if one exists).

#mail_to

Creates a mailto link tag to the specified email_address, which is also used as the name of the link unless name is specified.

#phone_to

Creates a TEL anchor link tag to the specified phone_number.

#sms_to

Creates an SMS anchor link tag to the specified phone_number.

#add_method_to_attributes!, #convert_options_to_data_attributes, #link_to_remote_options?, #method_for_options, #method_not_get_method?, #method_tag, #remove_trailing_slash!,
#to_form_params

Returns an array of hashes each containing :name and :value keys suitable for use as the names and values of form input fields:

#token_tag, #url_target,
#url_for

Basic implementation of url_for to allow use helpers without routes existence.

#_back_url, #_filtered_referrer

::ActionView::Helpers::ContentExfiltrationPreventionHelper - Included

::ActionView::Helpers::TagHelper - Included

#cdata_section

Returns a CDATA section with the given content.

#class_names
#content_tag

Returns an HTML block tag of type name surrounding the content.

#escape_once

Returns an escaped version of html without affecting existing escaped entities.

#tag

Returns an HTML tag.

#token_list

Returns a string of tokens built from args.

#build_tag_values, #ensure_valid_html5_tag_name, #tag_builder

::ActionView::Helpers::OutputSafetyHelper - Included

#raw

This method outputs without escaping a string.

#safe_join

This method returns an HTML safe string similar to what Array#join would return.

#to_sentence

Converts the array to a comma-separated sentence where the last element is joined by the connector word.

::ActionView::Helpers::CaptureHelper - Included

#capture

The capture method extracts part of a template as a string object.

#content_for

Calling content_for stores a block of markup in an identifier for later use.

#content_for?

content_for? checks whether any content has been captured yet using content_for.

#provide

The same as content_for but when used with streaming flushes straight back to the layout.

#with_output_buffer

Use an alternate output buffer for the duration of the block.

Constructor Details

.new(object_name, method_name, template_object, options = {}) ⇒ Base

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 11

def initialize(object_name, method_name, template_object, options = {})
  @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(options.delete(:object))
  @skip_default_ids = options.delete(:skip_default_ids)
  @allow_method_names_outside_object = options.delete(:allow_method_names_outside_object)
  @options = options

  if Regexp.last_match
    @generate_indexed_names = true
    @auto_index = retrieve_autoindex(Regexp.last_match.pre_match)
  else
    @generate_indexed_names = false
    @auto_index = nil
  end
end

Instance Attribute Details

#generate_ids?Boolean (readonly, private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 132

def generate_ids?
  !@skip_default_ids
end

#object (readonly)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 9

attr_reader :object

#value_came_from_user?Boolean (readonly, private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 58

def value_came_from_user?
  method_name = "#{@method_name}_came_from_user?"
  !object.respond_to?(method_name) || object.public_send(method_name)
end

Instance Method Details

#add_default_name_and_id(options) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 96

def add_default_name_and_id(options)
  index = name_and_id_index(options)
  options["name"] = options.fetch("name") { tag_name(options["multiple"], index) }

  if generate_ids?
    options["id"] = options.fetch("id") { tag_id(index, options.delete("namespace")) }
    if namespace = options.delete("namespace")
      options["id"] = options["id"] ? "#{namespace}_#{options['id']}" : namespace
    end
  end
end

#add_default_name_and_id_for_value(tag_value, options) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 83

def add_default_name_and_id_for_value(tag_value, options)
  if tag_value.nil?
    add_default_name_and_id(options)
  else
    specified_id = options["id"]
    add_default_name_and_id(options)

    if specified_id.blank? && options["id"].present?
      options["id"] += "_#{sanitized_value(tag_value)}"
    end
  end
end

#name_and_id_index(options) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 124

def name_and_id_index(options)
  if options.key?("index")
    options.delete("index") || ""
  elsif @generate_indexed_names
    @auto_index || ""
  end
end

#render

This is what child classes implement.

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 31

def render
  raise NotImplementedError, "Subclasses must implement a render method"
end

#retrieve_autoindex(pre_match) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 74

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) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 63

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

#sanitized_method_name (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 116

def sanitized_method_name
  @sanitized_method_name ||= @method_name.delete_suffix("?")
end

#sanitized_value(value) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 120

def sanitized_value(value)
  value.to_s.gsub(/[\s.]/, "_").gsub(/[^-[[:word:]]]/, "").downcase
end

#tag_id(index = nil, namespace = nil) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 112

def tag_id(index = nil, namespace = nil)
  @template_object.field_id(@object_name, @method_name, index: index, namespace: namespace)
end

#tag_name(multiple = false, index = nil) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 108

def tag_name(multiple = false, index = nil)
  @template_object.field_name(@object_name, sanitized_method_name, multiple: multiple, index: index)
end

#value (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 36

def value
  return unless object

  if @allow_method_names_outside_object
    object.public_send @method_name if object.respond_to?(@method_name)
  else
    object.public_send @method_name
  end
end

#value_before_type_cast (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tags/base.rb', line 46

def value_before_type_cast
  return unless object

  method_before_type_cast = @method_name + "_before_type_cast"

  if value_came_from_user? && object.respond_to?(method_before_type_cast)
    object.public_send(method_before_type_cast)
  else
    value
  end
end