Skip to content

Commit ec4440f

Browse files
committed
Merge pull request rails#4739 from nashby/refactor-date-helper
tiny refactoring of date_helper
2 parents 786a019 + 735c56d commit ec4440f

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

actionpack/lib/action_view/helpers/date_helper.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def select_hour(datetime, options = {}, html_options = {})
508508

509509
# Returns a select tag with options for each of the days 1 through 31 with the current day selected.
510510
# The <tt>date</tt> can also be substituted for a day number.
511-
# If you want to display days with a leading zero set the <tt>:use_two_digit_numbers</tt> key in +options+ to true.
511+
# If you want to display days with a leading zero set the <tt>:use_two_digit_numbers</tt> key in +options+ to true.
512512
# Override the field name using the <tt>:field_name</tt> option, 'day' by default.
513513
#
514514
# ==== Examples
@@ -854,7 +854,7 @@ def date_order
854854
end
855855

856856
def translated_date_order
857-
date_order = I18n.translate(:'date.order', :locale => @options[:locale]) || []
857+
date_order = I18n.translate(:'date.order', :locale => @options[:locale], :default => [])
858858

859859
forbidden_elements = date_order - [:year, :month, :day]
860860
if forbidden_elements.any?
@@ -990,18 +990,12 @@ def build_selects_from_types(order)
990990
# Returns the separator for a given datetime component.
991991
def separator(type)
992992
case type
993-
when :year
994-
@options[:discard_year] ? "" : @options[:date_separator]
995-
when :month
996-
@options[:discard_month] ? "" : @options[:date_separator]
997-
when :day
998-
@options[:discard_day] ? "" : @options[:date_separator]
993+
when :year, :month, :day
994+
@options[:"discard_#{type}"] ? "" : @options[:date_separator]
999995
when :hour
1000996
(@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
1001-
when :minute
1002-
@options[:discard_minute] ? "" : @options[:time_separator]
1003-
when :second
1004-
@options[:include_seconds] ? @options[:time_separator] : ""
997+
when :minute, :second
998+
@options[:"discard_#{type}"] ? "" : @options[:time_separator]
1005999
end
10061000
end
10071001
end

actionpack/test/template/date_helper_i18n_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_date_or_time_select_translates_prompts
103103
I18n.expects(:translate).with(('datetime.prompts.' + key.to_s).to_sym, :locale => 'en').returns prompt
104104
end
105105

106-
I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
106+
I18n.expects(:translate).with(:'date.order', :locale => 'en', :default => []).returns [:year, :month, :day]
107107
datetime_select('post', 'updated_at', :locale => 'en', :include_seconds => true, :prompt => true)
108108
end
109109

@@ -115,12 +115,12 @@ def test_date_or_time_select_given_an_order_options_does_not_translate_order
115115
end
116116

117117
def test_date_or_time_select_given_no_order_options_translates_order
118-
I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
118+
I18n.expects(:translate).with(:'date.order', :locale => 'en', :default => []).returns [:year, :month, :day]
119119
datetime_select('post', 'updated_at', :locale => 'en')
120120
end
121121

122122
def test_date_or_time_select_given_invalid_order
123-
I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:invalid, :month, :day]
123+
I18n.expects(:translate).with(:'date.order', :locale => 'en', :default => []).returns [:invalid, :month, :day]
124124

125125
assert_raise StandardError do
126126
datetime_select('post', 'updated_at', :locale => 'en')

0 commit comments

Comments
 (0)