actionview 4.1.0.beta1

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.

Files changed (106) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +274 -0
  3. data/MIT-LICENSE +21 -0
  4. data/README.rdoc +34 -0
  5. data/lib/action_view.rb +97 -0
  6. data/lib/action_view/base.rb +205 -0
  7. data/lib/action_view/buffers.rb +49 -0
  8. data/lib/action_view/context.rb +36 -0
  9. data/lib/action_view/dependency_tracker.rb +93 -0
  10. data/lib/action_view/digestor.rb +116 -0
  11. data/lib/action_view/flows.rb +76 -0
  12. data/lib/action_view/helpers.rb +64 -0
  13. data/lib/action_view/helpers/active_model_helper.rb +49 -0
  14. data/lib/action_view/helpers/asset_tag_helper.rb +322 -0
  15. data/lib/action_view/helpers/asset_url_helper.rb +355 -0
  16. data/lib/action_view/helpers/atom_feed_helper.rb +203 -0
  17. data/lib/action_view/helpers/cache_helper.rb +200 -0
  18. data/lib/action_view/helpers/capture_helper.rb +216 -0
  19. data/lib/action_view/helpers/controller_helper.rb +25 -0
  20. data/lib/action_view/helpers/csrf_helper.rb +30 -0
  21. data/lib/action_view/helpers/date_helper.rb +1075 -0
  22. data/lib/action_view/helpers/debug_helper.rb +39 -0
  23. data/lib/action_view/helpers/form_helper.rb +1876 -0
  24. data/lib/action_view/helpers/form_options_helper.rb +843 -0
  25. data/lib/action_view/helpers/form_tag_helper.rb +746 -0
  26. data/lib/action_view/helpers/javascript_helper.rb +75 -0
  27. data/lib/action_view/helpers/number_helper.rb +425 -0
  28. data/lib/action_view/helpers/output_safety_helper.rb +38 -0
  29. data/lib/action_view/helpers/record_tag_helper.rb +108 -0
  30. data/lib/action_view/helpers/rendering_helper.rb +90 -0
  31. data/lib/action_view/helpers/sanitize_helper.rb +256 -0
  32. data/lib/action_view/helpers/tag_helper.rb +176 -0
  33. data/lib/action_view/helpers/tags.rb +41 -0
  34. data/lib/action_view/helpers/tags/base.rb +148 -0
  35. data/lib/action_view/helpers/tags/check_box.rb +64 -0
  36. data/lib/action_view/helpers/tags/checkable.rb +16 -0
  37. data/lib/action_view/helpers/tags/collection_check_boxes.rb +44 -0
  38. data/lib/action_view/helpers/tags/collection_helpers.rb +85 -0
  39. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +36 -0
  40. data/lib/action_view/helpers/tags/collection_select.rb +28 -0
  41. data/lib/action_view/helpers/tags/color_field.rb +25 -0
  42. data/lib/action_view/helpers/tags/date_field.rb +13 -0
  43. data/lib/action_view/helpers/tags/date_select.rb +72 -0
  44. data/lib/action_view/helpers/tags/datetime_field.rb +22 -0
  45. data/lib/action_view/helpers/tags/datetime_local_field.rb +19 -0
  46. data/lib/action_view/helpers/tags/datetime_select.rb +8 -0
  47. data/lib/action_view/helpers/tags/email_field.rb +8 -0
  48. data/lib/action_view/helpers/tags/file_field.rb +8 -0
  49. data/lib/action_view/helpers/tags/grouped_collection_select.rb +29 -0
  50. data/lib/action_view/helpers/tags/hidden_field.rb +8 -0
  51. data/lib/action_view/helpers/tags/label.rb +65 -0
  52. data/lib/action_view/helpers/tags/month_field.rb +13 -0
  53. data/lib/action_view/helpers/tags/number_field.rb +18 -0
  54. data/lib/action_view/helpers/tags/password_field.rb +12 -0
  55. data/lib/action_view/helpers/tags/radio_button.rb +31 -0
  56. data/lib/action_view/helpers/tags/range_field.rb +8 -0
  57. data/lib/action_view/helpers/tags/search_field.rb +24 -0
  58. data/lib/action_view/helpers/tags/select.rb +41 -0
  59. data/lib/action_view/helpers/tags/tel_field.rb +8 -0
  60. data/lib/action_view/helpers/tags/text_area.rb +18 -0
  61. data/lib/action_view/helpers/tags/text_field.rb +29 -0
  62. data/lib/action_view/helpers/tags/time_field.rb +13 -0
  63. data/lib/action_view/helpers/tags/time_select.rb +8 -0
  64. data/lib/action_view/helpers/tags/time_zone_select.rb +20 -0
  65. data/lib/action_view/helpers/tags/url_field.rb +8 -0
  66. data/lib/action_view/helpers/tags/week_field.rb +13 -0
  67. data/lib/action_view/helpers/text_helper.rb +447 -0
  68. data/lib/action_view/helpers/translation_helper.rb +111 -0
  69. data/lib/action_view/helpers/url_helper.rb +625 -0
  70. data/lib/action_view/layouts.rb +426 -0
  71. data/lib/action_view/locale/en.yml +56 -0
  72. data/lib/action_view/log_subscriber.rb +44 -0
  73. data/lib/action_view/lookup_context.rb +249 -0
  74. data/lib/action_view/model_naming.rb +12 -0
  75. data/lib/action_view/path_set.rb +77 -0
  76. data/lib/action_view/railtie.rb +49 -0
  77. data/lib/action_view/record_identifier.rb +84 -0
  78. data/lib/action_view/renderer/abstract_renderer.rb +47 -0
  79. data/lib/action_view/renderer/partial_renderer.rb +492 -0
  80. data/lib/action_view/renderer/renderer.rb +50 -0
  81. data/lib/action_view/renderer/streaming_template_renderer.rb +103 -0
  82. data/lib/action_view/renderer/template_renderer.rb +96 -0
  83. data/lib/action_view/rendering.rb +145 -0
  84. data/lib/action_view/routing_url_for.rb +109 -0
  85. data/lib/action_view/tasks/dependencies.rake +17 -0
  86. data/lib/action_view/template.rb +340 -0
  87. data/lib/action_view/template/error.rb +141 -0
  88. data/lib/action_view/template/handlers.rb +53 -0
  89. data/lib/action_view/template/handlers/builder.rb +26 -0
  90. data/lib/action_view/template/handlers/erb.rb +145 -0
  91. data/lib/action_view/template/handlers/raw.rb +11 -0
  92. data/lib/action_view/template/resolver.rb +329 -0
  93. data/lib/action_view/template/text.rb +34 -0
  94. data/lib/action_view/template/types.rb +57 -0
  95. data/lib/action_view/test_case.rb +272 -0
  96. data/lib/action_view/testing/resolvers.rb +50 -0
  97. data/lib/action_view/vendor/html-scanner.rb +20 -0
  98. data/lib/action_view/vendor/html-scanner/html/document.rb +68 -0
  99. data/lib/action_view/vendor/html-scanner/html/node.rb +532 -0
  100. data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +188 -0
  101. data/lib/action_view/vendor/html-scanner/html/selector.rb +830 -0
  102. data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +107 -0
  103. data/lib/action_view/vendor/html-scanner/html/version.rb +11 -0
  104. data/lib/action_view/version.rb +11 -0
  105. data/lib/action_view/view_paths.rb +96 -0
  106. metadata +218 -0
@@ -0,0 +1,25 @@
1
+ require 'active_support/core_ext/module/attr_internal'
2
+
3
+ module ActionView
4
+ module Helpers
5
+ # This module keeps all methods and behavior in ActionView
6
+ # that simply delegates to the controller.
7
+ module ControllerHelper #:nodoc:
8
+ attr_internal :controller, :request
9
+
10
+ delegate :request_forgery_protection_token, :params, :session, :cookies, :response, :headers,
11
+ :flash, :action_name, :controller_name, :controller_path, :to => :controller
12
+
13
+ def assign_controller(controller)
14
+ if @_controller = controller
15
+ @_request = controller.request if controller.respond_to?(:request)
16
+ @_config = controller.config.inheritable_copy if controller.respond_to?(:config)
17
+ end
18
+ end
19
+
20
+ def logger
21
+ controller.logger if controller.respond_to?(:logger)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ module ActionView
2
+ # = Action View CSRF Helper
3
+ module Helpers
4
+ module CsrfHelper
5
+ # Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
6
+ # request forgery protection parameter and token, respectively.
7
+ #
8
+ # <head>
9
+ # <%= csrf_meta_tags %>
10
+ # </head>
11
+ #
12
+ # These are used to generate the dynamic forms that implement non-remote links with
13
+ # <tt>:method</tt>.
14
+ #
15
+ # Note that regular forms generate hidden fields, and that Ajax calls are whitelisted,
16
+ # so they do not use these tags.
17
+ def csrf_meta_tags
18
+ if protect_against_forgery?
19
+ [
20
+ tag('meta', :name => 'csrf-param', :content => request_forgery_protection_token),
21
+ tag('meta', :name => 'csrf-token', :content => form_authenticity_token)
22
+ ].join("\n").html_safe
23
+ end
24
+ end
25
+
26
+ # For backwards compatibility.
27
+ alias csrf_meta_tag csrf_meta_tags
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,1075 @@
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'
7
+
8
+ module ActionView
9
+ module Helpers
10
+ # = Action View Date Helpers
11
+ #
12
+ # The Date Helper primarily creates select/option tags for different kinds of dates and times or date and time
13
+ # elements. All of the select-type methods share a number of common options that are as follows:
14
+ #
15
+ # * <tt>:prefix</tt> - overwrites the default prefix of "date" used for the select names. So specifying "birthday"
16
+ # would give \birthday[month] instead of \date[month] if passed to the <tt>select_month</tt> method.
17
+ # * <tt>:include_blank</tt> - set to true if it should be possible to set an empty date.
18
+ # * <tt>:discard_type</tt> - set to true if you want to discard the type part of the select name. If set to true,
19
+ # the <tt>select_month</tt> method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead
20
+ # of \date[month].
21
+ module DateHelper
22
+ # Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds.
23
+ # Pass <tt>include_seconds: true</tt> if you want more detailed approximations when distance < 1 min, 29 secs.
24
+ # Distances are reported based on the following table:
25
+ #
26
+ # 0 <-> 29 secs # => less than a minute
27
+ # 30 secs <-> 1 min, 29 secs # => 1 minute
28
+ # 1 min, 30 secs <-> 44 mins, 29 secs # => [2..44] minutes
29
+ # 44 mins, 30 secs <-> 89 mins, 29 secs # => about 1 hour
30
+ # 89 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs # => about [2..24] hours
31
+ # 23 hrs, 59 mins, 30 secs <-> 41 hrs, 59 mins, 29 secs # => 1 day
32
+ # 41 hrs, 59 mins, 30 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days
33
+ # 29 days, 23 hrs, 59 mins, 30 secs <-> 44 days, 23 hrs, 59 mins, 29 secs # => about 1 month
34
+ # 44 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 2 months
35
+ # 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
36
+ # 1 yr <-> 1 yr, 3 months # => about 1 year
37
+ # 1 yr, 3 months <-> 1 yr, 9 months # => over 1 year
38
+ # 1 yr, 9 months <-> 2 yr minus 1 sec # => almost 2 years
39
+ # 2 yrs <-> max time or date # => (same rules as 1 yr)
40
+ #
41
+ # With <tt>include_seconds: true</tt> and the difference < 1 minute 29 seconds:
42
+ # 0-4 secs # => less than 5 seconds
43
+ # 5-9 secs # => less than 10 seconds
44
+ # 10-19 secs # => less than 20 seconds
45
+ # 20-39 secs # => half a minute
46
+ # 40-59 secs # => less than a minute
47
+ # 60-89 secs # => 1 minute
48
+ #
49
+ # from_time = Time.now
50
+ # distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
51
+ # distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
52
+ # distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute
53
+ # distance_of_time_in_words(from_time, from_time + 15.seconds, include_seconds: true) # => less than 20 seconds
54
+ # distance_of_time_in_words(from_time, 3.years.from_now) # => about 3 years
55
+ # distance_of_time_in_words(from_time, from_time + 60.hours) # => 3 days
56
+ # distance_of_time_in_words(from_time, from_time + 45.seconds, include_seconds: true) # => less than a minute
57
+ # distance_of_time_in_words(from_time, from_time - 45.seconds, include_seconds: true) # => less than a minute
58
+ # distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute
59
+ # distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
60
+ # distance_of_time_in_words(from_time, from_time + 3.years + 6.months) # => over 3 years
61
+ # distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => about 4 years
62
+ #
63
+ # to_time = Time.now + 6.years + 19.days
64
+ # distance_of_time_in_words(from_time, to_time, include_seconds: true) # => about 6 years
65
+ # distance_of_time_in_words(to_time, from_time, include_seconds: true) # => about 6 years
66
+ # distance_of_time_in_words(Time.now, Time.now) # => less than a minute
67
+ def distance_of_time_in_words(from_time, to_time = 0, options = {})
68
+ options = {
69
+ scope: :'datetime.distance_in_words'
70
+ }.merge!(options)
71
+
72
+ from_time = from_time.to_time if from_time.respond_to?(:to_time)
73
+ to_time = to_time.to_time if to_time.respond_to?(:to_time)
74
+ from_time, to_time = to_time, from_time if from_time > to_time
75
+ distance_in_minutes = ((to_time - from_time)/60.0).round
76
+ distance_in_seconds = (to_time - from_time).round
77
+
78
+ I18n.with_options :locale => options[:locale], :scope => options[:scope] do |locale|
79
+ case distance_in_minutes
80
+ when 0..1
81
+ return distance_in_minutes == 0 ?
82
+ locale.t(:less_than_x_minutes, :count => 1) :
83
+ locale.t(:x_minutes, :count => distance_in_minutes) unless options[:include_seconds]
84
+
85
+ case distance_in_seconds
86
+ when 0..4 then locale.t :less_than_x_seconds, :count => 5
87
+ when 5..9 then locale.t :less_than_x_seconds, :count => 10
88
+ when 10..19 then locale.t :less_than_x_seconds, :count => 20
89
+ when 20..39 then locale.t :half_a_minute
90
+ when 40..59 then locale.t :less_than_x_minutes, :count => 1
91
+ else locale.t :x_minutes, :count => 1
92
+ end
93
+
94
+ when 2...45 then locale.t :x_minutes, :count => distance_in_minutes
95
+ when 45...90 then locale.t :about_x_hours, :count => 1
96
+ # 90 mins up to 24 hours
97
+ when 90...1440 then locale.t :about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round
98
+ # 24 hours up to 42 hours
99
+ when 1440...2520 then locale.t :x_days, :count => 1
100
+ # 42 hours up to 30 days
101
+ when 2520...43200 then locale.t :x_days, :count => (distance_in_minutes.to_f / 1440.0).round
102
+ # 30 days up to 60 days
103
+ when 43200...86400 then locale.t :about_x_months, :count => (distance_in_minutes.to_f / 43200.0).round
104
+ # 60 days up to 365 days
105
+ when 86400...525600 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).round
106
+ else
107
+ if from_time.acts_like?(:time) && to_time.acts_like?(:time)
108
+ fyear = from_time.year
109
+ fyear += 1 if from_time.month >= 3
110
+ tyear = to_time.year
111
+ tyear -= 1 if to_time.month < 3
112
+ leap_years = (fyear > tyear) ? 0 : (fyear..tyear).count{|x| Date.leap?(x)}
113
+ minute_offset_for_leap_year = leap_years * 1440
114
+ # Discount the leap year days when calculating year distance.
115
+ # e.g. if there are 20 leap year days between 2 dates having the same day
116
+ # and month then the based on 365 days calculation
117
+ # the distance in years will come out to over 80 years when in written
118
+ # English it would read better as about 80 years.
119
+ minutes_with_offset = distance_in_minutes - minute_offset_for_leap_year
120
+ else
121
+ minutes_with_offset = distance_in_minutes
122
+ end
123
+ remainder = (minutes_with_offset % 525600)
124
+ distance_in_years = (minutes_with_offset.div 525600)
125
+ if remainder < 131400
126
+ locale.t(:about_x_years, :count => distance_in_years)
127
+ elsif remainder < 394200
128
+ locale.t(:over_x_years, :count => distance_in_years)
129
+ else
130
+ locale.t(:almost_x_years, :count => distance_in_years + 1)
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ # Like <tt>distance_of_time_in_words</tt>, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
137
+ #
138
+ # time_ago_in_words(3.minutes.from_now) # => 3 minutes
139
+ # time_ago_in_words(3.minutes.ago) # => 3 minutes
140
+ # time_ago_in_words(Time.now - 15.hours) # => about 15 hours
141
+ # time_ago_in_words(Time.now) # => less than a minute
142
+ # time_ago_in_words(Time.now, include_seconds: true) # => less than 5 seconds
143
+ #
144
+ # from_time = Time.now - 3.days - 14.minutes - 25.seconds
145
+ # time_ago_in_words(from_time) # => 3 days
146
+ #
147
+ # from_time = (3.days + 14.minutes + 25.seconds).ago
148
+ # time_ago_in_words(from_time) # => 3 days
149
+ #
150
+ # Note that you cannot pass a <tt>Numeric</tt> value to <tt>time_ago_in_words</tt>.
151
+ #
152
+ def time_ago_in_words(from_time, include_seconds_or_options = {})
153
+ distance_of_time_in_words(from_time, Time.now, include_seconds_or_options)
154
+ end
155
+
156
+ alias_method :distance_of_time_in_words_to_now, :time_ago_in_words
157
+
158
+ # Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based
159
+ # attribute (identified by +method+) on an object assigned to the template (identified by +object+).
160
+ #
161
+ # ==== Options
162
+ # * <tt>:use_month_numbers</tt> - Set to true if you want to use month numbers rather than month names (e.g.
163
+ # "2" instead of "February").
164
+ # * <tt>:use_two_digit_numbers</tt> - Set to true if you want to display two digit month and day numbers (e.g.
165
+ # "02" instead of "February" and "08" instead of "8").
166
+ # * <tt>:use_short_month</tt> - Set to true if you want to use abbreviated month names instead of full
167
+ # month names (e.g. "Feb" instead of "February").
168
+ # * <tt>:add_month_numbers</tt> - Set to true if you want to use both month numbers and month names (e.g.
169
+ # "2 - February" instead of "February").
170
+ # * <tt>:use_month_names</tt> - Set to an array with 12 month names if you want to customize month names.
171
+ # Note: You can also use Rails' i18n functionality for this.
172
+ # * <tt>:date_separator</tt> - Specifies a string to separate the date fields. Default is "" (i.e. nothing).
173
+ # * <tt>:start_year</tt> - Set the start year for the year select. Default is <tt>Time.now.year - 5</tt>.
174
+ # * <tt>:end_year</tt> - Set the end year for the year select. Default is <tt>Time.now.year + 5</tt>.
175
+ # * <tt>:discard_day</tt> - Set to true if you don't want to show a day select. This includes the day
176
+ # as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the
177
+ # first of the given month in order to not create invalid dates like 31 February.
178
+ # * <tt>:discard_month</tt> - Set to true if you don't want to show a month select. This includes the month
179
+ # as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true.
180
+ # * <tt>:discard_year</tt> - Set to true if you don't want to show a year select. This includes the year
181
+ # as a hidden field instead of showing a select field.
182
+ # * <tt>:order</tt> - Set to an array containing <tt>:day</tt>, <tt>:month</tt> and <tt>:year</tt> to
183
+ # customize the order in which the select fields are shown. If you leave out any of the symbols, the respective
184
+ # select will not be shown (like when you set <tt>discard_xxx: true</tt>. Defaults to the order defined in
185
+ # the respective locale (e.g. [:year, :month, :day] in the en locale that ships with Rails).
186
+ # * <tt>:include_blank</tt> - Include a blank option in every select field so it's possible to set empty
187
+ # dates.
188
+ # * <tt>:default</tt> - Set a default date if the affected date isn't set or is nil.
189
+ # * <tt>:selected</tt> - Set a date that overrides the actual value.
190
+ # * <tt>:disabled</tt> - Set to true if you want show the select fields as disabled.
191
+ # * <tt>:prompt</tt> - Set to true (for a generic prompt), a prompt string or a hash of prompt strings
192
+ # for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt> and <tt>:second</tt>.
193
+ # Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds)
194
+ # or the given prompt string.
195
+ # * <tt>:with_css_classes</tt> - Set to true if you want assign different styles for 'select' tags. This option
196
+ # automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags.
197
+ #
198
+ # If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
199
+ #
200
+ # NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed.
201
+ #
202
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute.
203
+ # date_select("article", "written_on")
204
+ #
205
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
206
+ # # with the year in the year drop down box starting at 1995.
207
+ # date_select("article", "written_on", start_year: 1995)
208
+ #
209
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
210
+ # # with the year in the year drop down box starting at 1995, numbers used for months instead of words,
211
+ # # and without a day select box.
212
+ # date_select("article", "written_on", start_year: 1995, use_month_numbers: true,
213
+ # discard_day: true, include_blank: true)
214
+ #
215
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
216
+ # # with two digit numbers used for months and days.
217
+ # date_select("article", "written_on", use_two_digit_numbers: true)
218
+ #
219
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
220
+ # # with the fields ordered as day, month, year rather than month, day, year.
221
+ # date_select("article", "written_on", order: [:day, :month, :year])
222
+ #
223
+ # # Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
224
+ # # lacking a year field.
225
+ # date_select("user", "birthday", order: [:month, :day])
226
+ #
227
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
228
+ # # which is initially set to the date 3 days from the current date
229
+ # date_select("article", "written_on", default: 3.days.from_now)
230
+ #
231
+ # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
232
+ # # which is set in the form with todays date, regardless of the value in the Active Record object.
233
+ # date_select("article", "written_on", selected: Date.today)
234
+ #
235
+ # # Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute
236
+ # # that will have a default day of 20.
237
+ # date_select("credit_card", "bill_due", default: { day: 20 })
238
+ #
239
+ # # Generates a date select with custom prompts.
240
+ # date_select("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' })
241
+ #
242
+ # The selects are prepared for multi-parameter assignment to an Active Record object.
243
+ #
244
+ # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that
245
+ # all month choices are valid.
246
+ def date_select(object_name, method, options = {}, html_options = {})
247
+ Tags::DateSelect.new(object_name, method, self, options, html_options).render
248
+ end
249
+
250
+ # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a
251
+ # specified time-based attribute (identified by +method+) on an object assigned to the template (identified by
252
+ # +object+). You can include the seconds with <tt>:include_seconds</tt>. You can get hours in the AM/PM format
253
+ # with <tt>:ampm</tt> option.
254
+ #
255
+ # This method will also generate 3 input hidden tags, for the actual year, month and day unless the option
256
+ # <tt>:ignore_date</tt> is set to +true+. If you set the <tt>:ignore_date</tt> to +true+, you must have a
257
+ # +date_select+ on the same method within the form otherwise an exception will be raised.
258
+ #
259
+ # If anything is passed in the html_options hash it will be applied to every select tag in the set.
260
+ #
261
+ # # Creates a time select tag that, when POSTed, will be stored in the article variable in the sunrise attribute.
262
+ # time_select("article", "sunrise")
263
+ #
264
+ # # Creates a time select tag with a seconds field that, when POSTed, will be stored in the article variables in
265
+ # # the sunrise attribute.
266
+ # time_select("article", "start_time", include_seconds: true)
267
+ #
268
+ # # You can set the <tt>:minute_step</tt> to 15 which will give you: 00, 15, 30 and 45.
269
+ # time_select 'game', 'game_time', {minute_step: 15}
270
+ #
271
+ # # Creates a time select tag with a custom prompt. Use <tt>prompt: true</tt> for generic prompts.
272
+ # time_select("article", "written_on", prompt: {hour: 'Choose hour', minute: 'Choose minute', second: 'Choose seconds'})
273
+ # time_select("article", "written_on", prompt: {hour: true}) # generic prompt for hours
274
+ # time_select("article", "written_on", prompt: true) # generic prompts for all
275
+ #
276
+ # # You can set :ampm option to true which will show the hours as: 12 PM, 01 AM .. 11 PM.
277
+ # time_select 'game', 'game_time', {ampm: true}
278
+ #
279
+ # The selects are prepared for multi-parameter assignment to an Active Record object.
280
+ #
281
+ # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that
282
+ # all month choices are valid.
283
+ def time_select(object_name, method, options = {}, html_options = {})
284
+ Tags::TimeSelect.new(object_name, method, self, options, html_options).render
285
+ end
286
+
287
+ # Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a
288
+ # specified datetime-based attribute (identified by +method+) on an object assigned to the template (identified
289
+ # by +object+).
290
+ #
291
+ # If anything is passed in the html_options hash it will be applied to every select tag in the set.
292
+ #
293
+ # # Generates a datetime select that, when POSTed, will be stored in the article variable in the written_on
294
+ # # attribute.
295
+ # datetime_select("article", "written_on")
296
+ #
297
+ # # Generates a datetime select with a year select that starts at 1995 that, when POSTed, will be stored in the
298
+ # # article variable in the written_on attribute.
299
+ # datetime_select("article", "written_on", start_year: 1995)
300
+ #
301
+ # # Generates a datetime select with a default value of 3 days from the current time that, when POSTed, will
302
+ # # be stored in the trip variable in the departing attribute.
303
+ # datetime_select("trip", "departing", default: 3.days.from_now)
304
+ #
305
+ # # Generate a datetime select with hours in the AM/PM format
306
+ # datetime_select("article", "written_on", ampm: true)
307
+ #
308
+ # # Generates a datetime select that discards the type that, when POSTed, will be stored in the article variable
309
+ # # as the written_on attribute.
310
+ # datetime_select("article", "written_on", discard_type: true)
311
+ #
312
+ # # Generates a datetime select with a custom prompt. Use <tt>prompt: true</tt> for generic prompts.
313
+ # datetime_select("article", "written_on", prompt: {day: 'Choose day', month: 'Choose month', year: 'Choose year'})
314
+ # datetime_select("article", "written_on", prompt: {hour: true}) # generic prompt for hours
315
+ # datetime_select("article", "written_on", prompt: true) # generic prompts for all
316
+ #
317
+ # The selects are prepared for multi-parameter assignment to an Active Record object.
318
+ def datetime_select(object_name, method, options = {}, html_options = {})
319
+ Tags::DatetimeSelect.new(object_name, method, self, options, html_options).render
320
+ end
321
+
322
+ # Returns a set of html select-tags (one for year, month, day, hour, minute, and second) pre-selected with the
323
+ # +datetime+. It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with
324
+ # an array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not
325
+ # supply a Symbol, it will be appended onto the <tt>:order</tt> passed in. You can also add
326
+ # <tt>:date_separator</tt>, <tt>:datetime_separator</tt> and <tt>:time_separator</tt> keys to the +options+ to
327
+ # control visual display of the elements.
328
+ #
329
+ # If anything is passed in the html_options hash it will be applied to every select tag in the set.
330
+ #
331
+ # my_date_time = Time.now + 4.days
332
+ #
333
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today).
334
+ # select_datetime(my_date_time)
335
+ #
336
+ # # Generates a datetime select that defaults to today (no specified datetime)
337
+ # select_datetime()
338
+ #
339
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
340
+ # # with the fields ordered year, month, day rather than month, day, year.
341
+ # select_datetime(my_date_time, order: [:year, :month, :day])
342
+ #
343
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
344
+ # # with a '/' between each date field.
345
+ # select_datetime(my_date_time, date_separator: '/')
346
+ #
347
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
348
+ # # with a date fields separated by '/', time fields separated by '' and the date and time fields
349
+ # # separated by a comma (',').
350
+ # select_datetime(my_date_time, date_separator: '/', time_separator: '', datetime_separator: ',')
351
+ #
352
+ # # Generates a datetime select that discards the type of the field and defaults to the datetime in
353
+ # # my_date_time (four days after today)
354
+ # select_datetime(my_date_time, discard_type: true)
355
+ #
356
+ # # Generate a datetime field with hours in the AM/PM format
357
+ # select_datetime(my_date_time, ampm: true)
358
+ #
359
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
360
+ # # prefixed with 'payday' rather than 'date'
361
+ # select_datetime(my_date_time, prefix: 'payday')
362
+ #
363
+ # # Generates a datetime select with a custom prompt. Use <tt>prompt: true</tt> for generic prompts.
364
+ # select_datetime(my_date_time, prompt: {day: 'Choose day', month: 'Choose month', year: 'Choose year'})
365
+ # select_datetime(my_date_time, prompt: {hour: true}) # generic prompt for hours
366
+ # select_datetime(my_date_time, prompt: true) # generic prompts for all
367
+ def select_datetime(datetime = Time.current, options = {}, html_options = {})
368
+ DateTimeSelector.new(datetime, options, html_options).select_datetime
369
+ end
370
+
371
+ # Returns a set of html select-tags (one for year, month, and day) pre-selected with the +date+.
372
+ # It's possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
373
+ # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order.
374
+ # If the array passed to the <tt>:order</tt> option does not contain all the three symbols, all tags will be hidden.
375
+ #
376
+ # If anything is passed in the html_options hash it will be applied to every select tag in the set.
377
+ #
378
+ # my_date = Time.now + 6.days
379
+ #
380
+ # # Generates a date select that defaults to the date in my_date (six days after today).
381
+ # select_date(my_date)
382
+ #
383
+ # # Generates a date select that defaults to today (no specified date).
384
+ # select_date()
385
+ #
386
+ # # Generates a date select that defaults to the date in my_date (six days after today)
387
+ # # with the fields ordered year, month, day rather than month, day, year.
388
+ # select_date(my_date, order: [:year, :month, :day])
389
+ #
390
+ # # Generates a date select that discards the type of the field and defaults to the date in
391
+ # # my_date (six days after today).
392
+ # select_date(my_date, discard_type: true)
393
+ #
394
+ # # Generates a date select that defaults to the date in my_date,
395
+ # # which has fields separated by '/'.
396
+ # select_date(my_date, date_separator: '/')
397
+ #
398
+ # # Generates a date select that defaults to the datetime in my_date (six days after today)
399
+ # # prefixed with 'payday' rather than 'date'.
400
+ # select_date(my_date, prefix: 'payday')
401
+ #
402
+ # # Generates a date select with a custom prompt. Use <tt>prompt: true</tt> for generic prompts.
403
+ # select_date(my_date, prompt: {day: 'Choose day', month: 'Choose month', year: 'Choose year'})
404
+ # select_date(my_date, prompt: {hour: true}) # generic prompt for hours
405
+ # select_date(my_date, prompt: true) # generic prompts for all
406
+ def select_date(date = Date.current, options = {}, html_options = {})
407
+ DateTimeSelector.new(date, options, html_options).select_date
408
+ end
409
+
410
+ # Returns a set of html select-tags (one for hour and minute).
411
+ # You can set <tt>:time_separator</tt> key to format the output, and
412
+ # the <tt>:include_seconds</tt> option to include an input for seconds.
413
+ #
414
+ # If anything is passed in the html_options hash it will be applied to every select tag in the set.
415
+ #
416
+ # my_time = Time.now + 5.days + 7.hours + 3.minutes + 14.seconds
417
+ #
418
+ # # Generates a time select that defaults to the time in my_time.
419
+ # select_time(my_time)
420
+ #
421
+ # # Generates a time select that defaults to the current time (no specified time).
422
+ # select_time()
423
+ #
424
+ # # Generates a time select that defaults to the time in my_time,
425
+ # # which has fields separated by ':'.
426
+ # select_time(my_time, time_separator: ':')
427
+ #
428
+ # # Generates a time select that defaults to the time in my_time,
429
+ # # that also includes an input for seconds.
430
+ # select_time(my_time, include_seconds: true)
431
+ #
432
+ # # Generates a time select that defaults to the time in my_time, that has fields
433
+ # # separated by ':' and includes an input for seconds.
434
+ # select_time(my_time, time_separator: ':', include_seconds: true)
435
+ #
436
+ # # Generate a time select field with hours in the AM/PM format
437
+ # select_time(my_time, ampm: true)
438
+ #
439
+ # # Generates a time select field with hours that range from 2 to 14
440
+ # select_time(my_time, start_hour: 2, end_hour: 14)
441
+ #
442
+ # # Generates a time select with a custom prompt. Use <tt>:prompt</tt> to true for generic prompts.
443
+ # select_time(my_time, prompt: {day: 'Choose day', month: 'Choose month', year: 'Choose year'})
444
+ # select_time(my_time, prompt: {hour: true}) # generic prompt for hours
445
+ # select_time(my_time, prompt: true) # generic prompts for all
446
+ def select_time(datetime = Time.current, options = {}, html_options = {})
447
+ DateTimeSelector.new(datetime, options, html_options).select_time
448
+ end
449
+
450
+ # Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
451
+ # The <tt>datetime</tt> can be either a +Time+ or +DateTime+ object or an integer.
452
+ # Override the field name using the <tt>:field_name</tt> option, 'second' by default.
453
+ #
454
+ # my_time = Time.now + 16.minutes
455
+ #
456
+ # # Generates a select field for seconds that defaults to the seconds for the time in my_time.
457
+ # select_second(my_time)
458
+ #
459
+ # # Generates a select field for seconds that defaults to the number given.
460
+ # select_second(33)
461
+ #
462
+ # # Generates a select field for seconds that defaults to the seconds for the time in my_time
463
+ # # that is named 'interval' rather than 'second'.
464
+ # select_second(my_time, field_name: 'interval')
465
+ #
466
+ # # Generates a select field for seconds with a custom prompt. Use <tt>prompt: true</tt> for a
467
+ # # generic prompt.
468
+ # select_second(14, prompt: 'Choose seconds')
469
+ def select_second(datetime, options = {}, html_options = {})
470
+ DateTimeSelector.new(datetime, options, html_options).select_second
471
+ end
472
+
473
+ # Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.
474
+ # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute
475
+ # selected. The <tt>datetime</tt> can be either a +Time+ or +DateTime+ object or an integer.
476
+ # Override the field name using the <tt>:field_name</tt> option, 'minute' by default.
477
+ #
478
+ # my_time = Time.now + 6.hours
479
+ #
480
+ # # Generates a select field for minutes that defaults to the minutes for the time in my_time.
481
+ # select_minute(my_time)
482
+ #
483
+ # # Generates a select field for minutes that defaults to the number given.
484
+ # select_minute(14)
485
+ #
486
+ # # Generates a select field for minutes that defaults to the minutes for the time in my_time
487
+ # # that is named 'moment' rather than 'minute'.
488
+ # select_minute(my_time, field_name: 'moment')
489
+ #
490
+ # # Generates a select field for minutes with a custom prompt. Use <tt>prompt: true</tt> for a
491
+ # # generic prompt.
492
+ # select_minute(14, prompt: 'Choose minutes')
493
+ def select_minute(datetime, options = {}, html_options = {})
494
+ DateTimeSelector.new(datetime, options, html_options).select_minute
495
+ end
496
+
497
+ # Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
498
+ # The <tt>datetime</tt> can be either a +Time+ or +DateTime+ object or an integer.
499
+ # Override the field name using the <tt>:field_name</tt> option, 'hour' by default.
500
+ #
501
+ # my_time = Time.now + 6.hours
502
+ #
503
+ # # Generates a select field for hours that defaults to the hour for the time in my_time.
504
+ # select_hour(my_time)
505
+ #
506
+ # # Generates a select field for hours that defaults to the number given.
507
+ # select_hour(13)
508
+ #
509
+ # # Generates a select field for hours that defaults to the hour for the time in my_time
510
+ # # that is named 'stride' rather than 'hour'.
511
+ # select_hour(my_time, field_name: 'stride')
512
+ #
513
+ # # Generates a select field for hours with a custom prompt. Use <tt>prompt: true</tt> for a
514
+ # # generic prompt.
515
+ # select_hour(13, prompt: 'Choose hour')
516
+ #
517
+ # # Generate a select field for hours in the AM/PM format
518
+ # select_hour(my_time, ampm: true)
519
+ #
520
+ # # Generates a select field that includes options for hours from 2 to 14.
521
+ # select_hour(my_time, start_hour: 2, end_hour: 14)
522
+ def select_hour(datetime, options = {}, html_options = {})
523
+ DateTimeSelector.new(datetime, options, html_options).select_hour
524
+ end
525
+
526
+ # Returns a select tag with options for each of the days 1 through 31 with the current day selected.
527
+ # The <tt>date</tt> can also be substituted for a day number.
528
+ # If you want to display days with a leading zero set the <tt>:use_two_digit_numbers</tt> key in +options+ to true.
529
+ # Override the field name using the <tt>:field_name</tt> option, 'day' by default.
530
+ #
531
+ # my_date = Time.now + 2.days
532
+ #
533
+ # # Generates a select field for days that defaults to the day for the date in my_date.
534
+ # select_day(my_date)
535
+ #
536
+ # # Generates a select field for days that defaults to the number given.
537
+ # select_day(5)
538
+ #
539
+ # # Generates a select field for days that defaults to the number given, but displays it with two digits.
540
+ # select_day(5, use_two_digit_numbers: true)
541
+ #
542
+ # # Generates a select field for days that defaults to the day for the date in my_date
543
+ # # that is named 'due' rather than 'day'.
544
+ # select_day(my_date, field_name: 'due')
545
+ #
546
+ # # Generates a select field for days with a custom prompt. Use <tt>prompt: true</tt> for a
547
+ # # generic prompt.
548
+ # select_day(5, prompt: 'Choose day')
549
+ def select_day(date, options = {}, html_options = {})
550
+ DateTimeSelector.new(date, options, html_options).select_day
551
+ end
552
+
553
+ # Returns a select tag with options for each of the months January through December with the current month
554
+ # selected. The month names are presented as keys (what's shown to the user) and the month numbers (1-12) are
555
+ # used as values (what's submitted to the server). It's also possible to use month numbers for the presentation
556
+ # instead of names -- set the <tt>:use_month_numbers</tt> key in +options+ to true for this to happen. If you
557
+ # want both numbers and names, set the <tt>:add_month_numbers</tt> key in +options+ to true. If you would prefer
558
+ # to show month names as abbreviations, set the <tt>:use_short_month</tt> key in +options+ to true. If you want
559
+ # to use your own month names, set the <tt>:use_month_names</tt> key in +options+ to an array of 12 month names.
560
+ # If you want to display months with a leading zero set the <tt>:use_two_digit_numbers</tt> key in +options+ to true.
561
+ # Override the field name using the <tt>:field_name</tt> option, 'month' by default.
562
+ #
563
+ # # Generates a select field for months that defaults to the current month that
564
+ # # will use keys like "January", "March".
565
+ # select_month(Date.today)
566
+ #
567
+ # # Generates a select field for months that defaults to the current month that
568
+ # # is named "start" rather than "month".
569
+ # select_month(Date.today, field_name: 'start')
570
+ #
571
+ # # Generates a select field for months that defaults to the current month that
572
+ # # will use keys like "1", "3".
573
+ # select_month(Date.today, use_month_numbers: true)
574
+ #
575
+ # # Generates a select field for months that defaults to the current month that
576
+ # # will use keys like "1 - January", "3 - March".
577
+ # select_month(Date.today, add_month_numbers: true)
578
+ #
579
+ # # Generates a select field for months that defaults to the current month that
580
+ # # will use keys like "Jan", "Mar".
581
+ # select_month(Date.today, use_short_month: true)
582
+ #
583
+ # # Generates a select field for months that defaults to the current month that
584
+ # # will use keys like "Januar", "Marts."
585
+ # select_month(Date.today, use_month_names: %w(Januar Februar Marts ...))
586
+ #
587
+ # # Generates a select field for months that defaults to the current month that
588
+ # # will use keys with two digit numbers like "01", "03".
589
+ # select_month(Date.today, use_two_digit_numbers: true)
590
+ #
591
+ # # Generates a select field for months with a custom prompt. Use <tt>prompt: true</tt> for a
592
+ # # generic prompt.
593
+ # select_month(14, prompt: 'Choose month')
594
+ def select_month(date, options = {}, html_options = {})
595
+ DateTimeSelector.new(date, options, html_options).select_month
596
+ end
597
+
598
+ # Returns a select tag with options for each of the five years on each side of the current, which is selected.
599
+ # The five year radius can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the
600
+ # +options+. Both ascending and descending year lists are supported by making <tt>:start_year</tt> less than or
601
+ # greater than <tt>:end_year</tt>. The <tt>date</tt> can also be substituted for a year given as a number.
602
+ # Override the field name using the <tt>:field_name</tt> option, 'year' by default.
603
+ #
604
+ # # Generates a select field for years that defaults to the current year that
605
+ # # has ascending year values.
606
+ # select_year(Date.today, start_year: 1992, end_year: 2007)
607
+ #
608
+ # # Generates a select field for years that defaults to the current year that
609
+ # # is named 'birth' rather than 'year'.
610
+ # select_year(Date.today, field_name: 'birth')
611
+ #
612
+ # # Generates a select field for years that defaults to the current year that
613
+ # # has descending year values.
614
+ # select_year(Date.today, start_year: 2005, end_year: 1900)
615
+ #
616
+ # # Generates a select field for years that defaults to the year 2006 that
617
+ # # has ascending year values.
618
+ # select_year(2006, start_year: 2000, end_year: 2010)
619
+ #
620
+ # # Generates a select field for years with a custom prompt. Use <tt>prompt: true</tt> for a
621
+ # # generic prompt.
622
+ # select_year(14, prompt: 'Choose year')
623
+ def select_year(date, options = {}, html_options = {})
624
+ DateTimeSelector.new(date, options, html_options).select_year
625
+ end
626
+
627
+ # Returns an html time tag for the given date or time.
628
+ #
629
+ # time_tag Date.today # =>
630
+ # <time datetime="2010-11-04">November 04, 2010</time>
631
+ # time_tag Time.now # =>
632
+ # <time datetime="2010-11-04T17:55:45+01:00">November 04, 2010 17:55</time>
633
+ # time_tag Date.yesterday, 'Yesterday' # =>
634
+ # <time datetime="2010-11-03">Yesterday</time>
635
+ # time_tag Date.today, pubdate: true # =>
636
+ # <time datetime="2010-11-04" pubdate="pubdate">November 04, 2010</time>
637
+ # time_tag Date.today, datetime: Date.today.strftime('%G-W%V') # =>
638
+ # <time datetime="2010-W44">November 04, 2010</time>
639
+ #
640
+ # <%= time_tag Time.now do %>
641
+ # <span>Right now</span>
642
+ # <% end %>
643
+ # # => <time datetime="2010-11-04T17:55:45+01:00"><span>Right now</span></time>
644
+ def time_tag(date_or_time, *args, &block)
645
+ options = args.extract_options!
646
+ format = options.delete(:format) || :long
647
+ content = args.first || I18n.l(date_or_time, :format => format)
648
+ datetime = date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.iso8601
649
+
650
+ content_tag(:time, content, options.reverse_merge(:datetime => datetime), &block)
651
+ end
652
+ end
653
+
654
+ class DateTimeSelector #:nodoc:
655
+ include ActionView::Helpers::TagHelper
656
+
657
+ DEFAULT_PREFIX = 'date'.freeze
658
+ POSITION = {
659
+ :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6
660
+ }.freeze
661
+
662
+ AMPM_TRANSLATION = Hash[
663
+ [[0, "12 AM"], [1, "01 AM"], [2, "02 AM"], [3, "03 AM"],
664
+ [4, "04 AM"], [5, "05 AM"], [6, "06 AM"], [7, "07 AM"],
665
+ [8, "08 AM"], [9, "09 AM"], [10, "10 AM"], [11, "11 AM"],
666
+ [12, "12 PM"], [13, "01 PM"], [14, "02 PM"], [15, "03 PM"],
667
+ [16, "04 PM"], [17, "05 PM"], [18, "06 PM"], [19, "07 PM"],
668
+ [20, "08 PM"], [21, "09 PM"], [22, "10 PM"], [23, "11 PM"]]
669
+ ].freeze
670
+
671
+ def initialize(datetime, options = {}, html_options = {})
672
+ @options = options.dup
673
+ @html_options = html_options.dup
674
+ @datetime = datetime
675
+ @options[:datetime_separator] ||= ' &mdash; '
676
+ @options[:time_separator] ||= ' : '
677
+ end
678
+
679
+ def select_datetime
680
+ order = date_order.dup
681
+ order -= [:hour, :minute, :second]
682
+ @options[:discard_year] ||= true unless order.include?(:year)
683
+ @options[:discard_month] ||= true unless order.include?(:month)
684
+ @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day)
685
+ @options[:discard_minute] ||= true if @options[:discard_hour]
686
+ @options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute]
687
+
688
+ set_day_if_discarded
689
+
690
+ if @options[:tag] && @options[:ignore_date]
691
+ select_time
692
+ else
693
+ [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
694
+ order += [:hour, :minute, :second] unless @options[:discard_hour]
695
+
696
+ build_selects_from_types(order)
697
+ end
698
+ end
699
+
700
+ def select_date
701
+ order = date_order.dup
702
+
703
+ @options[:discard_hour] = true
704
+ @options[:discard_minute] = true
705
+ @options[:discard_second] = true
706
+
707
+ @options[:discard_year] ||= true unless order.include?(:year)
708
+ @options[:discard_month] ||= true unless order.include?(:month)
709
+ @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day)
710
+
711
+ set_day_if_discarded
712
+
713
+ [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
714
+
715
+ build_selects_from_types(order)
716
+ end
717
+
718
+ def select_time
719
+ order = []
720
+
721
+ @options[:discard_month] = true
722
+ @options[:discard_year] = true
723
+ @options[:discard_day] = true
724
+ @options[:discard_second] ||= true unless @options[:include_seconds]
725
+
726
+ order += [:year, :month, :day] unless @options[:ignore_date]
727
+
728
+ order += [:hour, :minute]
729
+ order << :second if @options[:include_seconds]
730
+
731
+ build_selects_from_types(order)
732
+ end
733
+
734
+ def select_second
735
+ if @options[:use_hidden] || @options[:discard_second]
736
+ build_hidden(:second, sec) if @options[:include_seconds]
737
+ else
738
+ build_options_and_select(:second, sec)
739
+ end
740
+ end
741
+
742
+ def select_minute
743
+ if @options[:use_hidden] || @options[:discard_minute]
744
+ build_hidden(:minute, min)
745
+ else
746
+ build_options_and_select(:minute, min, :step => @options[:minute_step])
747
+ end
748
+ end
749
+
750
+ def select_hour
751
+ if @options[:use_hidden] || @options[:discard_hour]
752
+ build_hidden(:hour, hour)
753
+ else
754
+ options = {}
755
+ options[:ampm] = @options[:ampm] || false
756
+ options[:start] = @options[:start_hour] || 0
757
+ options[:end] = @options[:end_hour] || 23
758
+ build_options_and_select(:hour, hour, options)
759
+ end
760
+ end
761
+
762
+ def select_day
763
+ if @options[:use_hidden] || @options[:discard_day]
764
+ build_hidden(:day, day || 1)
765
+ else
766
+ build_options_and_select(:day, day, :start => 1, :end => 31, :leading_zeros => false, :use_two_digit_numbers => @options[:use_two_digit_numbers])
767
+ end
768
+ end
769
+
770
+ def select_month
771
+ if @options[:use_hidden] || @options[:discard_month]
772
+ build_hidden(:month, month || 1)
773
+ else
774
+ month_options = []
775
+ 1.upto(12) do |month_number|
776
+ options = { :value => month_number }
777
+ options[:selected] = "selected" if month == month_number
778
+ month_options << content_tag(:option, month_name(month_number), options) + "\n"
779
+ end
780
+ build_select(:month, month_options.join)
781
+ end
782
+ end
783
+
784
+ def select_year
785
+ if !@datetime || @datetime == 0
786
+ val = '1'
787
+ middle_year = Date.today.year
788
+ else
789
+ val = middle_year = year
790
+ end
791
+
792
+ if @options[:use_hidden] || @options[:discard_year]
793
+ build_hidden(:year, val)
794
+ else
795
+ options = {}
796
+ options[:start] = @options[:start_year] || middle_year - 5
797
+ options[:end] = @options[:end_year] || middle_year + 5
798
+ options[:step] = options[:start] < options[:end] ? 1 : -1
799
+ options[:leading_zeros] = false
800
+ options[:max_years_allowed] = @options[:max_years_allowed] || 1000
801
+
802
+ if (options[:end] - options[:start]).abs > options[:max_years_allowed]
803
+ raise ArgumentError, "There are too many years options to be built. Are you sure you haven't mistyped something? You can provide the :max_years_allowed parameter."
804
+ end
805
+
806
+ build_options_and_select(:year, val, options)
807
+ end
808
+ end
809
+
810
+ private
811
+ %w( sec min hour day month year ).each do |method|
812
+ define_method(method) do
813
+ @datetime.kind_of?(Numeric) ? @datetime : @datetime.send(method) if @datetime
814
+ end
815
+ end
816
+
817
+ # If the day is hidden, the day should be set to the 1st so all month and year choices are
818
+ # valid. Otherwise, February 31st or February 29th, 2011 can be selected, which are invalid.
819
+ def set_day_if_discarded
820
+ if @datetime && @options[:discard_day]
821
+ @datetime = @datetime.change(:day => 1)
822
+ end
823
+ end
824
+
825
+ # Returns translated month names, but also ensures that a custom month
826
+ # name array has a leading nil element.
827
+ def month_names
828
+ @month_names ||= begin
829
+ month_names = @options[:use_month_names] || translated_month_names
830
+ month_names.unshift(nil) if month_names.size < 13
831
+ month_names
832
+ end
833
+ end
834
+
835
+ # Returns translated month names.
836
+ # => [nil, "January", "February", "March",
837
+ # "April", "May", "June", "July",
838
+ # "August", "September", "October",
839
+ # "November", "December"]
840
+ #
841
+ # If <tt>:use_short_month</tt> option is set
842
+ # => [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
843
+ # "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
844
+ def translated_month_names
845
+ key = @options[:use_short_month] ? :'date.abbr_month_names' : :'date.month_names'
846
+ I18n.translate(key, :locale => @options[:locale])
847
+ end
848
+
849
+ # Lookup month name for number.
850
+ # month_name(1) => "January"
851
+ #
852
+ # If <tt>:use_month_numbers</tt> option is passed
853
+ # month_name(1) => 1
854
+ #
855
+ # If <tt>:use_two_month_numbers</tt> option is passed
856
+ # month_name(1) => '01'
857
+ #
858
+ # If <tt>:add_month_numbers</tt> option is passed
859
+ # month_name(1) => "1 - January"
860
+ def month_name(number)
861
+ if @options[:use_month_numbers]
862
+ number
863
+ elsif @options[:use_two_digit_numbers]
864
+ sprintf "%02d", number
865
+ elsif @options[:add_month_numbers]
866
+ "#{number} - #{month_names[number]}"
867
+ else
868
+ month_names[number]
869
+ end
870
+ end
871
+
872
+ def date_order
873
+ @date_order ||= @options[:order] || translated_date_order
874
+ end
875
+
876
+ def translated_date_order
877
+ date_order = I18n.translate(:'date.order', :locale => @options[:locale], :default => [])
878
+ date_order = date_order.map { |element| element.to_sym }
879
+
880
+ forbidden_elements = date_order - [:year, :month, :day]
881
+ if forbidden_elements.any?
882
+ raise StandardError,
883
+ "#{@options[:locale]}.date.order only accepts :year, :month and :day"
884
+ end
885
+
886
+ date_order
887
+ end
888
+
889
+ # Build full select tag from date type and options.
890
+ def build_options_and_select(type, selected, options = {})
891
+ build_select(type, build_options(selected, options))
892
+ end
893
+
894
+ # Build select option html from date value and options.
895
+ # build_options(15, start: 1, end: 31)
896
+ # => "<option value="1">1</option>
897
+ # <option value="2">2</option>
898
+ # <option value="3">3</option>..."
899
+ #
900
+ # If <tt>use_two_digit_numbers: true</tt> option is passed
901
+ # build_options(15, start: 1, end: 31, use_two_digit_numbers: true)
902
+ # => "<option value="1">01</option>
903
+ # <option value="2">02</option>
904
+ # <option value="3">03</option>..."
905
+ #
906
+ # If <tt>:step</tt> options is passed
907
+ # build_options(15, start: 1, end: 31, step: 2)
908
+ # => "<option value="1">1</option>
909
+ # <option value="3">3</option>
910
+ # <option value="5">5</option>..."
911
+ def build_options(selected, options = {})
912
+ options = {
913
+ leading_zeros: true, ampm: false, use_two_digit_numbers: false
914
+ }.merge!(options)
915
+
916
+ start = options.delete(:start) || 0
917
+ stop = options.delete(:end) || 59
918
+ step = options.delete(:step) || 1
919
+ leading_zeros = options.delete(:leading_zeros)
920
+
921
+ select_options = []
922
+ start.step(stop, step) do |i|
923
+ value = leading_zeros ? sprintf("%02d", i) : i
924
+ tag_options = { :value => value }
925
+ tag_options[:selected] = "selected" if selected == i
926
+ text = options[:use_two_digit_numbers] ? sprintf("%02d", i) : value
927
+ text = options[:ampm] ? AMPM_TRANSLATION[i] : text
928
+ select_options << content_tag(:option, text, tag_options)
929
+ end
930
+
931
+ (select_options.join("\n") + "\n").html_safe
932
+ end
933
+
934
+ # Builds select tag from date type and html select options.
935
+ # build_select(:month, "<option value="1">January</option>...")
936
+ # => "<select id="post_written_on_2i" name="post[written_on(2i)]">
937
+ # <option value="1">January</option>...
938
+ # </select>"
939
+ def build_select(type, select_options_as_html)
940
+ select_options = {
941
+ :id => input_id_from_type(type),
942
+ :name => input_name_from_type(type)
943
+ }.merge!(@html_options)
944
+ select_options[:disabled] = 'disabled' if @options[:disabled]
945
+ select_options[:class] = type if @options[:with_css_classes]
946
+
947
+ select_html = "\n"
948
+ select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
949
+ select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
950
+ select_html << select_options_as_html
951
+
952
+ (content_tag(:select, select_html.html_safe, select_options) + "\n").html_safe
953
+ end
954
+
955
+ # Builds a prompt option tag with supplied options or from default options.
956
+ # prompt_option_tag(:month, prompt: 'Select month')
957
+ # => "<option value="">Select month</option>"
958
+ def prompt_option_tag(type, options)
959
+ prompt = case options
960
+ when Hash
961
+ default_options = {:year => false, :month => false, :day => false, :hour => false, :minute => false, :second => false}
962
+ default_options.merge!(options)[type.to_sym]
963
+ when String
964
+ options
965
+ else
966
+ I18n.translate(:"datetime.prompts.#{type}", :locale => @options[:locale])
967
+ end
968
+
969
+ prompt ? content_tag(:option, prompt, :value => '') : ''
970
+ end
971
+
972
+ # Builds hidden input tag for date part and value.
973
+ # build_hidden(:year, 2008)
974
+ # => "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />"
975
+ def build_hidden(type, value)
976
+ select_options = {
977
+ :type => "hidden",
978
+ :id => input_id_from_type(type),
979
+ :name => input_name_from_type(type),
980
+ :value => value
981
+ }.merge!(@html_options.slice(:disabled))
982
+ select_options[:disabled] = 'disabled' if @options[:disabled]
983
+
984
+ tag(:input, select_options) + "\n".html_safe
985
+ end
986
+
987
+ # Returns the name attribute for the input tag.
988
+ # => post[written_on(1i)]
989
+ def input_name_from_type(type)
990
+ prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
991
+ prefix += "[#{@options[:index]}]" if @options.has_key?(:index)
992
+
993
+ field_name = @options[:field_name] || type
994
+ if @options[:include_position]
995
+ field_name += "(#{ActionView::Helpers::DateTimeSelector::POSITION[type]}i)"
996
+ end
997
+
998
+ @options[:discard_type] ? prefix : "#{prefix}[#{field_name}]"
999
+ end
1000
+
1001
+ # Returns the id attribute for the input tag.
1002
+ # => "post_written_on_1i"
1003
+ def input_id_from_type(type)
1004
+ id = input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
1005
+ id = @options[:namespace] + '_' + id if @options[:namespace]
1006
+
1007
+ id
1008
+ end
1009
+
1010
+ # Given an ordering of datetime components, create the selection HTML
1011
+ # and join them with their appropriate separators.
1012
+ def build_selects_from_types(order)
1013
+ select = ''
1014
+ first_visible = order.find { |type| !@options[:"discard_#{type}"] }
1015
+ order.reverse.each do |type|
1016
+ separator = separator(type) unless type == first_visible # don't add before first visible field
1017
+ select.insert(0, separator.to_s + send("select_#{type}").to_s)
1018
+ end
1019
+ select.html_safe
1020
+ end
1021
+
1022
+ # Returns the separator for a given datetime component.
1023
+ def separator(type)
1024
+ return "" if @options[:use_hidden]
1025
+
1026
+ case type
1027
+ when :year, :month, :day
1028
+ @options[:"discard_#{type}"] ? "" : @options[:date_separator]
1029
+ when :hour
1030
+ (@options[:discard_year] && @options[:discard_day]) ? "" : @options[:datetime_separator]
1031
+ when :minute, :second
1032
+ @options[:"discard_#{type}"] ? "" : @options[:time_separator]
1033
+ end
1034
+ end
1035
+ end
1036
+
1037
+ class FormBuilder
1038
+ # Wraps ActionView::Helpers::DateHelper#date_select for form builders:
1039
+ #
1040
+ # <%= form_for @person do |f| %>
1041
+ # <%= f.date_select :birth_date %>
1042
+ # <%= f.submit %>
1043
+ # <% end %>
1044
+ #
1045
+ # Please refer to the documentation of the base helper for details.
1046
+ def date_select(method, options = {}, html_options = {})
1047
+ @template.date_select(@object_name, method, objectify_options(options), html_options)
1048
+ end
1049
+
1050
+ # Wraps ActionView::Helpers::DateHelper#time_select for form builders:
1051
+ #
1052
+ # <%= form_for @race do |f| %>
1053
+ # <%= f.time_select :average_lap %>
1054
+ # <%= f.submit %>
1055
+ # <% end %>
1056
+ #
1057
+ # Please refer to the documentation of the base helper for details.
1058
+ def time_select(method, options = {}, html_options = {})
1059
+ @template.time_select(@object_name, method, objectify_options(options), html_options)
1060
+ end
1061
+
1062
+ # Wraps ActionView::Helpers::DateHelper#datetime_select for form builders:
1063
+ #
1064
+ # <%= form_for @person do |f| %>
1065
+ # <%= f.time_select :last_request_at %>
1066
+ # <%= f.submit %>
1067
+ # <% end %>
1068
+ #
1069
+ # Please refer to the documentation of the base helper for details.
1070
+ def datetime_select(method, options = {}, html_options = {})
1071
+ @template.datetime_select(@object_name, method, objectify_options(options), html_options)
1072
+ end
1073
+ end
1074
+ end
1075
+ end