Skip to content


Extend date_select helper functionality. #7975

merged 1 commit into from

3 participants


This is clone of pull request #5553
During the rebasing from rails/master github close my previous pull request with very strange comment... so I just copy/paste explanation from the closed pull request.

Often we need to have date selectors with different styles. (Most often different width.) Unfortunately date_select helper don't provide possibility to set different CSS classes. This commit extend date_select helper with option :css_by_type that set special classes for generated 'select' tags. Class names equal data type, for example:
f.date_select :birthday, :css_by_type => true

<select class='month ...>...</select>
<select class='day' ...>...</select>
<select class='year' ...>...</select>

Hope it will by helpfully.

Ruby on Rails member

Seems good. Could you add a CHANGELOG entry?

Ruby on Rails member

As a side note, I'd like this to be a default, I can see it being used more than not. But, it could break some css out there if we just change it, so nevermind.


CHANGELOG entry added

Ruby on Rails member

@butcher always put new changelog entries in the top, and please squash the commits.


@rafaelfranca thanks for the notes. Hope it will be first but not last merged pull request.

Ruby on Rails member

Merged. Thank you so much.

And welcome to the Rails contributors.

@rafaelfranca rafaelfranca merged commit a48ef9b into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 18, 2012
  1. @butcher
3 actionpack/
@@ -1,5 +1,8 @@
## Rails 4.0.0 (unreleased) ##
+* `date_select` helper accepts :with_css_classes => true to add css classes similar with type
+ of generated select tags. *Pavel Nikitin*
* `assert_template` can be used to assert on the same template with different locals
Fix #3675
3 actionpack/lib/action_view/helpers/date_helper.rb
@@ -196,6 +196,8 @@ def time_ago_in_words(from_time, include_seconds_or_options = {})
# for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt> and <tt>:second</tt>.
# Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds)
# or the given prompt string.
+ # * <tt>:with_css_classes</tt> - Set to true if you want assign different styles for 'select' tags. This option
+ # automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags.
# If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
@@ -937,6 +939,7 @@ def build_select(type, select_options_as_html)
:name => input_name_from_type(type)
select_options[:disabled] = 'disabled' if @options[:disabled]
+ select_options[:class] = type if @options[:with_css_classes]
select_html = "\n"
select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
16 actionpack/test/template/date_helper_test.rb
@@ -1007,6 +1007,22 @@ def test_select_date_with_hidden
assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => " / ", :prefix => "date[first]", :use_hidden => true })
+ def test_select_date_with_css_classes_option
+ expected = %(<select id="date_first_year" name="date[first][year]" class="year">\n)
+ expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n)
+ expected << "</select>\n"
+ expected << %(<select id="date_first_month" name="date[first][month]" class="month">\n)
+ expected << %(<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8" selected="selected">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n)
+ expected << "</select>\n"
+ expected << %(<select id="date_first_day" name="date[first][day]" class="day">\n)
+ expected << %(<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n)
+ expected << "</select>\n"
+ assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), {:start_year => 2003, :end_year => 2005, :prefix => "date[first]", :with_css_classes => true})
+ end
def test_select_datetime
expected = %(<select id="date_first_year" name="date[first][year]">\n)
expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n)
Something went wrong with that request. Please try again.