lib/date_time_text_field_helpers/instance_tag.rb
# Adapted from the Rails source date_or_time_select method to use text fields
# instead of selects and adds a few extra options
Adapted from the Rails source date_or_time_select method to use text fields
instead of selects and adds a few extra options
Adapted from the Rails source date_or_time_select method to use text fields instead of selects and adds a few extra options
def date_or_time_text_field(options)
      defaults = { :discard_type => true, 
                   :date_separator => '-',
                   :time_separator => ':',
                   :date_time_separator => '&mdash;', 
                   :class => 'date_time_field' }
      options  = defaults.merge(options)
      datetime = value(object)
      datetime ||= default_datetime(options) unless options[:blank]
      position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
      order = (options[:order] ||= [:year, :month, :day])
      discard = {}
      discard[:year]   = true if options[:discard_year] or !order.include?(:year)
      discard[:month]  = true if options[:discard_month] or !order.include?(:month)
      discard[:day]    = true if options[:discard_day] or discard[:month] or !order.include?(:day)
      discard[:hour]   = true if options[:discard_hour]
      discard[:minute] = true if options[:discard_minute] or discard[:hour]
      discard[:second] = true unless options[:include_seconds] && !discard[:minute]
      if datetime && discard[:day] && !discard[:month]
        datetime = datetime.change(:day => 1)
      end
      [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
      [:hour, :minute, :second].each { |o| order.delete(o); order.push(o) }
      date_or_time_text_field = ''
      order.reverse.each do |param|
        next if discard[param] && date_or_time_text_field.empty?
        date_or_time_text_field.insert(0, self.send(:text_field_for_date_part, param, datetime, options_with_prefix(position[param], options.merge(:use_hidden => discard[param]))))
        date_or_time_text_field.insert(0,
          case param
            when :year, :month, :day then (discard[param] || order.index(param) == 0)  ? '' : " #{options[:date_separator]} "
            when :hour then (discard[:year] && discard[:day] ? "" : " #{options[:date_time_separator]} ")
            when :minute then " #{options[:time_separator]} "
            when :second then options[:include_seconds] ? " #{options[:time_separator]} " : ""          
            else ''
          end)
      end
      content_tag(:span, date_or_time_text_field, :class => options[:class], :id => "#{@object_name}_#{@method_name}")
    end
def date_or_time_text_field ( options ) defaults = { :discard_type => true , :date_separator => '-' , :time_separator => ':' , :date_time_separator => '&mdash;' , :class => 'date_time_field' } options = defaults . merge ( options ) datetime = value ( object ) datetime ||= default_datetime ( options ) unless options [ :blank ] position = { :year => 1 , :month => 2 , :day => 3 , :hour => 4 , :minute => 5 , :second => 6 } order = ( options [ :order ] ||= [ :year , :month , :day ] ) discard = { } discard [ :year ] = true if options [ :discard_year ] or ! order . include? ( :year ) discard [ :month ] = true if options [ :discard_month ] or ! order . include? ( :month ) discard [ :day ] = true if options [ :discard_day ] or discard [ :month ] or ! order . include? ( :day ) discard [ :hour ] = true if options [ :discard_hour ] discard [ :minute ] = true if options [ :discard_minute ] or discard [ :hour ] discard [ :second ] = true unless options [ :include_seconds ] && ! discard [ :minute ] if datetime && discard [ :day ] && ! discard [ :month ] datetime = datetime . change ( :day => 1 ) end [ :day , :month , :year ] . each { | o | order . unshift ( o ) unless order . include? ( o ) } [ :hour , :minute , :second ] . each { | o | order . delete ( o ) ; order . push ( o ) } date_or_time_text_field = '' order . reverse . each do | param | next if discard [ param ] && date_or_time_text_field . empty? date_or_time_text_field . insert ( 0 , self . send ( :text_field_for_date_part , param , datetime , options_with_prefix ( position [ param ] , options . merge ( :use_hidden => discard [ param ] ) ) ) ) date_or_time_text_field . insert ( 0 , case param when :year , :month , :day then ( discard [ param ] || order . index ( param ) == 0 ) ? '' : " #{options[:date_separator]} " when :hour then ( discard [ :year ] && discard [ :day ] ? "" : " #{options[:date_time_separator]} " ) when :minute then " #{options[:time_separator]} " when :second then options [ :include_seconds ] ? " #{options[:time_separator]} " : "" else '' end ) end content_tag ( :span , date_or_time_text_field , :class => options [ :class ] , :id => "#{@object_name}_#{@method_name}" ) end
Adapted from the Rails source date_or_time_select method to use text fields
instead of selects and adds a few extra options
Adapted from the Rails source date_or_time_select method to use text fields instead of selects and adds a few extra options
# Discard explicit and implicit by not being included in the :order # 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) # Maintain valid dates by including hidden fields for discarded elements # Ensure proper ordering of :hour, :minute and :second # Send hidden fields for discarded elements once output has started # This ensures AR can reconstruct valid dates using ParseDate
options
<not_specific>