Class: ActionView::Helpers::DateTimeSelector

Inherits:
Object
  • Object
show all
Includes:
TagHelper
Defined in:
lib/action_view/helpers/date_helper.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_PREFIX =
"date"
POSITION =
{
  year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6
}.freeze
AMPM_TRANSLATION =
Hash[
  [[0, "12 AM"], [1, "01 AM"], [2, "02 AM"], [3, "03 AM"],
   [4, "04 AM"], [5, "05 AM"], [6, "06 AM"], [7, "07 AM"],
   [8, "08 AM"], [9, "09 AM"], [10, "10 AM"], [11, "11 AM"],
   [12, "12 PM"], [13, "01 PM"], [14, "02 PM"], [15, "03 PM"],
   [16, "04 PM"], [17, "05 PM"], [18, "06 PM"], [19, "07 PM"],
   [20, "08 PM"], [21, "09 PM"], [22, "10 PM"], [23, "11 PM"]]
].freeze

Constants included from TagHelper

TagHelper::ARIA_PREFIXES, TagHelper::BOOLEAN_ATTRIBUTES, TagHelper::DATA_PREFIXES, TagHelper::PRE_CONTENT_STRINGS, TagHelper::TAG_TYPES

Instance Method Summary collapse

Methods included from TagHelper

build_tag_values, #cdata_section, #content_tag, ensure_valid_html5_tag_name, #escape_once, #tag, #token_list

Methods included from OutputSafetyHelper

#raw, #safe_join, #to_sentence

Methods included from CaptureHelper

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

Constructor Details

#initialize(datetime, options = {}, html_options = {}) ⇒ DateTimeSelector

Returns a new instance of DateTimeSelector.



727
728
729
730
731
732
733
# File 'lib/action_view/helpers/date_helper.rb', line 727

def initialize(datetime, options = {}, html_options = {})
  @options      = options.dup
  @html_options = html_options.dup
  @datetime     = datetime
  @options[:datetime_separator] ||= " — "
  @options[:time_separator]     ||= " : "
end

Instance Method Details

#select_dateObject



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/action_view/helpers/date_helper.rb', line 756

def select_date
  order = date_order.dup

  @options[:discard_hour]     = true
  @options[:discard_minute]   = true
  @options[:discard_second]   = true

  @options[:discard_year]   ||= true unless order.include?(:year)
  @options[:discard_month]  ||= true unless order.include?(:month)
  @options[:discard_day]    ||= true if @options[:discard_month] || !order.include?(:day)

  set_day_if_discarded

  [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }

  build_selects_from_types(order)
end

#select_datetimeObject



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/action_view/helpers/date_helper.rb', line 735

def select_datetime
  order = date_order.dup
  order -= [:hour, :minute, :second]
  @options[:discard_year]   ||= true unless order.include?(:year)
  @options[:discard_month]  ||= true unless order.include?(:month)
  @options[:discard_day]    ||= true if @options[:discard_month] || !order.include?(:day)
  @options[:discard_minute] ||= true if @options[:discard_hour]
  @options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute]

  set_day_if_discarded

  if @options[:tag] && @options[:ignore_date]
    select_time
  else
    [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
    order += [:hour, :minute, :second] unless @options[:discard_hour]

    build_selects_from_types(order)
  end
end

#select_dayObject



818
819
820
821
822
823
824
# File 'lib/action_view/helpers/date_helper.rb', line 818

def select_day
  if @options[:use_hidden] || @options[:discard_day]
    build_hidden(:day, day || 1)
  else
    build_select(:day, build_day_options(day))
  end
end

#select_hourObject



806
807
808
809
810
811
812
813
814
815
816
# File 'lib/action_view/helpers/date_helper.rb', line 806

def select_hour
  if @options[:use_hidden] || @options[:discard_hour]
    build_hidden(:hour, hour)
  else
    options         = {}
    options[:ampm]  = @options[:ampm] || false
    options[:start] = @options[:start_hour] || 0
    options[:end]   = @options[:end_hour] || 23
    build_options_and_select(:hour, hour, options)
  end
end

#select_minuteObject



798
799
800
801
802
803
804
# File 'lib/action_view/helpers/date_helper.rb', line 798

def select_minute
  if @options[:use_hidden] || @options[:discard_minute]
    build_hidden(:minute, min)
  else
    build_options_and_select(:minute, min, step: @options[:minute_step])
  end
end

#select_monthObject



826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/action_view/helpers/date_helper.rb', line 826

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 << ("option", month_name(month_number), options) + "\n"
    end
    build_select(:month, month_options.join)
  end
end

#select_secondObject



790
791
792
793
794
795
796
# File 'lib/action_view/helpers/date_helper.rb', line 790

def select_second
  if @options[:use_hidden] || @options[:discard_second]
    build_hidden(:second, sec) if @options[:include_seconds]
  else
    build_options_and_select(:second, sec)
  end
end

#select_timeObject



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# File 'lib/action_view/helpers/date_helper.rb', line 774

def select_time
  order = []

  @options[:discard_month]    = true
  @options[:discard_year]     = true
  @options[:discard_day]      = true
  @options[:discard_second] ||= true unless @options[:include_seconds]

  order += [:year, :month, :day] unless @options[:ignore_date]

  order += [:hour, :minute]
  order << :second if @options[:include_seconds]

  build_selects_from_types(order)
end

#select_yearObject



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/action_view/helpers/date_helper.rb', line 840

def select_year
  if !year || @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

    max_years_allowed = @options[:max_years_allowed] || 1000

    if (options[:end] - options[:start]).abs > 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_select(:year, build_year_options(val, options))
  end
end