module ActionView::Helpers

Active Model Helpers

Action View Asset URL Helpers

Action View Atom Feed Helpers

Action View Cache Helper

Action View Capture Helper

Action View CSRF Helper

Action View Debug Helper

Provides a set of methods for making it easier to debug Rails objects.

Action View Form Helpers

Action View Form Option Helpers

Action View Form Tag Helpers

Action View Sanitize Helpers

Action View Translation Helpers

Public Instance Methods

select_month() click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 808
def select_month
  if @options[:use_hidden] || @options[:discard_month]
    build_hidden(:month, month || 1)
  else
    month_options = []
    1.upto(12) do |month_number|
      options = { value: month_number }
      options[:selected] = "selected" if month == month_number
      month_options << content_tag("option".freeze, month_name(month_number), options) + "\n"
    end
    build_select(:month, month_options.join)
  end
end
select_year() click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 822
def select_year
  if !@datetime || @datetime == 0
    val = "1"
    middle_year = Date.today.year
  else
    val = middle_year = year
  end

  if @options[:use_hidden] || @options[:discard_year]
    build_hidden(:year, val)
  else
    options                     = {}
    options[:start]             = @options[:start_year] || middle_year - 5
    options[:end]               = @options[:end_year] || middle_year + 5
    options[:step]              = options[:start] < options[:end] ? 1 : -1
    options[:leading_zeros]     = false
    options[:max_years_allowed] = @options[:max_years_allowed] || 1000

    if (options[:end] - options[:start]).abs > options[:max_years_allowed]
      raise ArgumentError, "There are too many years options to be built. Are you sure you haven't mistyped something? You can provide the :max_years_allowed parameter."
    end

    build_options_and_select(:year, val, options)
  end
end

Private Instance Methods

build_hidden(type, value) click to toggle source

Builds hidden input tag for date part and value.

build_hidden(:year, 2008)
=> "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />"
# File lib/action_view/helpers/date_helper.rb, line 1046
def build_hidden(type, value)
  select_options = {
    type: "hidden",
    id: input_id_from_type(type),
    name: input_name_from_type(type),
    value: value
  }.merge!(@html_options.slice(:disabled))
  select_options[:disabled] = "disabled" if @options[:disabled]

  tag(:input, select_options) + "\n".html_safe
end
build_options(selected, options = {}) click to toggle source

Build select option HTML from date value and options.

build_options(15, start: 1, end: 31)
=> "<option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>..."

If use_two_digit_numbers: true option is passed

build_options(15, start: 1, end: 31, use_two_digit_numbers: true)
=> "<option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>..."

If :step options is passed

build_options(15, start: 1, end: 31, step: 2)
=> "<option value="1">1</option>
    <option value="3">3</option>
    <option value="5">5</option>..."
# File lib/action_view/helpers/date_helper.rb, line 966
def build_options(selected, options = {})
  options = {
    leading_zeros: true, ampm: false, use_two_digit_numbers: false
  }.merge!(options)

  start         = options.delete(:start) || 0
  stop          = options.delete(:end) || 59
  step          = options.delete(:step) || 1
  leading_zeros = options.delete(:leading_zeros)

  select_options = []
  start.step(stop, step) do |i|
    value = leading_zeros ? sprintf("%02d", i) : i
    tag_options = { value: value }
    tag_options[:selected] = "selected" if selected == i
    text = options[:use_two_digit_numbers] ? sprintf("%02d", i) : value
    text = options[:ampm] ? AMPM_TRANSLATION[i] : text
    select_options << content_tag("option".freeze, text, tag_options)
  end

  (select_options.join("\n") + "\n").html_safe
end
build_options_and_select(type, selected, options = {}) click to toggle source

Build full select tag from date type and options.

# File lib/action_view/helpers/date_helper.rb, line 945
def build_options_and_select(type, selected, options = {})
  build_select(type, build_options(selected, options))
end
build_select(type, select_options_as_html) click to toggle source

Builds select tag from date type and HTML select options.

build_select(:month, "<option value="1">January</option>...")
=> "<select id="post_written_on_2i" name="post[written_on(2i)]">
      <option value="1">January</option>...
    </select>"
# File lib/action_view/helpers/date_helper.rb, line 994
def build_select(type, select_options_as_html)
  select_options = {
    id: input_id_from_type(type),
    name: input_name_from_type(type)
  }.merge!(@html_options)
  select_options[:disabled] = "disabled" if @options[:disabled]
  select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes]

  select_html = "\n"
  select_html << content_tag("option".freeze, "", value: "") + "\n" if @options[:include_blank]
  select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
  select_html << select_options_as_html

  (content_tag("select".freeze, select_html.html_safe, select_options) + "\n").html_safe
end
build_selects_from_types(order) click to toggle source

Given an ordering of datetime components, create the selection HTML and join them with their appropriate separators.

# File lib/action_view/helpers/date_helper.rb, line 1083
def build_selects_from_types(order)
  select = ""
  first_visible = order.find { |type| !@options[:"discard_#{type}"] }
  order.reverse_each do |type|
    separator = separator(type) unless type == first_visible # don't add before first visible field
    select.insert(0, separator.to_s + send("select_#{type}").to_s)
  end
  select.html_safe
end
date_order() click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 927
def date_order
  @date_order ||= @options[:order] || translated_date_order
end
input_id_from_type(type) click to toggle source

Returns the id attribute for the input tag.

=> "post_written_on_1i"
# File lib/action_view/helpers/date_helper.rb, line 1074
def input_id_from_type(type)
  id = input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, "_").gsub(/[\]\)]/, "")
  id = @options[:namespace] + "_" + id if @options[:namespace]

  id
end
input_name_from_type(type) click to toggle source

Returns the name attribute for the input tag.

=> post[written_on(1i)]
# File lib/action_view/helpers/date_helper.rb, line 1060
def input_name_from_type(type)
  prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
  prefix += "[#{@options[:index]}]" if @options.has_key?(:index)

  field_name = @options[:field_name] || type.to_s
  if @options[:include_position]
    field_name += "(#{ActionView::Helpers::DateTimeSelector::POSITION[type]}i)"
  end

  @options[:discard_type] ? prefix : "#{prefix}[#{field_name}]"
end
month_name(number) click to toggle source

Looks up month names by number (1-based):

month_name(1) # => "January"

If the :use_month_numbers option is passed:

month_name(1) # => 1

If the :use_two_month_numbers option is passed:

month_name(1) # => '01'

If the :add_month_numbers option is passed:

month_name(1) # => "1 - January"

If the :month_format_string option is passed:

month_name(1) # => "January (01)"

depending on the format string.

# File lib/action_view/helpers/date_helper.rb, line 913
def month_name(number)
  if @options[:use_month_numbers]
    number
  elsif @options[:use_two_digit_numbers]
    "%02d" % number
  elsif @options[:add_month_numbers]
    "#{number} - #{month_names[number]}"
  elsif format_string = @options[:month_format_string]
    format_string % { number: number, name: month_names[number] }
  else
    month_names[number]
  end
end
month_names() click to toggle source

Returns translated month names, but also ensures that a custom month name array has a leading nil element.

# File lib/action_view/helpers/date_helper.rb, line 870
def month_names
  @month_names ||= begin
    month_names = @options[:use_month_names] || translated_month_names
    month_names.unshift(nil) if month_names.size < 13
    month_names
  end
end
prompt_option_tag(type, options) click to toggle source

Builds a prompt option tag with supplied options or from default options.

prompt_option_tag(:month, prompt: 'Select month')
=> "<option value="">Select month</option>"
# File lib/action_view/helpers/date_helper.rb, line 1028
def prompt_option_tag(type, options)
  prompt =              case options
    when Hash
      default_options = { year: false, month: false, day: false, hour: false, minute: false, second: false }
      default_options.merge!(options)[type.to_sym]
    when String
      options
    else
      I18n.translate(:"datetime.prompts.#{type}", locale: @options[:locale])
    end

  prompt ? content_tag("option".freeze, prompt, value: "") : ""
end
separator(type) click to toggle source

Returns the separator for a given datetime component.

# File lib/action_view/helpers/date_helper.rb, line 1094
def separator(type)
  return "" if @options[:use_hidden]

  case type
  when :year, :month, :day
    @options[:"discard_#{type}"] ? "" : @options[:date_separator]
  when :hour
    (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
  when :minute, :second
    @options[:"discard_#{type}"] ? "" : @options[:time_separator]
  end
end
set_day_if_discarded() click to toggle source

If the day is hidden, the day should be set to the 1st so all month and year choices are valid. Otherwise, February 31st or February 29th, 2011 can be selected, which are invalid.

# File lib/action_view/helpers/date_helper.rb, line 862
def set_day_if_discarded
  if @datetime && @options[:discard_day]
    @datetime = @datetime.change(day: 1)
  end
end
translated_date_order() click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 931
def translated_date_order
  date_order = I18n.translate(:'date.order', locale: @options[:locale], default: [])
  date_order = date_order.map(&:to_sym)

  forbidden_elements = date_order - [:year, :month, :day]
  if forbidden_elements.any?
    raise StandardError,
      "#{@options[:locale]}.date.order only accepts :year, :month and :day"
  end

  date_order
end
translated_month_names() click to toggle source

Returns translated month names.

=> [nil, "January", "February", "March",
         "April", "May", "June", "July",
         "August", "September", "October",
         "November", "December"]

If :use_short_month option is set

=> [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
# File lib/action_view/helpers/date_helper.rb, line 887
def translated_month_names
  key = @options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
  I18n.translate(key, locale: @options[:locale])
end