actionview 4.2.10 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +141 -272
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +2 -3
  5. data/lib/action_view/base.rb +33 -21
  6. data/lib/action_view/buffers.rb +1 -1
  7. data/lib/action_view/context.rb +1 -1
  8. data/lib/action_view/dependency_tracker.rb +52 -20
  9. data/lib/action_view/digestor.rb +86 -83
  10. data/lib/action_view/flows.rb +9 -11
  11. data/lib/action_view/gem_version.rb +3 -3
  12. data/lib/action_view/helpers/active_model_helper.rb +8 -8
  13. data/lib/action_view/helpers/asset_tag_helper.rb +74 -38
  14. data/lib/action_view/helpers/asset_url_helper.rb +160 -59
  15. data/lib/action_view/helpers/atom_feed_helper.rb +16 -16
  16. data/lib/action_view/helpers/cache_helper.rb +90 -35
  17. data/lib/action_view/helpers/capture_helper.rb +7 -6
  18. data/lib/action_view/helpers/controller_helper.rb +3 -2
  19. data/lib/action_view/helpers/csrf_helper.rb +3 -3
  20. data/lib/action_view/helpers/date_helper.rb +156 -108
  21. data/lib/action_view/helpers/debug_helper.rb +3 -4
  22. data/lib/action_view/helpers/form_helper.rb +475 -94
  23. data/lib/action_view/helpers/form_options_helper.rb +87 -47
  24. data/lib/action_view/helpers/form_tag_helper.rb +88 -57
  25. data/lib/action_view/helpers/javascript_helper.rb +10 -10
  26. data/lib/action_view/helpers/number_helper.rb +76 -59
  27. data/lib/action_view/helpers/output_safety_helper.rb +34 -4
  28. data/lib/action_view/helpers/record_tag_helper.rb +12 -99
  29. data/lib/action_view/helpers/rendering_helper.rb +3 -3
  30. data/lib/action_view/helpers/sanitize_helper.rb +17 -14
  31. data/lib/action_view/helpers/tag_helper.rb +198 -73
  32. data/lib/action_view/helpers/tags/base.rb +132 -97
  33. data/lib/action_view/helpers/tags/check_box.rb +17 -17
  34. data/lib/action_view/helpers/tags/collection_check_boxes.rb +9 -33
  35. data/lib/action_view/helpers/tags/collection_helpers.rb +68 -36
  36. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +3 -11
  37. data/lib/action_view/helpers/tags/collection_select.rb +2 -2
  38. data/lib/action_view/helpers/tags/date_select.rb +36 -36
  39. data/lib/action_view/helpers/tags/datetime_field.rb +1 -1
  40. data/lib/action_view/helpers/tags/grouped_collection_select.rb +2 -2
  41. data/lib/action_view/helpers/tags/label.rb +5 -1
  42. data/lib/action_view/helpers/tags/password_field.rb +1 -1
  43. data/lib/action_view/helpers/tags/placeholderable.rb +1 -1
  44. data/lib/action_view/helpers/tags/radio_button.rb +4 -4
  45. data/lib/action_view/helpers/tags/search_field.rb +12 -9
  46. data/lib/action_view/helpers/tags/select.rb +9 -9
  47. data/lib/action_view/helpers/tags/text_area.rb +1 -1
  48. data/lib/action_view/helpers/tags/text_field.rb +5 -6
  49. data/lib/action_view/helpers/tags/translator.rb +15 -13
  50. data/lib/action_view/helpers/text_helper.rb +47 -30
  51. data/lib/action_view/helpers/translation_helper.rb +60 -30
  52. data/lib/action_view/helpers/url_helper.rb +132 -104
  53. data/lib/action_view/helpers.rb +1 -1
  54. data/lib/action_view/layouts.rb +59 -54
  55. data/lib/action_view/log_subscriber.rb +56 -7
  56. data/lib/action_view/lookup_context.rb +76 -61
  57. data/lib/action_view/model_naming.rb +1 -1
  58. data/lib/action_view/path_set.rb +28 -19
  59. data/lib/action_view/railtie.rb +30 -6
  60. data/lib/action_view/record_identifier.rb +51 -25
  61. data/lib/action_view/renderer/abstract_renderer.rb +19 -15
  62. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +55 -0
  63. data/lib/action_view/renderer/partial_renderer.rb +208 -206
  64. data/lib/action_view/renderer/renderer.rb +2 -6
  65. data/lib/action_view/renderer/streaming_template_renderer.rb +46 -48
  66. data/lib/action_view/renderer/template_renderer.rb +65 -66
  67. data/lib/action_view/rendering.rb +16 -9
  68. data/lib/action_view/routing_url_for.rb +25 -17
  69. data/lib/action_view/tasks/cache_digests.rake +23 -0
  70. data/lib/action_view/template/error.rb +14 -13
  71. data/lib/action_view/template/handlers/builder.rb +7 -7
  72. data/lib/action_view/template/handlers/erb/deprecated_erubis.rb +9 -0
  73. data/lib/action_view/template/handlers/erb/erubi.rb +81 -0
  74. data/lib/action_view/template/handlers/erb/erubis.rb +81 -0
  75. data/lib/action_view/template/handlers/erb.rb +9 -76
  76. data/lib/action_view/template/handlers/html.rb +9 -0
  77. data/lib/action_view/template/handlers/raw.rb +1 -3
  78. data/lib/action_view/template/handlers.rb +8 -6
  79. data/lib/action_view/template/html.rb +2 -4
  80. data/lib/action_view/template/resolver.rb +133 -109
  81. data/lib/action_view/template/text.rb +5 -8
  82. data/lib/action_view/template/types.rb +15 -17
  83. data/lib/action_view/template.rb +51 -28
  84. data/lib/action_view/test_case.rb +32 -27
  85. data/lib/action_view/testing/resolvers.rb +29 -31
  86. data/lib/action_view/version.rb +1 -1
  87. data/lib/action_view/view_paths.rb +26 -32
  88. data/lib/action_view.rb +5 -5
  89. data/lib/assets/compiled/rails-ujs.js +685 -0
  90. metadata +23 -23
  91. data/lib/action_view/tasks/dependencies.rake +0 -23
@@ -1,9 +1,10 @@
1
- require 'date'
2
- require 'action_view/helpers/tag_helper'
3
- require 'active_support/core_ext/array/extract_options'
4
- require 'active_support/core_ext/date/conversions'
5
- require 'active_support/core_ext/hash/slice'
6
- require 'active_support/core_ext/object/with_options'
1
+ require "date"
2
+ require "action_view/helpers/tag_helper"
3
+ require "active_support/core_ext/array/extract_options"
4
+ require "active_support/core_ext/date/conversions"
5
+ require "active_support/core_ext/hash/slice"
6
+ require "active_support/core_ext/object/acts_like"
7
+ require "active_support/core_ext/object/with_options"
7
8
 
8
9
  module ActionView
9
10
  module Helpers
@@ -68,6 +69,27 @@ module ActionView
68
69
  # distance_of_time_in_words(from_time, to_time, include_seconds: true) # => about 6 years
69
70
  # distance_of_time_in_words(to_time, from_time, include_seconds: true) # => about 6 years
70
71
  # distance_of_time_in_words(Time.now, Time.now) # => less than a minute
72
+ #
73
+ # With the <tt>scope</tt> option, you can define a custom scope for Rails
74
+ # to look up the translation.
75
+ #
76
+ # For example you can define the following in your locale (e.g. en.yml).
77
+ #
78
+ # datetime:
79
+ # distance_in_words:
80
+ # short:
81
+ # about_x_hours:
82
+ # one: 'an hour'
83
+ # other: '%{count} hours'
84
+ #
85
+ # See https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
86
+ # for more examples.
87
+ #
88
+ # Which will then result in the following:
89
+ #
90
+ # from_time = Time.now
91
+ # distance_of_time_in_words(from_time, from_time + 50.minutes, scope: 'datetime.distance_in_words.short') # => "an hour"
92
+ # distance_of_time_in_words(from_time, from_time + 3.hours, scope: 'datetime.distance_in_words.short') # => "3 hours"
71
93
  def distance_of_time_in_words(from_time, to_time = 0, options = {})
72
94
  options = {
73
95
  scope: :'datetime.distance_in_words'
@@ -76,63 +98,63 @@ module ActionView
76
98
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
77
99
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
78
100
  from_time, to_time = to_time, from_time if from_time > to_time
79
- distance_in_minutes = ((to_time - from_time)/60.0).round
101
+ distance_in_minutes = ((to_time - from_time) / 60.0).round
80
102
  distance_in_seconds = (to_time - from_time).round
81
103
 
82
- I18n.with_options :locale => options[:locale], :scope => options[:scope] do |locale|
104
+ I18n.with_options locale: options[:locale], scope: options[:scope] do |locale|
83
105
  case distance_in_minutes
84
- when 0..1
85
- return distance_in_minutes == 0 ?
86
- locale.t(:less_than_x_minutes, :count => 1) :
87
- locale.t(:x_minutes, :count => distance_in_minutes) unless options[:include_seconds]
88
-
89
- case distance_in_seconds
90
- when 0..4 then locale.t :less_than_x_seconds, :count => 5
91
- when 5..9 then locale.t :less_than_x_seconds, :count => 10
92
- when 10..19 then locale.t :less_than_x_seconds, :count => 20
93
- when 20..39 then locale.t :half_a_minute
94
- when 40..59 then locale.t :less_than_x_minutes, :count => 1
95
- else locale.t :x_minutes, :count => 1
96
- end
97
-
98
- when 2...45 then locale.t :x_minutes, :count => distance_in_minutes
99
- when 45...90 then locale.t :about_x_hours, :count => 1
106
+ when 0..1
107
+ return distance_in_minutes == 0 ?
108
+ locale.t(:less_than_x_minutes, count: 1) :
109
+ locale.t(:x_minutes, count: distance_in_minutes) unless options[:include_seconds]
110
+
111
+ case distance_in_seconds
112
+ when 0..4 then locale.t :less_than_x_seconds, count: 5
113
+ when 5..9 then locale.t :less_than_x_seconds, count: 10
114
+ when 10..19 then locale.t :less_than_x_seconds, count: 20
115
+ when 20..39 then locale.t :half_a_minute
116
+ when 40..59 then locale.t :less_than_x_minutes, count: 1
117
+ else locale.t :x_minutes, count: 1
118
+ end
119
+
120
+ when 2...45 then locale.t :x_minutes, count: distance_in_minutes
121
+ when 45...90 then locale.t :about_x_hours, count: 1
100
122
  # 90 mins up to 24 hours
101
- when 90...1440 then locale.t :about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round
123
+ when 90...1440 then locale.t :about_x_hours, count: (distance_in_minutes.to_f / 60.0).round
102
124
  # 24 hours up to 42 hours
103
- when 1440...2520 then locale.t :x_days, :count => 1
125
+ when 1440...2520 then locale.t :x_days, count: 1
104
126
  # 42 hours up to 30 days
105
- when 2520...43200 then locale.t :x_days, :count => (distance_in_minutes.to_f / 1440.0).round
127
+ when 2520...43200 then locale.t :x_days, count: (distance_in_minutes.to_f / 1440.0).round
106
128
  # 30 days up to 60 days
107
- when 43200...86400 then locale.t :about_x_months, :count => (distance_in_minutes.to_f / 43200.0).round
129
+ when 43200...86400 then locale.t :about_x_months, count: (distance_in_minutes.to_f / 43200.0).round
108
130
  # 60 days up to 365 days
109
- when 86400...525600 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).round
131
+ when 86400...525600 then locale.t :x_months, count: (distance_in_minutes.to_f / 43200.0).round
132
+ else
133
+ if from_time.acts_like?(:time) && to_time.acts_like?(:time)
134
+ fyear = from_time.year
135
+ fyear += 1 if from_time.month >= 3
136
+ tyear = to_time.year
137
+ tyear -= 1 if to_time.month < 3
138
+ leap_years = (fyear > tyear) ? 0 : (fyear..tyear).count { |x| Date.leap?(x) }
139
+ minute_offset_for_leap_year = leap_years * 1440
140
+ # Discount the leap year days when calculating year distance.
141
+ # e.g. if there are 20 leap year days between 2 dates having the same day
142
+ # and month then the based on 365 days calculation
143
+ # the distance in years will come out to over 80 years when in written
144
+ # English it would read better as about 80 years.
145
+ minutes_with_offset = distance_in_minutes - minute_offset_for_leap_year
110
146
  else
111
- if from_time.acts_like?(:time) && to_time.acts_like?(:time)
112
- fyear = from_time.year
113
- fyear += 1 if from_time.month >= 3
114
- tyear = to_time.year
115
- tyear -= 1 if to_time.month < 3
116
- leap_years = (fyear > tyear) ? 0 : (fyear..tyear).count{|x| Date.leap?(x)}
117
- minute_offset_for_leap_year = leap_years * 1440
118
- # Discount the leap year days when calculating year distance.
119
- # e.g. if there are 20 leap year days between 2 dates having the same day
120
- # and month then the based on 365 days calculation
121
- # the distance in years will come out to over 80 years when in written
122
- # English it would read better as about 80 years.
123
- minutes_with_offset = distance_in_minutes - minute_offset_for_leap_year
124
- else
125
- minutes_with_offset = distance_in_minutes
126
- end
127
- remainder = (minutes_with_offset % MINUTES_IN_YEAR)
128
- distance_in_years = (minutes_with_offset.div MINUTES_IN_YEAR)
129
- if remainder < MINUTES_IN_QUARTER_YEAR
130
- locale.t(:about_x_years, :count => distance_in_years)
131
- elsif remainder < MINUTES_IN_THREE_QUARTERS_YEAR
132
- locale.t(:over_x_years, :count => distance_in_years)
133
- else
134
- locale.t(:almost_x_years, :count => distance_in_years + 1)
135
- end
147
+ minutes_with_offset = distance_in_minutes
148
+ end
149
+ remainder = (minutes_with_offset % MINUTES_IN_YEAR)
150
+ distance_in_years = (minutes_with_offset.div MINUTES_IN_YEAR)
151
+ if remainder < MINUTES_IN_QUARTER_YEAR
152
+ locale.t(:about_x_years, count: distance_in_years)
153
+ elsif remainder < MINUTES_IN_THREE_QUARTERS_YEAR
154
+ locale.t(:over_x_years, count: distance_in_years)
155
+ else
156
+ locale.t(:almost_x_years, count: distance_in_years + 1)
157
+ end
136
158
  end
137
159
  end
138
160
  end
@@ -177,6 +199,8 @@ module ActionView
177
199
  # and +:name+ (string). A format string would be something like "%{name} (%<number>02d)" for example.
178
200
  # See <tt>Kernel.sprintf</tt> for documentation on format sequences.
179
201
  # * <tt>:date_separator</tt> - Specifies a string to separate the date fields. Default is "" (i.e. nothing).
202
+ # * <tt>:time_separator</tt> - Specifies a string to separate the time fields. Default is "" (i.e. nothing).
203
+ # * <tt>:datetime_separator</tt>- Specifies a string to separate the date and time fields. Default is "" (i.e. nothing).
180
204
  # * <tt>:start_year</tt> - Set the start year for the year select. Default is <tt>Date.today.year - 5</tt> if
181
205
  # you are creating new record. While editing existing record, <tt>:start_year</tt> defaults to
182
206
  # the current selected year minus 5.
@@ -196,15 +220,18 @@ module ActionView
196
220
  # the respective locale (e.g. [:year, :month, :day] in the en locale that ships with Rails).
197
221
  # * <tt>:include_blank</tt> - Include a blank option in every select field so it's possible to set empty
198
222
  # dates.
199
- # * <tt>:default</tt> - Set a default date if the affected date isn't set or is nil.
223
+ # * <tt>:default</tt> - Set a default date if the affected date isn't set or is +nil+.
200
224
  # * <tt>:selected</tt> - Set a date that overrides the actual value.
201
225
  # * <tt>:disabled</tt> - Set to true if you want show the select fields as disabled.
202
226
  # * <tt>:prompt</tt> - Set to true (for a generic prompt), a prompt string or a hash of prompt strings
203
227
  # for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt> and <tt>:second</tt>.
204
228
  # Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds)
205
229
  # or the given prompt string.
206
- # * <tt>:with_css_classes</tt> - Set to true if you want assign different styles for 'select' tags. This option
207
- # automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags.
230
+ # * <tt>:with_css_classes</tt> - Set to true or a hash of strings. Use true if you want to assign generic styles for
231
+ # select tags. This automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second'. A hash of
232
+ # strings for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt>, <tt>:second</tt>
233
+ # will extend the select type with the given value. Use +html_options+ to modify every select tag in the set.
234
+ # * <tt>:use_hidden</tt> - Set to true if you only want to generate hidden input tags.
208
235
  #
209
236
  # If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
210
237
  #
@@ -240,7 +267,7 @@ module ActionView
240
267
  # date_select("article", "written_on", default: 3.days.from_now)
241
268
  #
242
269
  # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
243
- # # which is set in the form with todays date, regardless of the value in the Active Record object.
270
+ # # which is set in the form with today's date, regardless of the value in the Active Record object.
244
271
  # date_select("article", "written_on", selected: Date.today)
245
272
  #
246
273
  # # Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute
@@ -276,7 +303,7 @@ module ActionView
276
303
  # # the sunrise attribute.
277
304
  # time_select("article", "start_time", include_seconds: true)
278
305
  #
279
- # # You can set the <tt>:minute_step</tt> to 15 which will give you: 00, 15, 30 and 45.
306
+ # # You can set the <tt>:minute_step</tt> to 15 which will give you: 00, 15, 30, and 45.
280
307
  # time_select 'game', 'game_time', {minute_step: 15}
281
308
  #
282
309
  # # Creates a time select tag with a custom prompt. Use <tt>prompt: true</tt> for generic prompts.
@@ -462,7 +489,7 @@ module ActionView
462
489
  # The <tt>datetime</tt> can be either a +Time+ or +DateTime+ object or an integer.
463
490
  # Override the field name using the <tt>:field_name</tt> option, 'second' by default.
464
491
  #
465
- # my_time = Time.now + 16.minutes
492
+ # my_time = Time.now + 16.seconds
466
493
  #
467
494
  # # Generates a select field for seconds that defaults to the seconds for the time in my_time.
468
495
  # select_second(my_time)
@@ -486,7 +513,7 @@ module ActionView
486
513
  # selected. The <tt>datetime</tt> can be either a +Time+ or +DateTime+ object or an integer.
487
514
  # Override the field name using the <tt>:field_name</tt> option, 'minute' by default.
488
515
  #
489
- # my_time = Time.now + 6.hours
516
+ # my_time = Time.now + 10.minutes
490
517
  #
491
518
  # # Generates a select field for minutes that defaults to the minutes for the time in my_time.
492
519
  # select_minute(my_time)
@@ -655,19 +682,19 @@ module ActionView
655
682
  def time_tag(date_or_time, *args, &block)
656
683
  options = args.extract_options!
657
684
  format = options.delete(:format) || :long
658
- content = args.first || I18n.l(date_or_time, :format => format)
685
+ content = args.first || I18n.l(date_or_time, format: format)
659
686
  datetime = date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.iso8601
660
687
 
661
- content_tag(:time, content, options.reverse_merge(:datetime => datetime), &block)
688
+ content_tag("time".freeze, content, options.reverse_merge(datetime: datetime), &block)
662
689
  end
663
690
  end
664
691
 
665
692
  class DateTimeSelector #:nodoc:
666
693
  include ActionView::Helpers::TagHelper
667
694
 
668
- DEFAULT_PREFIX = 'date'.freeze
695
+ DEFAULT_PREFIX = "date".freeze
669
696
  POSITION = {
670
- :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6
697
+ year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6
671
698
  }.freeze
672
699
 
673
700
  AMPM_TRANSLATION = Hash[
@@ -683,8 +710,8 @@ module ActionView
683
710
  @options = options.dup
684
711
  @html_options = html_options.dup
685
712
  @datetime = datetime
686
- @options[:datetime_separator] ||= ' &mdash; '
687
- @options[:time_separator] ||= ' : '
713
+ @options[:datetime_separator] ||= " &mdash; "
714
+ @options[:time_separator] ||= " : "
688
715
  end
689
716
 
690
717
  def select_datetime
@@ -754,7 +781,7 @@ module ActionView
754
781
  if @options[:use_hidden] || @options[:discard_minute]
755
782
  build_hidden(:minute, min)
756
783
  else
757
- build_options_and_select(:minute, min, :step => @options[:minute_step])
784
+ build_options_and_select(:minute, min, step: @options[:minute_step])
758
785
  end
759
786
  end
760
787
 
@@ -774,7 +801,7 @@ module ActionView
774
801
  if @options[:use_hidden] || @options[:discard_day]
775
802
  build_hidden(:day, day || 1)
776
803
  else
777
- build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => false, :use_two_digit_numbers => @options[:use_two_digit_numbers])
804
+ build_options_and_select(:day, day, start: 1, end: 31, leading_zeros: false, use_two_digit_numbers: @options[:use_two_digit_numbers])
778
805
  end
779
806
  end
780
807
 
@@ -784,9 +811,9 @@ module ActionView
784
811
  else
785
812
  month_options = []
786
813
  1.upto(12) do |month_number|
787
- options = { :value => month_number }
814
+ options = { value: month_number }
788
815
  options[:selected] = "selected" if month == month_number
789
- month_options << content_tag(:option, month_name(month_number), options) + "\n"
816
+ month_options << content_tag("option".freeze, month_name(month_number), options) + "\n"
790
817
  end
791
818
  build_select(:month, month_options.join)
792
819
  end
@@ -794,7 +821,7 @@ module ActionView
794
821
 
795
822
  def select_year
796
823
  if !@datetime || @datetime == 0
797
- val = '1'
824
+ val = "1"
798
825
  middle_year = Date.today.year
799
826
  else
800
827
  val = middle_year = year
@@ -821,7 +848,12 @@ module ActionView
821
848
  private
822
849
  %w( sec min hour day month year ).each do |method|
823
850
  define_method(method) do
824
- @datetime.kind_of?(Numeric) ? @datetime : @datetime.send(method) if @datetime
851
+ case @datetime
852
+ when Hash then @datetime[method.to_sym]
853
+ when Numeric then @datetime
854
+ when nil then nil
855
+ else @datetime.send(method)
856
+ end
825
857
  end
826
858
  end
827
859
 
@@ -829,12 +861,12 @@ module ActionView
829
861
  # valid. Otherwise, February 31st or February 29th, 2011 can be selected, which are invalid.
830
862
  def set_day_if_discarded
831
863
  if @datetime && @options[:discard_day]
832
- @datetime = @datetime.change(:day => 1)
864
+ @datetime = @datetime.change(day: 1)
833
865
  end
834
866
  end
835
867
 
836
868
  # Returns translated month names, but also ensures that a custom month
837
- # name array has a leading nil element.
869
+ # name array has a leading +nil+ element.
838
870
  def month_names
839
871
  @month_names ||= begin
840
872
  month_names = @options[:use_month_names] || translated_month_names
@@ -854,7 +886,7 @@ module ActionView
854
886
  # "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
855
887
  def translated_month_names
856
888
  key = @options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
857
- I18n.translate(key, :locale => @options[:locale])
889
+ I18n.translate(key, locale: @options[:locale])
858
890
  end
859
891
 
860
892
  # Looks up month names by number (1-based):
@@ -882,11 +914,11 @@ module ActionView
882
914
  if @options[:use_month_numbers]
883
915
  number
884
916
  elsif @options[:use_two_digit_numbers]
885
- '%02d' % number
917
+ "%02d" % number
886
918
  elsif @options[:add_month_numbers]
887
919
  "#{number} - #{month_names[number]}"
888
920
  elsif format_string = @options[:month_format_string]
889
- format_string % {number: number, name: month_names[number]}
921
+ format_string % { number: number, name: month_names[number] }
890
922
  else
891
923
  month_names[number]
892
924
  end
@@ -897,8 +929,8 @@ module ActionView
897
929
  end
898
930
 
899
931
  def translated_date_order
900
- date_order = I18n.translate(:'date.order', :locale => @options[:locale], :default => [])
901
- date_order = date_order.map { |element| element.to_sym }
932
+ date_order = I18n.translate(:'date.order', locale: @options[:locale], default: [])
933
+ date_order = date_order.map(&:to_sym)
902
934
 
903
935
  forbidden_elements = date_order - [:year, :month, :day]
904
936
  if forbidden_elements.any?
@@ -944,11 +976,11 @@ module ActionView
944
976
  select_options = []
945
977
  start.step(stop, step) do |i|
946
978
  value = leading_zeros ? sprintf("%02d", i) : i
947
- tag_options = { :value => value }
979
+ tag_options = { value: value }
948
980
  tag_options[:selected] = "selected" if selected == i
949
981
  text = options[:use_two_digit_numbers] ? sprintf("%02d", i) : value
950
982
  text = options[:ampm] ? AMPM_TRANSLATION[i] : text
951
- select_options << content_tag(:option, text, tag_options)
983
+ select_options << content_tag("option".freeze, text, tag_options)
952
984
  end
953
985
 
954
986
  (select_options.join("\n") + "\n").html_safe
@@ -961,35 +993,51 @@ module ActionView
961
993
  # </select>"
962
994
  def build_select(type, select_options_as_html)
963
995
  select_options = {
964
- :id => input_id_from_type(type),
965
- :name => input_name_from_type(type)
996
+ id: input_id_from_type(type),
997
+ name: input_name_from_type(type)
966
998
  }.merge!(@html_options)
967
- select_options[:disabled] = 'disabled' if @options[:disabled]
968
- select_options[:class] = [select_options[:class], type].compact.join(' ') if @options[:with_css_classes]
999
+ select_options[:disabled] = "disabled" if @options[:disabled]
1000
+ select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes]
969
1001
 
970
1002
  select_html = "\n"
971
- select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
1003
+ select_html << content_tag("option".freeze, "", value: "") + "\n" if @options[:include_blank]
972
1004
  select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
973
1005
  select_html << select_options_as_html
974
1006
 
975
- (content_tag(:select, select_html.html_safe, select_options) + "\n").html_safe
1007
+ (content_tag("select".freeze, select_html.html_safe, select_options) + "\n").html_safe
1008
+ end
1009
+
1010
+ # Builds the css class value for the select element
1011
+ # css_class_attribute(:year, 'date optional', { year: 'my-year' })
1012
+ # => "date optional my-year"
1013
+ def css_class_attribute(type, html_options_class, options) # :nodoc:
1014
+ css_class = \
1015
+ case options
1016
+ when Hash
1017
+ options[type.to_sym]
1018
+ else
1019
+ type
1020
+ end
1021
+
1022
+ [html_options_class, css_class].compact.join(" ")
976
1023
  end
977
1024
 
978
1025
  # Builds a prompt option tag with supplied options or from default options.
979
1026
  # prompt_option_tag(:month, prompt: 'Select month')
980
1027
  # => "<option value="">Select month</option>"
981
1028
  def prompt_option_tag(type, options)
982
- prompt = case options
1029
+ prompt = \
1030
+ case options
983
1031
  when Hash
984
- default_options = {:year => false, :month => false, :day => false, :hour => false, :minute => false, :second => false}
1032
+ default_options = { year: false, month: false, day: false, hour: false, minute: false, second: false }
985
1033
  default_options.merge!(options)[type.to_sym]
986
1034
  when String
987
1035
  options
988
1036
  else
989
- I18n.translate(:"datetime.prompts.#{type}", :locale => @options[:locale])
990
- end
1037
+ I18n.translate(:"datetime.prompts.#{type}", locale: @options[:locale])
1038
+ end
991
1039
 
992
- prompt ? content_tag(:option, prompt, :value => '') : ''
1040
+ prompt ? content_tag("option".freeze, prompt, value: "") : ""
993
1041
  end
994
1042
 
995
1043
  # Builds hidden input tag for date part and value.
@@ -997,12 +1045,12 @@ module ActionView
997
1045
  # => "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />"
998
1046
  def build_hidden(type, value)
999
1047
  select_options = {
1000
- :type => "hidden",
1001
- :id => input_id_from_type(type),
1002
- :name => input_name_from_type(type),
1003
- :value => value
1048
+ type: "hidden",
1049
+ id: input_id_from_type(type),
1050
+ name: input_name_from_type(type),
1051
+ value: value
1004
1052
  }.merge!(@html_options.slice(:disabled))
1005
- select_options[:disabled] = 'disabled' if @options[:disabled]
1053
+ select_options[:disabled] = "disabled" if @options[:disabled]
1006
1054
 
1007
1055
  tag(:input, select_options) + "\n".html_safe
1008
1056
  end
@@ -1013,7 +1061,7 @@ module ActionView
1013
1061
  prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
1014
1062
  prefix += "[#{@options[:index]}]" if @options.has_key?(:index)
1015
1063
 
1016
- field_name = @options[:field_name] || type
1064
+ field_name = @options[:field_name] || type.to_s
1017
1065
  if @options[:include_position]
1018
1066
  field_name += "(#{ActionView::Helpers::DateTimeSelector::POSITION[type]}i)"
1019
1067
  end
@@ -1024,8 +1072,8 @@ module ActionView
1024
1072
  # Returns the id attribute for the input tag.
1025
1073
  # => "post_written_on_1i"
1026
1074
  def input_id_from_type(type)
1027
- id = input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
1028
- id = @options[:namespace] + '_' + id if @options[:namespace]
1075
+ id = input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, "_").gsub(/[\]\)]/, "")
1076
+ id = @options[:namespace] + "_" + id if @options[:namespace]
1029
1077
 
1030
1078
  id
1031
1079
  end
@@ -1033,7 +1081,7 @@ module ActionView
1033
1081
  # Given an ordering of datetime components, create the selection HTML
1034
1082
  # and join them with their appropriate separators.
1035
1083
  def build_selects_from_types(order)
1036
- select = ''
1084
+ select = ""
1037
1085
  first_visible = order.find { |type| !@options[:"discard_#{type}"] }
1038
1086
  order.reverse_each do |type|
1039
1087
  separator = separator(type) unless type == first_visible # don't add before first visible field
@@ -1047,12 +1095,12 @@ module ActionView
1047
1095
  return "" if @options[:use_hidden]
1048
1096
 
1049
1097
  case type
1050
- when :year, :month, :day
1051
- @options[:"discard_#{type}"] ? "" : @options[:date_separator]
1052
- when :hour
1053
- (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
1054
- when :minute, :second
1055
- @options[:"discard_#{type}"] ? "" : @options[:time_separator]
1098
+ when :year, :month, :day
1099
+ @options[:"discard_#{type}"] ? "" : @options[:date_separator]
1100
+ when :hour
1101
+ (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
1102
+ when :minute, :second
1103
+ @options[:"discard_#{type}"] ? "" : @options[:time_separator]
1056
1104
  end
1057
1105
  end
1058
1106
  end
@@ -4,7 +4,6 @@ module ActionView
4
4
  # Provides a set of methods for making it easier to debug Rails objects.
5
5
  module Helpers
6
6
  module DebugHelper
7
-
8
7
  include TagHelper
9
8
 
10
9
  # Returns a YAML representation of +object+ wrapped with <pre> and </pre>.
@@ -25,10 +24,10 @@ module ActionView
25
24
  def debug(object)
26
25
  Marshal::dump(object)
27
26
  object = ERB::Util.html_escape(object.to_yaml)
28
- content_tag(:pre, object, :class => "debug_dump")
29
- rescue Exception # errors from Marshal or YAML
27
+ content_tag(:pre, object, class: "debug_dump")
28
+ rescue # errors from Marshal or YAML
30
29
  # Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
31
- content_tag(:code, object.inspect, :class => "debug_dump")
30
+ content_tag(:code, object.inspect, class: "debug_dump")
32
31
  end
33
32
  end
34
33
  end