From 1fecaf15d779151defecf3eadda2b4524f7f5703 Mon Sep 17 00:00:00 2001 From: Colin Burn-Murdoch Date: Mon, 14 Jan 2013 17:55:45 +0000 Subject: Rename :value option to :selected, in line with other select helpers Add tests for time & datetime. Add documentation. --- actionpack/CHANGELOG.md | 2 +- actionpack/lib/action_view/helpers/date_helper.rb | 5 +++ .../lib/action_view/helpers/tags/date_select.rb | 2 +- actionpack/test/template/date_helper_test.rb | 52 +++++++++++++++++++++- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index a8325b86fc..08892ead67 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,6 +1,6 @@ ## Rails 4.0.0 (unreleased) ## -* Allow value to be set on date_select tag helper. +* Allow `:selected` to be set on `date_select` tag helper. *Colin Burn-Murdoch* diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 1fbf61a5a9..cf978d8e83 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -193,6 +193,7 @@ module ActionView # * :include_blank - Include a blank option in every select field so it's possible to set empty # dates. # * :default - Set a default date if the affected date isn't set or is nil. + # * :selected - Set a date that overrides the actual value. # * :disabled - Set to true if you want show the select fields as disabled. # * :prompt - Set to true (for a generic prompt), a prompt string or a hash of prompt strings # for :year, :month, :day, :hour, :minute and :second. @@ -234,6 +235,10 @@ module ActionView # # which is initially set to the date 3 days from the current date # date_select("article", "written_on", default: 3.days.from_now) # + # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute + # # which is set in the form with todays date, regardless of the value in the Active Record object. + # date_select("article", "written_on", selected: Date.today) + # # # Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute # # that will have a default day of 20. # date_select("credit_card", "bill_due", default: { day: 20 }) diff --git a/actionpack/lib/action_view/helpers/tags/date_select.rb b/actionpack/lib/action_view/helpers/tags/date_select.rb index f073c63f73..380d6d3686 100644 --- a/actionpack/lib/action_view/helpers/tags/date_select.rb +++ b/actionpack/lib/action_view/helpers/tags/date_select.rb @@ -27,7 +27,7 @@ module ActionView end def datetime_selector(options, html_options) - datetime = options[:value] || value(object) || default_datetime(options) + datetime = options[:selected] || value(object) || default_datetime(options) @auto_index ||= nil options = options.dup diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 0ab5c15131..d622392caf 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1511,7 +1511,7 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on") end - def test_date_select_with_value + def test_date_select_with_selected @post = Post.new @post.written_on = Date.new(2004, 6, 15) @@ -1528,7 +1528,7 @@ class DateHelperTest < ActionView::TestCase expected << "\n" - assert_dom_equal expected, date_select("post", "written_on", :value => '2004-07-10'.to_date) + assert_dom_equal expected, date_select("post", "written_on", :selected => Date.new(2004, 07, 10)) end @@ -1989,6 +1989,25 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, time_select("post", "written_on") end + def test_time_select_with_selected + @post = Post.new + @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) + + expected = %{\n} + expected << %{\n} + expected << %{\n} + + expected << %(\n" + expected << " : " + expected << %(\n" + + assert_dom_equal expected, time_select("post", "written_on", :selected => Time.local(2004, 6, 15, 12, 20, 30)) + end + def test_time_select_without_date_hidden_fields @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) @@ -2186,6 +2205,35 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, datetime_select("post", "updated_at") end + def test_datetime_select_with_selected + @post = Post.new + @post.updated_at = Time.local(2004, 6, 15, 16, 35) + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_dom_equal expected, datetime_select("post", "updated_at", :selected => Time.local(2004, 3, 10, 12, 30)) + end + def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set # The love zone is UTC+0 mytz = Class.new(ActiveSupport::TimeZone) { -- cgit v1.2.3