Permalink
Browse files

Added :use_short_month option to select_month helper to show month na…

…mes as abbreviations

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2033 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent c031a01 commit 82f44f314402cbd4627caa1c8ba24c35a5c875dd @jamis jamis committed Aug 22, 2005
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added :use_short_month option to select_month helper to show month names as abbreviations
+
* Make link_to escape the javascript in the confirm option #1964 [nicolas.pouillard@gmail.com]
* Make assert_redirected_to properly check URL's passed as strings #1910 [Scott Barron]
View
8 actionpack/lib/action_view/helpers/date_helper.rb
@@ -181,16 +181,20 @@ def select_day(date, options = {})
# select_month(Date.today, :add_month_numbers => true) # Will use keys like "1 - January", "3 - March"
#
# Override the field name using the <tt>:field_name</tt> option, 'month' by default.
+ #
+ # If you would prefer to show month names as abbreviations, set the
+ # <tt>:use_short_month</tt> key in +options+ to true.
def select_month(date, options = {})
month_options = []
+ month_names = options[:use_short_month] ? Date::ABBR_MONTHNAMES : Date::MONTHNAMES
1.upto(12) do |month_number|
month_name = if options[:use_month_numbers]
month_number
elsif options[:add_month_numbers]
- month_number.to_s + ' - ' + Date::MONTHNAMES[month_number]
+ month_number.to_s + ' - ' + month_names[month_number]
else
- Date::MONTHNAMES[month_number]
+ month_names[month_number]
end
month_options << ((date && (date.kind_of?(Fixnum) ? date : date.month) == month_number) ?
View
18 actionpack/test/template/date_helper_test.rb
@@ -130,6 +130,24 @@ def test_select_month_with_numbers_and_names
assert_equal expected, select_month(8, :add_month_numbers => true)
end
+ def test_select_month_with_numbers_and_names_with_abbv
+ expected = %(<select name="date[month]">\n)
+ expected << %(<option value="1">1 - Jan</option>\n<option value="2">2 - Feb</option>\n<option value="3">3 - Mar</option>\n<option value="4">4 - Apr</option>\n<option value="5">5 - May</option>\n<option value="6">6 - Jun</option>\n<option value="7">7 - Jul</option>\n<option value="8" selected="selected">8 - Aug</option>\n<option value="9">9 - Sep</option>\n<option value="10">10 - Oct</option>\n<option value="11">11 - Nov</option>\n<option value="12">12 - Dec</option>\n)
+ expected << "</select>\n"
+
+ assert_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true, :use_short_month => true)
+ assert_equal expected, select_month(8, :add_month_numbers => true, :use_short_month => true)
+ end
+
+ def test_select_month_with_abbv
+ expected = %(<select name="date[month]">\n)
+ expected << %(<option value="1">Jan</option>\n<option value="2">Feb</option>\n<option value="3">Mar</option>\n<option value="4">Apr</option>\n<option value="5">May</option>\n<option value="6">Jun</option>\n<option value="7">Jul</option>\n<option value="8" selected="selected">Aug</option>\n<option value="9">Sep</option>\n<option value="10">Oct</option>\n<option value="11">Nov</option>\n<option value="12">Dec</option>\n)
+ expected << "</select>\n"
+
+ assert_equal expected, select_month(Time.mktime(2003, 8, 16), :use_short_month => true)
+ assert_equal expected, select_month(8, :use_short_month => true)
+ end
+
def test_select_year
expected = %(<select name="date[year]">\n)
expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n)

0 comments on commit 82f44f3

Please sign in to comment.