Permalink
Browse files

Merge pull request #4154 from nashby/include-blank-date-helper

a prompt string in include_blank option for date helpers
  • Loading branch information...
drogus committed Dec 26, 2011
2 parents 28cd098 + f7669cd commit 76f0fa6066b78cd8f0e139dff4a7b65c96db5778
Showing with 22 additions and 2 deletions.
  1. +2 −2 actionpack/lib/action_view/helpers/date_helper.rb
  2. +20 −0 actionpack/test/template/date_helper_test.rb
@@ -13,7 +13,7 @@ module Helpers
#
# * <tt>:prefix</tt> - overwrites the default prefix of "date" used for the select names. So specifying "birthday"
# would give birthday[month] instead of date[month] if passed to the <tt>select_month</tt> method.
- # * <tt>:include_blank</tt> - set to true if it should be possible to set an empty date.
+ # * <tt>:include_blank</tt> - set to true or to a prompt string if it should be possible to set an empty date.
# * <tt>:discard_type</tt> - set to true if you want to discard the type part of the select name. If set to true,
# the <tt>select_month</tt> method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead
# of "date[month]".
@@ -887,7 +887,7 @@ def build_select(type, select_options_as_html)
select_options.merge!(:disabled => 'disabled') if @options[:disabled]
select_html = "\n"
- select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
+ select_html << content_tag(:option, "#{ERB::Util.html_escape(@options[:include_blank]) if @options[:include_blank].kind_of?(String)}", :value => '') + "\n" if @options[:include_blank]
@josevalim

josevalim Dec 26, 2011

Contributor

Why would :include_blank allow a prompt? Shouldn't we support :prompt instead? Also, please break this implementation into more than one line, it is extremely long. :)

@drogus

drogus Dec 26, 2011

Member

I think that this behavior is nice because other select helpers allow to pass a string to include_blank.

@drogus

drogus Dec 26, 2011

Member

Which makes it more consistent with the other helpers

select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
select_html << select_options_as_html
@@ -1581,6 +1581,26 @@ def test_date_select_with_nil_and_blank
assert_dom_equal expected, date_select("post", "written_on", :include_blank => true)
end
+ def test_date_select_with_stringified_blank
+ @post = Post.new
+
+ start_year = Time.now.year-5
+ end_year = Time.now.year+5
+
+ expected = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n"
+ expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
+ expected << "<option value=\"\">blank</option>\n"
+ start_year.upto(end_year) { |i| expected << %(<option value="#{i}">#{i}</option>\n) }
+ expected << "</select>\n"
+
+ expected << %{<select id="post_written_on_2i" name="post[written_on(2i)]">\n}
+ expected << "<option value=\"\">blank</option>\n"
+ 1.upto(12) { |i| expected << %(<option value="#{i}">#{Date::MONTHNAMES[i]}</option>\n) }
+ expected << "</select>\n"
+
+ assert_dom_equal expected, date_select("post", "written_on", :order=>[:year, :month], :include_blank=> 'blank')
+ end
+
def test_date_select_with_nil_and_blank_and_order
@post = Post.new

0 comments on commit 76f0fa6

Please sign in to comment.