diff options
Diffstat (limited to 'actionpack/lib')
-rwxr-xr-x | actionpack/lib/action_view/helpers/date_helper.rb | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index d33a576214..9806d51a87 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -90,6 +90,8 @@ module ActionView # set the order of the tags using the <tt>:order</tt> option with an array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in # the desired order. Symbols may be omitted and the respective select is not included. # + # Pass the <tt>:default</tt> option to set the default date. Use a Time object or a Hash of :year, :month, :day, :hour, :minute, and :second. + # # Passing :disabled => true as part of the +options+ will make elements inaccessible for change. # # NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed. @@ -103,6 +105,9 @@ module ActionView # date_select("post", "written_on", :order => [:day, :month, :year]) # date_select("user", "birthday", :order => [:month, :day]) # + # date_select("post", "written_on", :default => 3.days.from_now) + # date_select("credit_card", "bill_due", :default => { :day => 20 }) + # # The selects are prepared for multi-parameter assignment to an Active Record object. def date_select(object_name, method, options = {}) InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_date_select_tag(options) @@ -361,7 +366,7 @@ module ActionView defaults = { :discard_type => true } options = defaults.merge(options) datetime = value(object) - datetime ||= Time.now unless options[:include_blank] + datetime ||= default_time_from_options(options[:default]) unless options[:include_blank] position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 } @@ -410,6 +415,26 @@ module ActionView end options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]") end + + def default_time_from_options(default) + case default + when nil + Time.now + when Date, Time + default + else + # Rename :minute and :second to :min and :sec + default[:min] ||= default[:minute] + default[:sec] ||= default[:second] + + [:year, :month, :day, :hour, :min, :sec].each do |key| + default[key] ||= Time.now.send(key) + end + + Time.mktime(default[:year], default[:month], default[:day], + default[:hour], default[:min], default[:sec]) + end + end end class FormBuilder |