Class: ActionView::Helpers::DateTimeSelector

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

Overview

:nodoc:

Constant Summary collapse

DEFAULT_PREFIX =
'date'.freeze
POSITION =
{
  :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6
}.freeze

Constants included from TagHelper

TagHelper::BOOLEAN_ATTRIBUTES

Constants included from ERB::Util

ERB::Util::HTML_ESCAPE, ERB::Util::JSON_ESCAPE

Instance Method Summary collapse

Methods included from TagHelper

#cdata_section, #content_tag, #escape_once, #tag

Methods included from ERB::Util

#html_escape, j, json_escape

Constructor Details

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

Returns a new instance of DateTimeSelector.



519
520
521
522
523
# File 'lib/action_view/helpers/date_helper.rb', line 519

def initialize(datetime, options = {}, html_options = {})
  @options      = options.dup
  @html_options = html_options.dup
  @datetime     = datetime
end

Instance Method Details

#select_dateObject



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/action_view/helpers/date_helper.rb', line 555

def select_date
  order = date_order.dup

  # TODO: Remove tag conditional
  if @options[:tag]
    @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)

    # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
    # valid (otherwise it could be 31 and february wouldn't be a valid date)
    if @datetime && @options[:discard_day] && !@options[:discard_month]
      @datetime = @datetime.change(:day => 1)
    end
  end

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

  build_selects_from_types(order)
end

#select_datetimeObject



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/action_view/helpers/date_helper.rb', line 525

def select_datetime
  # TODO: Remove tag conditional
  # Ideally we could just join select_date and select_date for the tag case
  if @options[:tag] && @options[:ignore_date]
    select_time
  elsif @options[:tag]
    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]

    # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
    # valid (otherwise it could be 31 and february wouldn't be a valid date)
    if @datetime && @options[:discard_day] && !@options[:discard_month]
      @datetime = @datetime.change(:day => 1)
    end

    [: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)
  else
    "#{select_date}#{@options[:datetime_separator]}#{select_time}"
  end
end

#select_dayObject



623
624
625
626
627
628
629
# File 'lib/action_view/helpers/date_helper.rb', line 623

def select_day
  if @options[:use_hidden] || @options[:discard_day]
    build_hidden(:day, day)
  else
    build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => false)
  end
end

#select_hourObject



615
616
617
618
619
620
621
# File 'lib/action_view/helpers/date_helper.rb', line 615

def select_hour
  if @options[:use_hidden] || @options[:discard_hour]
    build_hidden(:hour, hour)
  else
    build_options_and_select(:hour, hour, :end => 23)
  end
end

#select_minuteObject



607
608
609
610
611
612
613
# File 'lib/action_view/helpers/date_helper.rb', line 607

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



631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/action_view/helpers/date_helper.rb', line 631

def select_month
  if @options[:use_hidden] || @options[:discard_month]
    build_hidden(:month, month)
  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



599
600
601
602
603
604
605
# File 'lib/action_view/helpers/date_helper.rb', line 599

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



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/action_view/helpers/date_helper.rb', line 580

def select_time
  order = []

  # TODO: Remove tag conditional
  if @options[:tag]
    @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]
  end

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

  build_selects_from_types(order)
end

#select_yearObject



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/action_view/helpers/date_helper.rb', line 645

def select_year
  if !@datetime || @datetime == 0
    val = ''
    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

    build_options_and_select(:year, val, options)
  end
end