actionpack 4.0.1 → 4.2.11.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

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