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