From 2bf29b3485151f819318437561bea8abebe9427b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 23 Feb 2005 12:54:58 +0000 Subject: Added FormOptionsHelper#time_zone_select and FormOptionsHelper#time_zone_options_for_select to work with the new value object TimeZone in Active Record #688 [Jamis Buck] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@759 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/form_options_helper.rb | 57 +++++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index a2ed1cc113..8ead838d36 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -6,7 +6,9 @@ module ActionView module Helpers # Provides a number of methods for turning different kinds of containers into a set of option tags. # == Options - # The collection_select, country_select, and select methods take an options parameter, a hash. + # The collection_select, country_select, select, + # and time_zone_select methods take an options parameter, + # a hash. # # * :include_blank - set to true if the first option element of the select element is a blank. Useful if there is not a default value required for the select element. # select("post", "category", Post::CATEGORIES, {:include_blank => true}) @@ -43,6 +45,18 @@ module ActionView InstanceTag.new(object, method, self).to_country_select_tag(priority_countries, options, html_options) end + # Return select and option tags for the given object and method, using + # #time_zone_options_for_select to generate the list of option tags. + # + # In addition to the :include_blank option documented above, + # this method also supports a :model option, which defaults + # to TimeZone. This may be used by users to specify a different time + # zone model object. (See #time_zone_options_for_select for more + # information.) + def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {}) + InstanceTag.new(object, method, self).to_time_zone_select_tag(priority_zones, options, html_options) + end + # Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container # where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and # the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values @@ -164,6 +178,35 @@ module ActionView return country_options end + # Returns a string of option tags for pretty much any time zone in the + # world. Supply a TimeZone object as +selected+ to have it marked as the + # selected option tag. You can also supply an array of TimeZone objects + # as +priority_zones+, so that they will be listed above the rest of the + # (long) list. (You can use TimeZone.us_zones as a convenience for + # obtaining a list of the US time zones.) + # + # By default, +model+ is the TimeZone constant (which can be obtained + # in ActiveRecord as a value object). The only requirement is that the + # +model+ parameter be an object that responds to #all, and returns + # an array of objects that represent time zones. + # + # NOTE: Only the option tags are returned, you have to wrap this call in + # a regular HTML select tag. + def time_zone_options_for_select(selected = nil, priority_zones = nil, model = TimeZone) + zone_options = "" + + if priority_zones + zone_options += options_for_select(priority_zones, selected) + zone_options += "\n" + + zones = model.all.reject { |z| priority_zones.include?( z ) } + zone_options += options_for_select(zones, selected) + else + zone_options += options_for_select(model.all, selected) + end + + zone_options + end private # All the countries included in the country_options output. @@ -233,10 +276,20 @@ module ActionView content_tag("select", add_blank_option(country_options_for_select(value, priority_countries), options[:include_blank]), html_options) end + def to_time_zone_select_tag(priority_zones, options, html_options) + add_default_name_and_id(html_options) + content_tag("select", + add_blank_option( + time_zone_options_for_select(value, priority_zones, options[:model] || TimeZone), + options[:include_blank] + ), html_options + ) + end + private def add_blank_option(option_tags, add_blank) add_blank ? "\n" + option_tags : option_tags end end end -end +end \ No newline at end of file -- cgit v1.2.3