From d6d94c7377b60aac92f3718cafc4b3e6c3852fe3 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 28 Jan 2007 16:44:44 +0000 Subject: [PATCH] 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 --- actionpack/CHANGELOG | 4 + .../lib/action_view/helpers/date_helper.rb | 27 ++++++- actionpack/test/template/date_helper_test.rb | 74 +++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 0f34c678fd..aefa0678b4 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* date_select and datetime_select take a :default option. #7052 [nik.wakelin] + date_select "post", "written_on", :default => 3.days.from_now + date_select "credit_card", "bill_due", :default => { :day => 20 } + * select :multiple => true suffixes the attribute name with [] unless already suffixed. #6977 [nik.kakelin, ben, julik] * Improve routes documentation. #7095 [zackchandler] 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 :order option with an array of symbols :year, :month and :day in # the desired order. Symbols may be omitted and the respective select is not included. # + # Pass the :default 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 diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 07b0fa9c09..340175b9c9 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1318,4 +1318,78 @@ class DateHelperTest < Test::Unit::TestCase assert_equal expected, datetime_select("post", "updated_at", :order => [:day, :month]) end + + def test_datetime_select_with_default_value_as_time + @post = Post.new + @post.updated_at = nil + + expected = %{\n" + expected << %{\n" + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_equal expected, datetime_select("post", "updated_at", :default => Time.local(2006, 9, 19, 15, 16, 35)) + end + + def test_include_blank_overrides_default_option + @post = Post.new + @post.updated_at = nil + + expected = %{\n" + expected << %{\n" + expected << %{\n" + + assert_equal expected, date_select("post", "updated_at", :default => Time.local(2006, 9, 19, 15, 16, 35), :include_blank => true) + end + + def test_datetime_select_with_default_value_as_hash + @post = Post.new + @post.updated_at = nil + + expected = %{\n" + expected << %{\n" + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_equal expected, datetime_select("post", "updated_at", :default => { :month => 10, :minute => 42, :hour => 9 }) + end end