class ActionView::Helpers::DateTimeSelector
:nodoc:
def build_hidden(type, value)
build_hidden(:year, 2008)
Builds hidden input tag for date part and value
def build_hidden(type, value) (tag(:input, { :type => "hidden", :id => input_id_from_type(type), :name => input_name_from_type(type), :value => value }.merge(@html_options.slice(:disabled))) + "\n").html_safe end
def build_options(selected, options = {})
=> "
build_options(15, :start => 1, :end => 31)
Build select option html from date value and options
def build_options(selected, options = {}) start = options.delete(:start) || 0 stop = options.delete(:end) || 59 step = options.delete(:step) || 1 options.reverse_merge!({:leading_zeros => true}) 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 select_options << content_tag(:option, value, tag_options) end (select_options.join("\n") + "\n").html_safe end
def build_options_and_select(type, selected, options = {})
def build_options_and_select(type, selected, options = {}) build_select(type, build_options(selected, options)) end
def build_select(type, select_options_as_html)
...
=> "
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.merge!(:disabled => 'disabled') if @options[:disabled] select_html = "\n" select_html << content_tag(:option, '', :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, select_html.html_safe, select_options) + "\n").html_safe end
def build_selects_from_types(order)
Given an ordering of datetime components, create the selection HTML
def build_selects_from_types(order) select = '' order.reverse.each do |type| separator = separator(type) unless type == order.first # don't add on last field select.insert(0, separator.to_s + send("select_#{type}").to_s) end select.html_safe end
def date_order
def date_order @options[:order] || translated_date_order end
def initialize(datetime, options = {}, html_options = {})
def initialize(datetime, options = {}, html_options = {}) @options = options.dup @html_options = html_options.dup @datetime = datetime @options[:datetime_separator] ||= ' — ' @options[:time_separator] ||= ' : ' end
def input_id_from_type(type)
Returns the id attribute for the input tag
def input_id_from_type(type) input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '') end
def input_name_from_type(type)
Returns the name attribute for the input tag
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 if @options[:include_position] field_name += "(#{ActionView::Helpers::DateTimeSelector::POSITION[type]}i)" end @options[:discard_type] ? prefix : "#{prefix}[#{field_name}]" end
def month_name(number)
If :add_month_numbers option is passed
month_name(1) => 1
If :use_month_numbers option is passed
month_name(1) => "January"
Lookup month name for number
def month_name(number) if @options[:use_month_numbers] number elsif @options[:add_month_numbers] "#{number} - #{month_names[number]}" else month_names[number] end end
def month_names
Returns translated month names, but also ensures that a custom month
def month_names month_names = @options[:use_month_names] || translated_month_names month_names.unshift(nil) if month_names.size < 13 month_names end
def prompt_option_tag(type, options)
prompt_option_tag(:month, :prompt => 'Select month')
Builds a prompt option tag with supplied options or from default options
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, prompt, :value => '') : '' end
def select_date
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) # 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) } build_selects_from_types(order) end
def select_datetime
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] # 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 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
def select_day
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
def select_hour
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
def select_minute
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
def select_month
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 << content_tag(:option, month_name(month_number), options) + "\n" end build_select(:month, month_options.join) end end
def select_second
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
def select_time
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
def select_year
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
def separator(type)
def separator(type) case type when :month @options[:discard_month] ? "" : @options[:date_separator] when :day @options[:discard_day] ? "" : @options[:date_separator] when :hour (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator] when :minute @options[:discard_minute] ? "" : @options[:time_separator] when :second @options[:include_seconds] ? @options[:time_separator] : "" end end
def translated_date_order
def translated_date_order I18n.translate(:'date.order', :locale => @options[:locale]) || [] end
def translated_month_names
=> [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
If :use_short_month option is set
"November", "December"]
"August", "September", "October",
"April", "May", "June", "July",
=> [nil, "January", "February", "March",
Returns translated month names
def translated_month_names key = @options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names' I18n.translate(key, :locale => @options[:locale]) end