From a869382a9fa241f72a7a73d4a9738531c4c37ba5 Mon Sep 17 00:00:00 2001 From: Aditya Sanghi Date: Fri, 29 Apr 2011 01:49:45 +0530 Subject: Allow AM/PM in datetime selectors --- actionpack/lib/action_view/helpers/date_helper.rb | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 9277359d5c..7e8ad54d5f 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -468,6 +468,9 @@ module ActionView # # generic prompt. # select_hour(13, :prompt => 'Choose hour') # + # # Generate a select field for hours in the AM/PM format + # select_hour(my_time, :ampm => true) + # def select_hour(datetime, options = {}, html_options = {}) DateTimeSelector.new(datetime, options, html_options).select_hour end @@ -600,6 +603,18 @@ module ActionView POSITION = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }.freeze + ampm = ["AM","PM"].map do |s| + ["12 #{s}"] + (1..11).map{|x| "#{sprintf('%02d',x)} #{s}"} + end.flatten + + AMPM_TRANSLATION = Hash[ + [[0, "12 AM"], [1, "01 AM"], [2, "02 AM"], [3, "03 AM"], + [4, "04 AM"], [5, "05 AM"], [6, "06 AM"], [7, "07 AM"], + [8, "08 AM"], [9, "09 AM"], [10, "10 AM"], [11, "11 AM"], + [12, "12 PM"], [13, "01 PM"], [14, "02 PM"], [15, "03 PM"], + [16, "04 PM"], [17, "05 PM"], [18, "06 PM"], [19, "07 PM"], + [20, "08 PM"], [21, "09 PM"], [22, "10 PM"], [23, "11 PM"]] + ].freeze def initialize(datetime, options = {}, html_options = {}) @options = options.dup @@ -691,6 +706,8 @@ module ActionView def select_hour if @options[:use_hidden] || @options[:discard_hour] build_hidden(:hour, hour) + elsif @options[:ampm] + build_select(:hour, build_ampm_options(hour, :end => 23)) else build_options_and_select(:hour, hour, :end => 23) end @@ -801,6 +818,24 @@ module ActionView build_select(type, build_options(selected, options)) end + def build_ampm_options(selected, options = {}) + start = options.delete(:start) || 0 + stop = options.delete(:end) || 23 + step = options.delete(:step) || 1 + options.reverse_merge!({:leading_zeros => true}) + leading_zeros = options.delete(:leading_zeros) + + select_options = [] + start.step(stop, step) do |i| + text = AMPM_TRANSLATION[i] + value = leading_zeros ? sprintf("%02d", i) : i + tag_options = { :value => value } + tag_options[:selected] = "selected" if selected == i + select_options << content_tag(:option, text, tag_options) + end + (select_options.join("\n") + "\n").html_safe + end + # Build select option html from date value and options # build_options(15, :start => 1, :end => 31) # => " -- cgit v1.2.3 From 610e4d9f24b8fe0c372104481a733b48d2270c3f Mon Sep 17 00:00:00 2001 From: Aditya Sanghi Date: Fri, 29 Apr 2011 02:03:56 +0530 Subject: add more documentation; remove unused assignment --- actionpack/lib/action_view/helpers/date_helper.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 7e8ad54d5f..9e1be05e6c 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -207,7 +207,8 @@ module ActionView # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a # specified time-based attribute (identified by +method+) on an object assigned to the template (identified by - # +object+). You can include the seconds with :include_seconds. + # +object+). You can include the seconds with :include_seconds. You can get hours in the AM/PM format + # with :ampm option. # # This method will also generate 3 input hidden tags, for the actual year, month and day unless the option # :ignore_date is set to +true+. @@ -230,6 +231,9 @@ module ActionView # time_select("post", "written_on", :prompt => {:hour => true}) # generic prompt for hours # time_select("post", "written_on", :prompt => true) # generic prompts for all # + # # You can set :ampm option to true which will show the hours as: 12 PM, 01 AM .. 11 PM. + # time_select 'game', 'game_time', {:ampm => true} + # # The selects are prepared for multi-parameter assignment to an Active Record object. # # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that @@ -257,6 +261,9 @@ module ActionView # # be stored in the trip variable in the departing attribute. # datetime_select("trip", "departing", :default => 3.days.from_now) # + # # Generate a datetime select with hours in the AM/PM format + # datetime_select("post", "written_on", :ampm => true) + # # # Generates a datetime select that discards the type that, when POSTed, will be stored in the post variable # # as the written_on attribute. # datetime_select("post", "written_on", :discard_type => true) @@ -306,6 +313,9 @@ module ActionView # # my_date_time (four days after today) # select_datetime(my_date_time, :discard_type => true) # + # # Generate a datetime field with hours in the AM/PM format + # select_datetime(my_date_time, :ampm => true) + # # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today) # # prefixed with 'payday' rather than 'date' # select_datetime(my_date_time, :prefix => 'payday') @@ -387,6 +397,9 @@ module ActionView # # separated by ':' and includes an input for seconds # select_time(my_time, :time_separator => ':', :include_seconds => true) # + # # Generate a time select field with hours in the AM/PM format + # select_time(my_time, :ampm => true) + # # # Generates a time select with a custom prompt. Use :prompt=>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 @@ -603,9 +616,6 @@ module ActionView POSITION = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }.freeze - ampm = ["AM","PM"].map do |s| - ["12 #{s}"] + (1..11).map{|x| "#{sprintf('%02d',x)} #{s}"} - end.flatten AMPM_TRANSLATION = Hash[ [[0, "12 AM"], [1, "01 AM"], [2, "02 AM"], [3, "03 AM"], -- cgit v1.2.3 From 8bce6e761d52afd68bb06ca9ff8222f072e06cc8 Mon Sep 17 00:00:00 2001 From: Aditya Sanghi Date: Fri, 29 Apr 2011 10:21:16 +0530 Subject: DRY this baby up --- actionpack/lib/action_view/helpers/date_helper.rb | 27 ++++------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 9e1be05e6c..080cf0a866 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -716,10 +716,8 @@ module ActionView def select_hour if @options[:use_hidden] || @options[:discard_hour] build_hidden(:hour, hour) - elsif @options[:ampm] - build_select(:hour, build_ampm_options(hour, :end => 23)) else - build_options_and_select(:hour, hour, :end => 23) + build_options_and_select(:hour, hour, :end => 23, :ampm => @options[:ampm]) end end @@ -828,24 +826,6 @@ module ActionView build_select(type, build_options(selected, options)) end - def build_ampm_options(selected, options = {}) - start = options.delete(:start) || 0 - stop = options.delete(:end) || 23 - step = options.delete(:step) || 1 - options.reverse_merge!({:leading_zeros => true}) - leading_zeros = options.delete(:leading_zeros) - - select_options = [] - start.step(stop, step) do |i| - text = AMPM_TRANSLATION[i] - value = leading_zeros ? sprintf("%02d", i) : i - tag_options = { :value => value } - tag_options[:selected] = "selected" if selected == i - select_options << content_tag(:option, text, tag_options) - end - (select_options.join("\n") + "\n").html_safe - end - # Build select option html from date value and options # build_options(15, :start => 1, :end => 31) # => " @@ -855,7 +835,7 @@ module ActionView start = options.delete(:start) || 0 stop = options.delete(:end) || 59 step = options.delete(:step) || 1 - options.reverse_merge!({:leading_zeros => true}) + options.reverse_merge!({:leading_zeros => true, :ampm => false}) leading_zeros = options.delete(:leading_zeros) select_options = [] @@ -863,7 +843,8 @@ module ActionView value = leading_zeros ? sprintf("%02d", i) : i tag_options = { :value => value } tag_options[:selected] = "selected" if selected == i - select_options << content_tag(:option, value, tag_options) + text = options[:ampm] ? AMPM_TRANSLATION[i] : value + select_options << content_tag(:option, text, tag_options) end (select_options.join("\n") + "\n").html_safe end -- cgit v1.2.3