From 462698b2c704eff962060ed5851af80f4215958b Mon Sep 17 00:00:00 2001 From: Lecky Lao Date: Tue, 22 Jul 2014 16:03:55 +1000 Subject: making selected value to accept Hash like the default option. E.g. selected: {day: params[:day].to_i, month: params[:month].to_id} Adds in test test_date_select_with_selected_in_hash and change log fixes typo in CHANGELOG --- actionview/CHANGELOG.md | 4 ++++ actionview/lib/action_view/helpers/date_helper.rb | 10 +++++++++- actionview/test/template/date_helper_test.rb | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index c010b7ce91..b37a275fe0 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -208,4 +208,8 @@ *Daniel Gomez de Souza* +* Allow date_select helper selected option to accept hash like the default options. + + *Lecky Lao* + Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/actionview/CHANGELOG.md) for previous changes. diff --git a/actionview/lib/action_view/helpers/date_helper.rb b/actionview/lib/action_view/helpers/date_helper.rb index 312e41ee48..c69e4cc77e 100644 --- a/actionview/lib/action_view/helpers/date_helper.rb +++ b/actionview/lib/action_view/helpers/date_helper.rb @@ -845,7 +845,15 @@ module ActionView private %w( sec min hour day month year ).each do |method| define_method(method) do - @datetime.kind_of?(Numeric) ? @datetime : @datetime.send(method) if @datetime + if @datetime + if @datetime.kind_of?(Hash) + @datetime[method.to_sym] + elsif @datetime.kind_of?(Numeric) + @datetime + else + @datetime.send(method) + end + end end end diff --git a/actionview/test/template/date_helper_test.rb b/actionview/test/template/date_helper_test.rb index 9212420ec9..c4234a71c3 100644 --- a/actionview/test/template/date_helper_test.rb +++ b/actionview/test/template/date_helper_test.rb @@ -1559,6 +1559,26 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on", :selected => Date.new(2004, 07, 10)) end + def test_date_select_with_selected_in_hash + @post = Post.new + @post.written_on = Date.new(2004, 6, 15) + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + assert_dom_equal expected, date_select("post", "written_on", :selected => {day: 10, month: 07, year: 2004}) + end + def test_date_select_with_selected_nil @post = Post.new @post.written_on = Date.new(2004, 6, 15) -- cgit v1.2.3