actionpack 4.0.13 → 4.1.0.beta1

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