diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-01-28 16:44:44 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-01-28 16:44:44 +0000 |
commit | d6d94c7377b60aac92f3718cafc4b3e6c3852fe3 (patch) | |
tree | c31b053ed5337f734b2714246aa9e2a42345719e /actionpack/lib/action_view | |
parent | ad29870c21f2f196858251831f527e52a7ef27cb (diff) | |
download | rails-d6d94c7377b60aac92f3718cafc4b3e6c3852fe3.tar.gz rails-d6d94c7377b60aac92f3718cafc4b3e6c3852fe3.tar.bz2 rails-d6d94c7377b60aac92f3718cafc4b3e6c3852fe3.zip |
date_select and datetime_select take a :default option. Closes #7052.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6080 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view')
-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 |