0
@@ -13,9 +13,6 @@ module ActionView
0
# the select_month method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead of
0
- include ActionView::Helpers::TagHelper
0
- DEFAULT_PREFIX = 'date' unless const_defined?('DEFAULT_PREFIX')
0
# Reports the approximate distance in time between two Time or Date objects or integers as seconds.
0
# Set <tt>include_seconds</tt> to true if you want more detailed approximations when distance < 1 min, 29 secs
0
# Distances are reported based on the following table:
0
@@ -52,7 +49,7 @@ module ActionView
0
# distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute
0
# distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute
0
# distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
0
- # distance_of_time_in_words(from_time, from_time + 4.years +
15.days + 30.minutes + 5.seconds) # => over 4 years
0
+ # distance_of_time_in_words(from_time, from_time + 4.years +
9.days + 30.minutes + 5.seconds) # => over 4 years
0
# to_time = Time.now + 6.years + 19.days
0
# distance_of_time_in_words(from_time, to_time, true) # => over 6 years
0
@@ -109,19 +106,36 @@ module ActionView
0
alias_method :distance_of_time_in_words_to_now, :time_ago_in_words
0
# Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based
0
- # attribute (identified by +method+) on an object assigned to the template (identified by +object+). It's
0
- # possible to tailor the selects through the +options+ hash, which accepts all the keys that each of the
0
- # individual select builders do (like <tt>:use_month_numbers</tt> for select_month) as well as a range of discard
0
- # options. The discard options are <tt>:discard_year</tt>, <tt>:discard_month</tt> and <tt>:discard_day</tt>. Set
0
- # to true, they'll drop the respective select. Discarding the month select will also automatically discard the
0
- # day select. It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with an
0
- # array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. Symbols may be omitted
0
- # and the respective select is not included.
0
- # Pass the <tt>:default</tt> option to set the default date. Use a Time object or a Hash of <tt>:year</tt>,
0
- # <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt>, and <tt>:second</tt>.
0
- # Passing <tt>:disabled => true</tt> as part of the +options+ will make elements inaccessible for change.
0
+ # attribute (identified by +method+) on an object assigned to the template (identified by +object+). You can
0
+ # the output in the +options+ hash.
0
+ # * <tt>:use_month_numbers</tt> - Set to true if you want to use month numbers rather than month names (e.g.
0
+ # "2" instead of "February").
0
+ # * <tt>:use_short_month</tt> - Set to true if you want to use the abbreviated month name instead of the full
0
+ # name (e.g. "Feb" instead of "February").
0
+ # * <tt>:add_month_number</tt> - Set to true if you want to show both, the month's number and name (e.g.
0
+ # "2 - February" instead of "February").
0
+ # * <tt>:use_month_names</tt> - Set to an array with 12 month names if you want to customize month names.
0
+ # Note: You can also use Rails' new i18n functionality for this.
0
+ # * <tt>:date_separator</tt> - Specifies a string to separate the date fields. Default is "" (i.e. nothing).
0
+ # * <tt>:start_year</tt> - Set the start year for the year select. Default is <tt>Time.now.year - 5</tt>.
0
+ # * <tt>:end_year</tt> - Set the end year for the year select. Default is <tt>Time.now.year + 5</tt>.
0
+ # * <tt>:discard_day</tt> - Set to true if you don't want to show a day select. This includes the day
0
+ # as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the
0
+ # first of the given month in order to not create invalid dates like 31 February.
0
+ # * <tt>:discard_month</tt> - Set to true if you don't want to show a month select. This includes the month
0
+ # as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true.
0
+ # * <tt>:discard_year</tt> - Set to true if you don't want to show a year select. This includes the year
0
+ # as a hidden field instead of showing a select field.
0
+ # * <tt>:order</tt> - Set to an array containing <tt>:day</tt>, <tt>:month</tt> and <tt>:year</tt> do
0
+ # customize the order in which the select fields are shown. If you leave out any of the symbols, the respective
0
+ # select will not be shown (like when you set <tt>:discard_xxx => true</tt>. Defaults to the order defined in
0
+ # the respective locale (e.g. [:year, :month, :day] in the en-US locale that ships with Rails).
0
+ # * <tt>:include_blank</tt> - Include a blank option in every select field so it's possible to set empty
0
+ # * <tt>:default</tt> - Set a default date if the affected date isn't set or is nil.
0
+ # * <tt>:disabled</tt> - Set to true if you want show the select fields as disabled.
0
# If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
0
@@ -165,9 +179,9 @@ module ActionView
0
InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_select_tag(options, html_options)
0
- # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified
0
- # time-based attribute (identified by +method+) on an object assigned to the template (identified by +object+).
0
- # You can include the seconds with <tt>:include_seconds</tt>.
0
+ # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a
0
+ # specified time-based attribute (identified by +method+) on an object assigned to the template (identified by
0
+ # +object+). You can include the seconds with <tt>:include_seconds</tt>.
0
# This method will also generate 3 input hidden tags, for the actual year, month and day unless the option
0
# <tt>:ignore_date</tt> is set to +true+.
0
@@ -178,7 +192,8 @@ module ActionView
0
# # Creates a time select tag that, when POSTed, will be stored in the post variable in the sunrise attribute
0
# time_select("post", "sunrise")
0
- # # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
0
+ # # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted
0
# time_select("order", "submitted")
0
# # Creates a time select tag that, when POSTed, will be stored in the mail variable in the sent_at attribute
0
@@ -210,7 +225,8 @@ module ActionView
0
# If anything is passed in the html_options hash it will be applied to every select tag in the set.
0
- # # Generates a datetime select that, when POSTed, will be stored in the post variable in the written_on attribute
0
+ # # Generates a datetime select that, when POSTed, will be stored in the post variable in the written_on
0
# datetime_select("post", "written_on")
0
# # Generates a datetime select with a year select that starts at 1995 that, when POSTed, will be stored in the
0
@@ -230,12 +246,12 @@ module ActionView
0
InstanceTag.new(object_name, method, self, options.delete(:object)).to_datetime_select_tag(options, html_options)
0
- # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the +datetime+.
0
- # It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
0
- # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol,
0
- # it will be appended onto the <tt>:order</tt> passed in. You can also add <tt>:date_separator</tt>,
0
- # <tt>:datetime_separator</tt> and <tt>:time_separator</tt> keys to the +options+ to control visual display of
0
+ # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the
0
+ # +datetime+. It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with
0
+ # an array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not
0
+ # supply a Symbol, it will be appended onto the <tt>:order</tt> passed in. You can also add
0
+ # <tt>:date_separator</tt>, <tt>:datetime_separator</tt> and <tt>:time_separator</tt> keys to the +options+ to
0
+ # control visual display of the elements.
0
# If anything is passed in the html_options hash it will be applied to every select tag in the set.
0
@@ -270,14 +286,13 @@ module ActionView
0
# select_datetime(my_date_time, :prefix => 'payday')
0
def select_datetime(datetime = Time.current, options = {}, html_options = {})
0
- separator = options[:datetime_separator] || ''
0
- select_date(datetime, options, html_options) + separator + select_time(datetime, options, html_options)
0
+ DateTimeSelector.new(datetime, options, html_options).select_datetime
0
# Returns a set of html select-tags (one for year, month, and day) pre-selected with the +date+.
0
# It's possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
0
- # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol, it
0
- # will be appended onto the <tt>:order</tt> passed in.
0
+ # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol,
0
+ # it will be appended onto the <tt>:order</tt> passed in.
0
# If anything is passed in the html_options hash it will be applied to every select tag in the set.
0
@@ -307,12 +322,7 @@ module ActionView
0
# select_date(my_date, :prefix => 'payday')
0
def select_date(date = Date.current, options = {}, html_options = {})
0
- options.reverse_merge!(:order => [], :date_separator => '')
0
- [:year, :month, :day].each { |o| options[:order].push(o) unless options[:order].include?(o) }
0
- options[:order].inject([]) { |s, o|
0
- s << self.send("select_#{o}", date, options, html_options)
0
- }.join(options[:date_separator])
0
+ DateTimeSelector.new(date, options, html_options).select_date
0
# Returns a set of html select-tags (one for hour and minute)
0
@@ -343,9 +353,7 @@ module ActionView
0
# select_time(my_time, :time_separator => ':', :include_seconds => true)
0
def select_time(datetime = Time.current, options = {}, html_options = {})
0
- separator = options[:time_separator] || ''
0
- select_hour(datetime, options, html_options) + separator + select_minute(datetime, options, html_options) +
0
- (options[:include_seconds] ? separator + select_second(datetime, options, html_options) : '')
0
+ DateTimeSelector.new(datetime, options, html_options).select_time
0
# Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
0
@@ -366,15 +374,12 @@ module ActionView
0
# select_second(my_time, :field_name => 'interval')
0
def select_second(datetime, options = {}, html_options = {})
0
- val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.sec) : ''
0
- options[:use_hidden] ?
0
- (options[:include_seconds] ? _date_hidden_html(options[:field_name] || 'second', val, options) : '') :
0
- _date_select_html(options[:field_name] || 'second', _date_build_options(val), options, html_options)
0
+ DateTimeSelector.new(datetime, options, html_options).select_second
0
# Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.
0
- # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute selected
0
- # The <tt>minute</tt> can also be substituted for a minute number.
0
+ # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute
0
+ # selected. The <tt>minute</tt> can also be substituted for a minute number.
0
# Override the field name using the <tt>:field_name</tt> option, 'minute' by default.
0
@@ -391,11 +396,7 @@ module ActionView
0
# select_minute(my_time, :field_name => 'stride')
0
def select_minute(datetime, options = {}, html_options = {})
0
- val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.min) : ''
0
- options[:use_hidden] ?
0
- _date_hidden_html(options[:field_name] || 'minute', val, options) :
0
- _date_select_html(options[:field_name] || 'minute',
0
- _date_build_options(val, :step => options[:minute_step]), options, html_options)
0
+ DateTimeSelector.new(datetime, options, html_options).select_minute
0
# Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
0
@@ -416,9 +417,7 @@ module ActionView
0
# select_minute(my_time, :field_name => 'stride')
0
def select_hour(datetime, options = {}, html_options = {})
0
- val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
0
- options[:use_hidden] ? _date_hidden_html(options[:field_name] || 'hour', val, options) :
0
- _date_select_html(options[:field_name] || 'hour', _date_build_options(val, :end => 23), options, html_options)
0
+ DateTimeSelector.new(datetime, options, html_options).select_hour
0
# Returns a select tag with options for each of the days 1 through 31 with the current day selected.
0
@@ -439,11 +438,7 @@ module ActionView
0
# select_day(my_time, :field_name => 'due')
0
def select_day(date, options = {}, html_options = {})
0
- val = date ? (date.kind_of?(Fixnum) ? date : date.day) : ''
0
- options[:use_hidden] ? _date_hidden_html(options[:field_name] || 'day', val, options) :
0
- _date_select_html(options[:field_name] || 'day',
0
- _date_build_options(val, :start => 1, :end => 31, :leading_zeros => false),
0
- options, html_options)
0
+ DateTimeSelector.new(date, options, html_options).select_day
0
# Returns a select tag with options for each of the months January through December with the current month
0
@@ -481,36 +476,7 @@ module ActionView
0
# select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...))
0
def select_month(date, options = {}, html_options = {})
0
- locale = options[:locale]
0
- val = date ? (date.kind_of?(Fixnum) ? date : date.month) : ''
0
- if options[:use_hidden]
0
- _date_hidden_html(options[:field_name] || 'month', val, options)
0
- month_names = options[:use_month_names] || begin
0
- key = options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
0
- I18n.translate key, :locale => locale
0
- month_names.unshift(nil) if month_names.size < 13
0
- 1.upto(12) do |month_number|
0
- month_name = if options[:use_month_numbers]
0
- elsif options[:add_month_numbers]
0
- month_number.to_s + ' - ' + month_names[month_number]
0
- month_names[month_number]
0
- month_options << ((val == month_number) ?
0
- content_tag(:option, month_name, :value => month_number, :selected => "selected") :
0
- content_tag(:option, month_name, :value => month_number)
0
- _date_select_html(options[:field_name] || 'month', month_options.join, options, html_options)
0
+ DateTimeSelector.new(date, options, html_options).select_month
0
# Returns a select tag with options for each of the five years on each side of the current, which is selected.
0
@@ -537,158 +503,369 @@ module ActionView
0
# select_year(2006, :start_year => 2000, :end_year => 2010)
0
def select_year(date, options = {}, html_options = {})
0
+ DateTimeSelector.new(date, options, html_options).select_year
0
+ class DateTimeSelector #:nodoc:
0
+ extend ActiveSupport::Memoizable
0
+ include ActionView::Helpers::TagHelper
0
+ DEFAULT_PREFIX = 'date'.freeze unless const_defined?('DEFAULT_PREFIX')
0
+ :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6
0
+ }.freeze unless const_defined?('POSITION')
0
+ def initialize(datetime, options = {}, html_options = {})
0
+ @options = options.dup
0
+ @html_options = html_options.dup
0
+ # TODO: Remove tag conditional
0
+ # Ideally we could just join select_date and select_date for the tag case
0
+ if @options[:tag] && @options[:ignore_date]
0
+ order = date_order.dup
0
+ order -= [:hour, :minute, :second]
0
+ @options[:discard_year] ||= true unless order.include?(:year)
0
+ @options[:discard_month] ||= true unless order.include?(:month)
0
+ @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day)
0
+ @options[:discard_minute] ||= true if @options[:discard_hour]
0
+ @options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute]
0
+ # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
0
+ # valid (otherwise it could be 31 and february wouldn't be a valid date)
0
+ if @options[:discard_day] && !@options[:discard_month]
0
+ @datetime = @datetime.change(:day => 1)
0
+ [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
0
+ order += [:hour, :minute, :second] unless @options[:discard_hour]
0
+ build_selects_from_types(order)
0
+ "#{select_date}#{@options[:datetime_separator]}#{select_time}"
0
+ order = date_order.dup
0
+ # TODO: Remove tag conditional
0
+ @options[:discard_hour] = true
0
+ @options[:discard_minute] = true
0
+ @options[:discard_second] = true
0
+ @options[:discard_year] ||= true unless order.include?(:year)
0
+ @options[:discard_month] ||= true unless order.include?(:month)
0
+ @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day)
0
+ # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
0
+ # valid (otherwise it could be 31 and february wouldn't be a valid date)
0
+ if @options[:discard_day] && !@options[:discard_month]
0
+ @datetime = @datetime.change(:day => 1)
0
+ [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
0
+ build_selects_from_types(order)
0
+ # TODO: Remove tag conditional
0
+ @options[:discard_month] = true
0
+ @options[:discard_year] = true
0
+ @options[:discard_day] = true
0
+ @options[:discard_second] ||= true unless @options[:include_seconds]
0
+ order += [:year, :month, :day] unless @options[:ignore_date]
0
+ order += [:hour, :minute]
0
+ order << :second if @options[:include_seconds]
0
+ build_selects_from_types(order)
0
+ if @options[:use_hidden] || @options[:discard_second]
0
+ build_hidden(:second, sec) if @options[:include_seconds]
0
+ build_options_and_select(:second, sec)
0
+ if @options[:use_hidden] || @options[:discard_minute]
0
+ build_hidden(:minute, min)
0
+ build_options_and_select(:minute, min, :step => @options[:minute_step])
0
+ if @options[:use_hidden] || @options[:discard_hour]
0
+ build_hidden(:hour, hour)
0
+ build_options_and_select(:hour, hour, :end => 23)
0
+ if @options[:use_hidden] || @options[:discard_day]
0
+ build_hidden(:day, day)
0
+ build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => false)
0
+ if @options[:use_hidden] || @options[:discard_month]
0
+ build_hidden(:month, month)
0
+ 1.upto(12) do |month_number|
0
+ options = { :value => month_number }
0
+ options[:selected] = "selected" if month == month_number
0
+ month_options << content_tag(:option, month_name(month_number), options) + "\n"
0
+ build_select(:month, month_options.join)
0
+ if !@datetime || @datetime == 0
0
middle_year = Date.today.year
0
- elsif date.kind_of?(Fixnum)
0
- val = middle_year = date
0
- val = middle_year =
date.year
0
+ val = middle_year =
year
0
- if options[:use_hidden]
0
- _date_hidden_html(options[:field_name] || 'year', val, options)
0
+ if @options[:use_hidden] || @options[:discard_year]
0
+ build_hidden(:year, val)
0
- options[:start_year] ||= middle_year - 5
0
- options[:end_year] ||= middle_year + 5
0
- step = options[:start_year] < options[:end_year] ? 1 : -1
0
- _date_select_html(options[:field_name] || 'year',
0
- _date_build_options(val,
0
- :start => options[:start_year],
0
- :end => options[:end_year],
0
- :leading_zeros => false
0
- ), options, html_options)
0
+ options[:start] = @options[:start_year] || middle_year - 5
0
+ options[:end] = @options[:end_year] || middle_year + 5
0
+ options[:step] = options[:start] < options[:end] ? 1 : -1
0
+ options[:leading_zeros] = false
0
+ build_options_and_select(:year, val, options)
0
- def _date_build_options(selected, options={})
0
- options.reverse_merge!(:start => 0, :end => 59, :step => 1, :leading_zeros => true)
0
+ %w( sec min hour day month year ).each do |method|
0
+ define_method(method) do
0
+ @datetime.kind_of?(Fixnum) ? @datetime : @datetime.send(method) if @datetime
0
+ # Returns translated month names, but also ensures that a custom month
0
+ # name array has a leading nil element
0
+ month_names = @options[:use_month_names] || translated_month_names
0
+ month_names.unshift(nil) if month_names.size < 13
0
+ # Returns translated month names
0
+ # => [nil, "January", "February", "March",
0
+ # "April", "May", "June", "July",
0
+ # "August", "September", "October",
0
+ # "November", "December"]
0
+ # If :use_short_month option is set
0
+ # => [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
0
+ # "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
0
+ def translated_month_names
0
+ key = @options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
0
+ I18n.translate(key, :locale => @options[:locale])
0
+ # Lookup month name for number
0
+ # month_name(1) => "January"
0
+ # If :use_month_numbers option is passed
0
+ # If :add_month_numbers option is passed
0
+ # month_name(1) => "1 - January"
0
+ def month_name(number)
0
+ if @options[:use_month_numbers]
0
+ elsif @options[:add_month_numbers]
0
+ "#{number} - #{month_names[number]}"
0
+ @options[:order] || translated_date_order
0
+ def translated_date_order
0
+ I18n.translate(:'date.order', :locale => @options[:locale]) || []
0
+ # Build full select tag from date type and options
0
+ def build_options_and_select(type, selected, options = {})
0
+ build_select(type, build_options(selected, options))
0
+ # Build select option html from date value and options
0
+ # build_options(15, :start => 1, :end => 31)
0
+ # => "<option value="1">1</option>
0
+ # <option value=\"2\">2</option>
0
+ # <option value=\"3\">3</option>..."
0
+ def build_options(selected, options = {})
0
+ start = options.delete(:start) || 0
0
+ stop = options.delete(:end) || 59
0
+ step = options.delete(:step) || 1
0
+ leading_zeros = options.delete(:leading_zeros).nil? ? true : false
0
- (options[:start] || 0).step((options[:end] || 59), options[:step] || 1) do |i|
0
- value = options[:leading_zeros] ? sprintf("%02d", i) : i
0
+ start.step(stop, step) do |i|
0
+ value = leading_zeros ? sprintf("%02d", i) : i
0
tag_options = { :value => value }
0
tag_options[:selected] = "selected" if selected == i
0
select_options << content_tag(:option, value, tag_options)
0
select_options.join("\n") + "\n"
0
- def _date_select_html(type, html_options, options, select_tag_options = {})
0
- _date_name_and_id_from_options(options, type)
0
- select_options = {:id => options[:id], :name => options[:name]}
0
- select_options.merge!(:disabled => 'disabled') if options[:disabled]
0
- select_options.merge!(select_tag_options) unless select_tag_options.empty?
0
+ # Builds select tag from date type and html select options
0
+ # build_select(:month, "<option value="1">January</option>...")
0
+ # => "<select id="post_written_on_2i" name="post[written_on(2i)]">
0
+ # <option value="1">January</option>...
0
+ def build_select(type, select_options_as_html)
0
+ :id => input_id_from_type(type),
0
+ :name => input_name_from_type(type)
0
+ }.merge(@html_options)
0
+ select_options.merge!(:disabled => 'disabled') if @options[:disabled]
0
- select_html << content_tag(:option, '', :value => '') + "\n" if options[:include_blank]
0
- select_html << html_options.to_s
0
+ select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
0
+ select_html << select_options_as_html.to_s
0
content_tag(:select, select_html, select_options) + "\n"
0
- def _date_hidden_html(type, value, options)
0
- _date_name_and_id_from_options(options, type)
0
- hidden_html = tag(:input, :type => "hidden", :id => options[:id], :name => options[:name], :value => value) + "\n"
0
+ # Builds hidden input tag for date part and value
0
+ # build_hidden(:year, 2008)
0
+ # => "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />"
0
+ def build_hidden(type, value)
0
+ :id => input_id_from_type(type),
0
+ :name => input_name_from_type(type),
0
- def _date_name_and_id_from_options(options, type)
0
- options[:name] = (options[:prefix] || DEFAULT_PREFIX) + (options[:discard_type] ? '' : "[#{type}]")
0
- options[:id] = options[:name].gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
0
+ # Returns the name attribute for the input tag
0
+ # => post[written_on(1i)]
0
+ def input_name_from_type(type)
0
+ prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
0
+ prefix += "[#{@options[:index]}]" if @options[:index]
0
+ field_name = @options[:field_name] || type
0
+ if @options[:include_position]
0
+ field_name += "(#{ActionView::Helpers::DateTimeSelector::POSITION[type]}i)"
0
+ @options[:discard_type] ? prefix : "#{prefix}[#{field_name}]"
0
+ # Returns the id attribute for the input tag
0
+ # => "post_written_on_1i"
0
+ def input_id_from_type(type)
0
+ input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
0
+ # Given an ordering of datetime components, create the selection html
0
+ # and join them with their appropriate seperators
0
+ def build_selects_from_types(order)
0
+ order.reverse.each do |type|
0
+ separator = separator(type) unless type == order.first # don't add on last field
0
+ select.insert(0, separator.to_s + send("select_#{type}").to_s)
0
+ # Returns the separator for a given datetime component
0
+ @options[:date_separator]
0
+ (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
0
+ @options[:time_separator]
0
+ @options[:include_seconds] ? @options[:time_separator] : ""
0
class InstanceTag #:nodoc:
0
def to_date_select_tag(options = {}, html_options = {})
0
- date
_or_time_select(options.merge(:discard_hour => true), html_options)0
+ date
time_selector(options, html_options).select_date0
def to_time_select_tag(options = {}, html_options = {})
0
- date
_or_time_select(options.merge(:discard_year => true, :discard_month => true), html_options)0
+ date
time_selector(options, html_options).select_time0
def to_datetime_select_tag(options = {}, html_options = {})
0
- date
_or_time_select(options, html_options)0
+ date
time_selector(options, html_options).select_datetime0
- def date_or_time_select(options, html_options = {})
0
- locale = options[:locale]
0
- defaults = { :discard_type => true }
0
- options = defaults.merge(options)
0
- datetime = value(object)
0
- datetime ||= default_time_from_options(options[:default]) unless options[:include_blank]
0
- position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
0
- order = options[:order] ||= I18n.translate(:'date.order', :locale => locale)
0
- # Discard explicit and implicit by not being included in the :order
0
- discard[:year] = true if options[:discard_year] or !order.include?(:year)
0
- discard[:month] = true if options[:discard_month] or !order.include?(:month)
0
- discard[:day] = true if options[:discard_day] or discard[:month] or !order.include?(:day)
0
- discard[:hour] = true if options[:discard_hour]
0
- discard[:minute] = true if options[:discard_minute] or discard[:hour]
0
- discard[:second] = true unless options[:include_seconds] && !discard[:minute]
0
- # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are valid
0
- # (otherwise it could be 31 and february wouldn't be a valid date)
0
- if datetime && discard[:day] && !discard[:month]
0
- datetime = datetime.change(:day => 1)
0
- # Maintain valid dates by including hidden fields for discarded elements
0
- [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
0
- # Ensure proper ordering of :hour, :minute and :second
0
- [:hour, :minute, :second].each { |o| order.delete(o); order.push(o) }
0
- date_or_time_select = ''
0
- order.reverse.each do |param|
0
- # Send hidden fields for discarded elements once output has started
0
- # This ensures AR can reconstruct valid dates using ParseDate
0
- next if discard[param] && (date_or_time_select.empty? || options[:ignore_date])
0
- date_or_time_select.insert(0,
0
- self.send("select_#{param}",
0
- options_with_prefix(position[param], options.merge(:use_hidden => discard[param])),
0
- date_or_time_select.insert(0,
0
- when :hour then (discard[:year] && discard[:day] ? "" : " — ")
0
- when :minute then " : "
0
- when :second then options[:include_seconds] ? " : " : ""
0
+ def datetime_selector(options, html_options)
0
+ datetime = value(object) || default_datetime(options)
0
+ options[:field_name] = @method_name
0
+ options[:include_position] = true
0
+ options[:prefix] ||= @object_name
0
+ options[:index] ||= @auto_index
0
+ options[:datetime_separator] ||= ' — '
0
+ options[:time_separator] ||= ' : '
0
+ DateTimeSelector.new(datetime, options.merge(:tag => true), html_options)
0
- def options_with_prefix(position, options)
0
- prefix = "#{@object_name}"
0
- prefix << "[#{options[:index]}]"
0
- prefix << "[#{@auto_index}]"
0
- options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]")
0
+ def default_datetime(options)
0
+ return if options[:include_blank]
0
- def default_time_from_options(default)
0
+ case options[:default]
0
+ default = options[:default].dup
0
# Rename :minute and :second to :min and :sec
0
default[:min] ||= default[:minute]
0
default[:sec] ||= default[:second]
0
@@ -699,8 +876,11 @@ module ActionView
0
default[key] ||= time.send(key)
0
- Time.utc_time(default[:year], default[:month], default[:day], default[:hour], default[:min], default[:sec])
0
+ default[:year], default[:month], default[:day],
0
+ default[:hour], default[:min], default[:sec]