diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 8f845ce7e1..2e683e7c47 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Add start_hour and end_hour options to the select_hour helper. *Evan Tann* + * Raises an ArgumentError when the first argument in `form_for` contain `nil` or is empty. diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 659aacf6d7..dea2aa69dd 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -427,6 +427,9 @@ module ActionView # # Generate a time select field with hours in the AM/PM format # select_time(my_time, :ampm => true) # + # # Generates a time select field with hours that range from 2 to 14 + # select_time(my_time, :start_hour => 2, :end_hour => 14) + # # # Generates a time select with a custom prompt. Use :prompt to true for generic prompts. # select_time(my_time, :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year'}) # select_time(my_time, :prompt => {:hour => true}) # generic prompt for hours @@ -504,6 +507,9 @@ module ActionView # # # Generate a select field for hours in the AM/PM format # select_hour(my_time, :ampm => true) + # + # # Generates a select field that includes options for hours from 2 to 14. + # select_hour(my_time, :start_hour => 2, :end_hour => 14) def select_hour(datetime, options = {}, html_options = {}) DateTimeSelector.new(datetime, options, html_options).select_hour end @@ -734,7 +740,11 @@ module ActionView if @options[:use_hidden] || @options[:discard_hour] build_hidden(:hour, hour) else - build_options_and_select(:hour, hour, :end => 23, :ampm => @options[:ampm]) + options = {} + options[:ampm] = @options[:ampm] || false + options[:start] = @options[:start_hour] || 0 + options[:end] = @options[:end_hour] || 23 + build_options_and_select(:hour, hour, options) end end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index ff85a675a2..c13878af98 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1243,6 +1243,35 @@ class DateHelperTest < ActionView::TestCase :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year', :hour => 'Choose hour', :minute => 'Choose minute'}) end + def test_select_datetime_with_custom_hours + expected = %(\n" + + expected << %(\n" + + expected << %(\n" + + expected << " — " + + expected << %(\n" + + expected << " : " + + expected << %(\n" + + assert_dom_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), :start_year => 2003, :end_year => 2005, :start_hour => 1, :end_hour => 9, :prefix => "date[first]", + :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year', :hour => 'Choose hour', :minute => 'Choose minute'}) + end + def test_select_datetime_with_hidden expected = %(\n) expected << %(\n)