actionpack 1.13.6 → 2.0.0

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 (317) hide show
  1. data/CHANGELOG +1400 -20
  2. data/MIT-LICENSE +1 -1
  3. data/README +5 -5
  4. data/RUNNING_UNIT_TESTS +4 -5
  5. data/Rakefile +5 -6
  6. data/install.rb +2 -2
  7. data/lib/action_controller.rb +11 -15
  8. data/lib/action_controller/assertions.rb +12 -25
  9. data/lib/action_controller/assertions/dom_assertions.rb +18 -4
  10. data/lib/action_controller/assertions/model_assertions.rb +8 -1
  11. data/lib/action_controller/assertions/response_assertions.rb +35 -12
  12. data/lib/action_controller/assertions/routing_assertions.rb +56 -12
  13. data/lib/action_controller/assertions/selector_assertions.rb +105 -38
  14. data/lib/action_controller/assertions/tag_assertions.rb +28 -15
  15. data/lib/action_controller/base.rb +318 -250
  16. data/lib/action_controller/benchmarking.rb +33 -29
  17. data/lib/action_controller/caching.rb +130 -64
  18. data/lib/action_controller/cgi_ext.rb +16 -0
  19. data/lib/action_controller/cgi_ext/{cookie_performance_fix.rb → cookie.rb} +25 -40
  20. data/lib/action_controller/cgi_ext/query_extension.rb +22 -0
  21. data/lib/action_controller/cgi_ext/session.rb +73 -0
  22. data/lib/action_controller/cgi_ext/stdinput.rb +23 -0
  23. data/lib/action_controller/cgi_process.rb +34 -57
  24. data/lib/action_controller/components.rb +19 -36
  25. data/lib/action_controller/cookies.rb +10 -9
  26. data/lib/action_controller/dispatcher.rb +195 -0
  27. data/lib/action_controller/filters.rb +35 -34
  28. data/lib/action_controller/flash.rb +30 -35
  29. data/lib/action_controller/helpers.rb +121 -47
  30. data/lib/action_controller/http_authentication.rb +126 -0
  31. data/lib/action_controller/integration.rb +105 -101
  32. data/lib/action_controller/layout.rb +59 -47
  33. data/lib/action_controller/mime_responds.rb +57 -68
  34. data/lib/action_controller/mime_type.rb +43 -80
  35. data/lib/action_controller/mime_types.rb +20 -0
  36. data/lib/action_controller/polymorphic_routes.rb +88 -0
  37. data/lib/action_controller/record_identifier.rb +91 -0
  38. data/lib/action_controller/request.rb +553 -88
  39. data/lib/action_controller/request_forgery_protection.rb +126 -0
  40. data/lib/action_controller/request_profiler.rb +138 -0
  41. data/lib/action_controller/rescue.rb +185 -69
  42. data/lib/action_controller/resources.rb +211 -172
  43. data/lib/action_controller/response.rb +49 -8
  44. data/lib/action_controller/routing.rb +359 -236
  45. data/lib/action_controller/routing_optimisation.rb +119 -0
  46. data/lib/action_controller/session/active_record_store.rb +3 -2
  47. data/lib/action_controller/session/cookie_store.rb +161 -0
  48. data/lib/action_controller/session/mem_cache_store.rb +9 -16
  49. data/lib/action_controller/session_management.rb +17 -8
  50. data/lib/action_controller/streaming.rb +6 -3
  51. data/lib/action_controller/templates/rescues/_request_and_response.erb +24 -0
  52. data/lib/action_controller/templates/rescues/{_trace.rhtml → _trace.erb} +0 -0
  53. data/lib/action_controller/templates/rescues/{diagnostics.rhtml → diagnostics.erb} +2 -2
  54. data/lib/action_controller/templates/rescues/{layout.rhtml → layout.erb} +0 -0
  55. data/lib/action_controller/templates/rescues/{missing_template.rhtml → missing_template.erb} +0 -0
  56. data/lib/action_controller/templates/rescues/{routing_error.rhtml → routing_error.erb} +0 -0
  57. data/lib/action_controller/templates/rescues/{template_error.rhtml → template_error.erb} +2 -2
  58. data/lib/action_controller/templates/rescues/{unknown_action.rhtml → unknown_action.erb} +0 -0
  59. data/lib/action_controller/test_case.rb +53 -0
  60. data/lib/action_controller/test_process.rb +59 -46
  61. data/lib/action_controller/url_rewriter.rb +48 -24
  62. data/lib/action_controller/vendor/html-scanner/html/document.rb +7 -4
  63. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +173 -0
  64. data/lib/action_controller/vendor/html-scanner/html/selector.rb +11 -6
  65. data/lib/action_controller/verification.rb +27 -21
  66. data/lib/action_pack.rb +1 -1
  67. data/lib/action_pack/version.rb +4 -4
  68. data/lib/action_view.rb +2 -3
  69. data/lib/action_view/base.rb +218 -63
  70. data/lib/action_view/compiled_templates.rb +1 -2
  71. data/lib/action_view/helpers/active_record_helper.rb +35 -17
  72. data/lib/action_view/helpers/asset_tag_helper.rb +395 -87
  73. data/lib/action_view/helpers/atom_feed_helper.rb +111 -0
  74. data/lib/action_view/helpers/benchmark_helper.rb +12 -5
  75. data/lib/action_view/helpers/cache_helper.rb +29 -0
  76. data/lib/action_view/helpers/capture_helper.rb +97 -63
  77. data/lib/action_view/helpers/date_helper.rb +295 -35
  78. data/lib/action_view/helpers/debug_helper.rb +6 -2
  79. data/lib/action_view/helpers/form_helper.rb +354 -111
  80. data/lib/action_view/helpers/form_options_helper.rb +171 -109
  81. data/lib/action_view/helpers/form_tag_helper.rb +332 -76
  82. data/lib/action_view/helpers/javascript_helper.rb +35 -11
  83. data/lib/action_view/helpers/javascripts/controls.js +484 -354
  84. data/lib/action_view/helpers/javascripts/dragdrop.js +88 -58
  85. data/lib/action_view/helpers/javascripts/effects.js +396 -364
  86. data/lib/action_view/helpers/javascripts/prototype.js +2817 -1107
  87. data/lib/action_view/helpers/number_helper.rb +84 -60
  88. data/lib/action_view/helpers/prototype_helper.rb +419 -43
  89. data/lib/action_view/helpers/record_identification_helper.rb +20 -0
  90. data/lib/action_view/helpers/record_tag_helper.rb +59 -0
  91. data/lib/action_view/helpers/sanitize_helper.rb +223 -0
  92. data/lib/action_view/helpers/scriptaculous_helper.rb +63 -4
  93. data/lib/action_view/helpers/tag_helper.rb +69 -39
  94. data/lib/action_view/helpers/text_helper.rb +221 -148
  95. data/lib/action_view/helpers/url_helper.rb +283 -165
  96. data/lib/action_view/partials.rb +134 -62
  97. data/lib/action_view/template_error.rb +4 -12
  98. data/lib/actionpack.rb +1 -0
  99. data/test/abstract_unit.rb +21 -1
  100. data/test/action_view_test.rb +26 -0
  101. data/test/active_record_unit.rb +12 -20
  102. data/test/activerecord/active_record_store_test.rb +2 -2
  103. data/test/activerecord/render_partial_with_record_identification_test.rb +74 -0
  104. data/test/controller/action_pack_assertions_test.rb +21 -152
  105. data/test/controller/addresses_render_test.rb +2 -7
  106. data/test/controller/assert_select_test.rb +120 -14
  107. data/test/controller/base_test.rb +11 -13
  108. data/test/controller/caching_test.rb +125 -5
  109. data/test/controller/capture_test.rb +23 -16
  110. data/test/controller/cgi_test.rb +66 -391
  111. data/test/controller/components_test.rb +31 -42
  112. data/test/controller/content_type_test.rb +1 -1
  113. data/test/controller/cookie_test.rb +42 -14
  114. data/test/controller/deprecation/deprecated_base_methods_test.rb +1 -42
  115. data/test/controller/dispatcher_test.rb +123 -0
  116. data/test/controller/fake_models.rb +5 -0
  117. data/test/controller/filters_test.rb +44 -7
  118. data/test/controller/flash_test.rb +46 -2
  119. data/test/controller/fragment_store_setting_test.rb +10 -8
  120. data/test/controller/helper_test.rb +19 -2
  121. data/test/controller/html-scanner/document_test.rb +124 -0
  122. data/test/controller/html-scanner/node_test.rb +69 -0
  123. data/test/controller/html-scanner/sanitizer_test.rb +250 -0
  124. data/test/controller/html-scanner/tag_node_test.rb +239 -0
  125. data/test/controller/html-scanner/text_node_test.rb +51 -0
  126. data/test/controller/html-scanner/tokenizer_test.rb +125 -0
  127. data/test/controller/http_authentication_test.rb +54 -0
  128. data/test/controller/integration_test.rb +12 -26
  129. data/test/controller/layout_test.rb +64 -12
  130. data/test/controller/mime_responds_test.rb +193 -38
  131. data/test/controller/mime_type_test.rb +30 -8
  132. data/test/controller/new_render_test.rb +104 -22
  133. data/test/controller/polymorphic_routes_test.rb +98 -0
  134. data/test/controller/record_identifier_test.rb +103 -0
  135. data/test/controller/redirect_test.rb +120 -18
  136. data/test/controller/render_test.rb +195 -45
  137. data/test/controller/request_forgery_protection_test.rb +217 -0
  138. data/test/controller/request_test.rb +545 -27
  139. data/test/controller/rescue_test.rb +501 -0
  140. data/test/controller/resources_test.rb +258 -132
  141. data/test/controller/routing_test.rb +502 -106
  142. data/test/controller/selector_test.rb +5 -5
  143. data/test/controller/send_file_test.rb +17 -7
  144. data/test/controller/session/cookie_store_test.rb +246 -0
  145. data/test/controller/session/mem_cache_store_test.rb +182 -0
  146. data/test/controller/session_fixation_test.rb +8 -11
  147. data/test/controller/session_management_test.rb +7 -7
  148. data/test/controller/test_test.rb +150 -38
  149. data/test/controller/url_rewriter_test.rb +87 -12
  150. data/test/controller/verification_test.rb +11 -0
  151. data/test/controller/view_paths_test.rb +137 -0
  152. data/test/controller/webservice_test.rb +11 -75
  153. data/test/fixtures/addresses/{list.rhtml → list.erb} +0 -0
  154. data/test/fixtures/db_definitions/sqlite.sql +2 -1
  155. data/test/fixtures/developer.rb +2 -0
  156. data/test/fixtures/fun/games/{hello_world.rhtml → hello_world.erb} +0 -0
  157. data/test/fixtures/helpers/fun/pdf_helper.rb +1 -1
  158. data/test/fixtures/layout_tests/alt/hello.rhtml +1 -0
  159. data/test/fixtures/layout_tests/layouts/multiple_extensions.html.erb +1 -0
  160. data/test/fixtures/layouts/{builder.rxml → builder.builder} +0 -0
  161. data/test/fixtures/layouts/{standard.rhtml → standard.erb} +0 -0
  162. data/test/fixtures/layouts/{talk_from_action.rhtml → talk_from_action.erb} +0 -0
  163. data/test/fixtures/layouts/{yield.rhtml → yield.erb} +0 -0
  164. data/test/fixtures/multipart/binary_file +0 -0
  165. data/test/fixtures/multipart/bracketed_param +5 -0
  166. data/test/fixtures/override/test/hello_world.erb +1 -0
  167. data/test/fixtures/override2/layouts/test/sub.erb +1 -0
  168. data/test/fixtures/post_test/layouts/post.html.erb +1 -0
  169. data/test/fixtures/post_test/layouts/super_post.iphone.erb +1 -0
  170. data/test/fixtures/post_test/post/index.html.erb +1 -0
  171. data/test/fixtures/post_test/post/index.iphone.erb +1 -0
  172. data/test/fixtures/post_test/super_post/index.html.erb +1 -0
  173. data/test/fixtures/post_test/super_post/index.iphone.erb +1 -0
  174. data/test/fixtures/public/404.html +1 -0
  175. data/test/fixtures/public/500.html +1 -0
  176. data/test/fixtures/public/javascripts/application.js +0 -1
  177. data/test/fixtures/public/javascripts/bank.js +1 -0
  178. data/test/fixtures/public/javascripts/robber.js +1 -0
  179. data/test/fixtures/public/stylesheets/bank.css +1 -0
  180. data/test/fixtures/public/stylesheets/robber.css +1 -0
  181. data/test/fixtures/replies.yml +2 -0
  182. data/test/fixtures/reply.rb +2 -1
  183. data/test/fixtures/respond_to/{all_types_with_layout.rhtml → all_types_with_layout.html.erb} +0 -0
  184. data/test/fixtures/respond_to/{all_types_with_layout.rjs → all_types_with_layout.js.rjs} +0 -0
  185. data/test/fixtures/respond_to/custom_constant_handling_without_block.mobile.erb +1 -0
  186. data/test/fixtures/respond_to/iphone_with_html_response_type.html.erb +1 -0
  187. data/test/fixtures/respond_to/iphone_with_html_response_type.iphone.erb +1 -0
  188. data/test/fixtures/respond_to/layouts/missing.html.erb +1 -0
  189. data/test/fixtures/respond_to/layouts/standard.html.erb +1 -0
  190. data/test/fixtures/respond_to/layouts/standard.iphone.erb +1 -0
  191. data/test/fixtures/respond_to/{using_defaults.rhtml → using_defaults.html.erb} +0 -0
  192. data/test/fixtures/respond_to/{using_defaults.rjs → using_defaults.js.rjs} +0 -0
  193. data/test/fixtures/respond_to/{using_defaults.rxml → using_defaults.xml.builder} +0 -0
  194. data/test/fixtures/respond_to/{using_defaults_with_type_list.rhtml → using_defaults_with_type_list.html.erb} +0 -0
  195. data/test/fixtures/respond_to/{using_defaults_with_type_list.rjs → using_defaults_with_type_list.js.rjs} +0 -0
  196. data/test/fixtures/respond_to/{using_defaults_with_type_list.rxml → using_defaults_with_type_list.xml.builder} +0 -0
  197. data/test/fixtures/scope/test/{modgreet.rhtml → modgreet.erb} +0 -0
  198. data/test/fixtures/test/{_customer.rhtml → _customer.erb} +0 -0
  199. data/test/fixtures/test/{_customer_greeting.rhtml → _customer_greeting.erb} +0 -0
  200. data/test/fixtures/test/_hash_greeting.erb +1 -0
  201. data/test/fixtures/test/_hash_object.erb +2 -0
  202. data/test/fixtures/test/{_hello.rxml → _hello.builder} +0 -0
  203. data/test/fixtures/test/_layout_for_partial.html.erb +3 -0
  204. data/test/fixtures/test/_partial.erb +1 -0
  205. data/test/fixtures/test/_partial.html.erb +1 -0
  206. data/test/fixtures/test/_partial.js.erb +1 -0
  207. data/test/fixtures/test/_partial_for_use_in_layout.html.erb +1 -0
  208. data/test/fixtures/test/{_partial_only.rhtml → _partial_only.erb} +0 -0
  209. data/test/fixtures/test/{_person.rhtml → _person.erb} +0 -0
  210. data/test/fixtures/test/{action_talk_to_layout.rhtml → action_talk_to_layout.erb} +0 -0
  211. data/test/fixtures/test/{block_content_for.rhtml → block_content_for.erb} +0 -0
  212. data/test/fixtures/test/calling_partial_with_layout.html.erb +1 -0
  213. data/test/fixtures/test/{capturing.rhtml → capturing.erb} +0 -0
  214. data/test/fixtures/test/{content_for.rhtml → content_for.erb} +0 -0
  215. data/test/fixtures/test/content_for_concatenated.erb +3 -0
  216. data/test/fixtures/test/content_for_with_parameter.erb +2 -0
  217. data/test/fixtures/test/dot.directory/{render_file_with_ivar.rhtml → render_file_with_ivar.erb} +0 -0
  218. data/test/fixtures/test/{erb_content_for.rhtml → erb_content_for.erb} +0 -0
  219. data/test/fixtures/test/formatted_html_erb.html.erb +1 -0
  220. data/test/fixtures/test/formatted_xml_erb.builder +1 -0
  221. data/test/fixtures/test/formatted_xml_erb.html.erb +1 -0
  222. data/test/fixtures/test/formatted_xml_erb.xml.erb +1 -0
  223. data/test/fixtures/test/{greeting.rhtml → greeting.erb} +0 -0
  224. data/test/fixtures/test/{hello.rxml → hello.builder} +0 -0
  225. data/test/fixtures/test/{hello_world.rxml → hello_world.builder} +0 -0
  226. data/test/fixtures/test/{hello_world.rhtml → hello_world.erb} +0 -0
  227. data/test/fixtures/test/{hello_world_container.rxml → hello_world_container.builder} +0 -0
  228. data/test/fixtures/test/{hello_world_with_layout_false.rhtml → hello_world_with_layout_false.erb} +0 -0
  229. data/test/fixtures/test/{hello_xml_world.rxml → hello_xml_world.builder} +0 -0
  230. data/test/fixtures/test/list.erb +1 -0
  231. data/test/fixtures/test/{non_erb_block_content_for.rxml → non_erb_block_content_for.builder} +0 -0
  232. data/test/fixtures/test/{potential_conflicts.rhtml → potential_conflicts.erb} +0 -0
  233. data/test/fixtures/test/{render_file_with_ivar.rhtml → render_file_with_ivar.erb} +0 -0
  234. data/test/fixtures/test/{render_file_with_locals.rhtml → render_file_with_locals.erb} +0 -0
  235. data/test/fixtures/test/{render_to_string_test.rhtml → render_to_string_test.erb} +0 -0
  236. data/test/fixtures/test/{update_element_with_capture.rhtml → update_element_with_capture.erb} +0 -0
  237. data/test/fixtures/test/using_layout_around_block.html.erb +1 -0
  238. data/test/fixtures/topic.rb +1 -1
  239. data/test/template/active_record_helper_test.rb +67 -20
  240. data/test/template/asset_tag_helper_test.rb +222 -54
  241. data/test/template/atom_feed_helper_test.rb +101 -0
  242. data/test/template/benchmark_helper_test.rb +2 -2
  243. data/test/template/compiled_templates_test.rb +76 -32
  244. data/test/template/date_helper_test.rb +125 -9
  245. data/test/template/form_helper_test.rb +326 -33
  246. data/test/template/form_options_helper_test.rb +822 -15
  247. data/test/template/form_tag_helper_test.rb +96 -30
  248. data/test/template/javascript_helper_test.rb +61 -13
  249. data/test/template/number_helper_test.rb +12 -11
  250. data/test/template/prototype_helper_test.rb +185 -24
  251. data/test/template/sanitize_helper_test.rb +49 -0
  252. data/test/template/scriptaculous_helper_test.rb +9 -3
  253. data/test/template/tag_helper_test.rb +13 -2
  254. data/test/template/text_helper_test.rb +38 -52
  255. data/test/template/url_helper_test.rb +216 -46
  256. metadata +144 -116
  257. data/examples/.htaccess +0 -24
  258. data/examples/address_book/index.rhtml +0 -33
  259. data/examples/address_book/layout.rhtml +0 -8
  260. data/examples/address_book_controller.cgi +0 -9
  261. data/examples/address_book_controller.fcgi +0 -6
  262. data/examples/address_book_controller.rb +0 -52
  263. data/examples/address_book_controller.rbx +0 -4
  264. data/examples/benchmark.rb +0 -52
  265. data/examples/benchmark_with_ar.fcgi +0 -89
  266. data/examples/blog_controller.cgi +0 -53
  267. data/examples/debate/index.rhtml +0 -14
  268. data/examples/debate/new_topic.rhtml +0 -22
  269. data/examples/debate/topic.rhtml +0 -32
  270. data/examples/debate_controller.cgi +0 -57
  271. data/lib/action_controller/assertions/deprecated_assertions.rb +0 -228
  272. data/lib/action_controller/cgi_ext/cgi_ext.rb +0 -36
  273. data/lib/action_controller/cgi_ext/cgi_methods.rb +0 -211
  274. data/lib/action_controller/cgi_ext/pstore_performance_fix.rb +0 -30
  275. data/lib/action_controller/cgi_ext/raw_post_data_fix.rb +0 -95
  276. data/lib/action_controller/cgi_ext/session_performance_fix.rb +0 -30
  277. data/lib/action_controller/deprecated_dependencies.rb +0 -65
  278. data/lib/action_controller/deprecated_redirects.rb +0 -17
  279. data/lib/action_controller/deprecated_request_methods.rb +0 -34
  280. data/lib/action_controller/macros/auto_complete.rb +0 -53
  281. data/lib/action_controller/macros/in_place_editing.rb +0 -33
  282. data/lib/action_controller/pagination.rb +0 -408
  283. data/lib/action_controller/scaffolding.rb +0 -189
  284. data/lib/action_controller/templates/rescues/_request_and_response.rhtml +0 -44
  285. data/lib/action_controller/templates/scaffolds/edit.rhtml +0 -7
  286. data/lib/action_controller/templates/scaffolds/layout.rhtml +0 -69
  287. data/lib/action_controller/templates/scaffolds/list.rhtml +0 -27
  288. data/lib/action_controller/templates/scaffolds/new.rhtml +0 -6
  289. data/lib/action_controller/templates/scaffolds/show.rhtml +0 -9
  290. data/lib/action_controller/vendor/xml_node.rb +0 -97
  291. data/lib/action_view/helpers/deprecated_helper.rb +0 -37
  292. data/lib/action_view/helpers/java_script_macros_helper.rb +0 -233
  293. data/lib/action_view/helpers/pagination_helper.rb +0 -86
  294. data/test/activerecord/active_record_assertions_test.rb +0 -92
  295. data/test/activerecord/pagination_test.rb +0 -165
  296. data/test/controller/deprecated_instance_variables_test.rb +0 -48
  297. data/test/controller/raw_post_test.rb +0 -68
  298. data/test/fixtures/deprecated_instance_variables/_cookies_ivar.rhtml +0 -1
  299. data/test/fixtures/deprecated_instance_variables/_cookies_method.rhtml +0 -1
  300. data/test/fixtures/deprecated_instance_variables/_flash_ivar.rhtml +0 -1
  301. data/test/fixtures/deprecated_instance_variables/_flash_method.rhtml +0 -1
  302. data/test/fixtures/deprecated_instance_variables/_headers_ivar.rhtml +0 -1
  303. data/test/fixtures/deprecated_instance_variables/_headers_method.rhtml +0 -1
  304. data/test/fixtures/deprecated_instance_variables/_params_ivar.rhtml +0 -1
  305. data/test/fixtures/deprecated_instance_variables/_params_method.rhtml +0 -1
  306. data/test/fixtures/deprecated_instance_variables/_request_ivar.rhtml +0 -1
  307. data/test/fixtures/deprecated_instance_variables/_request_method.rhtml +0 -1
  308. data/test/fixtures/deprecated_instance_variables/_response_ivar.rhtml +0 -1
  309. data/test/fixtures/deprecated_instance_variables/_response_method.rhtml +0 -1
  310. data/test/fixtures/deprecated_instance_variables/_session_ivar.rhtml +0 -1
  311. data/test/fixtures/deprecated_instance_variables/_session_method.rhtml +0 -1
  312. data/test/fixtures/respond_to/layouts/standard.rhtml +0 -1
  313. data/test/fixtures/test/_hash_object.rhtml +0 -1
  314. data/test/fixtures/test/list.rhtml +0 -1
  315. data/test/template/deprecated_helper_test.rb +0 -36
  316. data/test/template/deprecated_instance_variables_test.rb +0 -43
  317. data/test/template/java_script_macros_helper_test.rb +0 -109
@@ -0,0 +1,111 @@
1
+ # Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERb or any other
2
+ # template languages).
3
+ module ActionView
4
+ module Helpers #:nodoc:
5
+ module AtomFeedHelper
6
+ # Full usage example:
7
+ #
8
+ # config/routes.rb:
9
+ # ActionController::Routing::Routes.draw do |map|
10
+ # map.resources :posts
11
+ # map.root :controller => "posts"
12
+ # end
13
+ #
14
+ # app/controllers/posts_controller.rb:
15
+ # class PostsController < ApplicationController::Base
16
+ # # GET /posts.html
17
+ # # GET /posts.atom
18
+ # def index
19
+ # @posts = Post.find(:all)
20
+ #
21
+ # respond_to do |format|
22
+ # format.html
23
+ # format.atom
24
+ # end
25
+ # end
26
+ # end
27
+ #
28
+ # app/views/posts/index.atom.builder:
29
+ # atom_feed do |feed|
30
+ # feed.title("My great blog!")
31
+ # feed.updated((@posts.first.created_at))
32
+ #
33
+ # for post in @posts
34
+ # feed.entry(post) do |entry|
35
+ # entry.title(post.title)
36
+ # entry.content(post.body, :type => 'html')
37
+ #
38
+ # entry.author do |author|
39
+ # author.name("DHH")
40
+ # end
41
+ # end
42
+ # end
43
+ # end
44
+ #
45
+ # The options are for atom_feed are:
46
+ #
47
+ # * <tt>:language</tt>: Defaults to "en-US".
48
+ # * <tt>:root_url</tt>: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
49
+ # * <tt>:url</tt>: The URL for this feed. Defaults to the current URL.
50
+ #
51
+ # atom_feed yields a AtomFeedBuilder instance.
52
+ def atom_feed(options = {}, &block)
53
+ xml = options[:xml] || eval("xml", block.binding)
54
+ xml.instruct!
55
+
56
+ xml.feed "xml:lang" => options[:language] || "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do
57
+ xml.id("tag:#{request.host}:#{request.request_uri.split(".")[0].gsub("/", "")}")
58
+ xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port))
59
+
60
+ if options[:url]
61
+ xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
62
+ end
63
+
64
+ yield AtomFeedBuilder.new(xml, self)
65
+ end
66
+ end
67
+
68
+
69
+ class AtomFeedBuilder
70
+ def initialize(xml, view)
71
+ @xml, @view = xml, view
72
+ end
73
+
74
+ # Accepts a Date or Time object and inserts it in the proper format. If nil is passed, current time in UTC is used.
75
+ def updated(date_or_time = nil)
76
+ @xml.updated((date_or_time || Time.now.utc).xmlschema)
77
+ end
78
+
79
+ # Creates an entry tag for a specific record and prefills the id using class and id.
80
+ #
81
+ # Options:
82
+ #
83
+ # * <tt>:updated</tt>: Time of update. Defaults to the created_at attribute on the record if one such exists.
84
+ # * <tt>:published</tt>: Time first published. Defaults to the updated_at attribute on the record if one such exists.
85
+ # * <tt>:url</tt>: The URL for this entry. Defaults to the polymorphic_url for the record.
86
+ def entry(record, options = {})
87
+ @xml.entry do
88
+ @xml.id("tag:#{@view.request.host_with_port}:#{record.class}#{record.id}")
89
+
90
+ if options[:published] || (record.respond_to?(:created_at) && record.created_at)
91
+ @xml.published((options[:published] || record.created_at).xmlschema)
92
+ end
93
+
94
+ if options[:updated] || (record.respond_to?(:updated_at) && record.updated_at)
95
+ @xml.updated((options[:updated] || record.updated_at).xmlschema)
96
+ end
97
+
98
+ @xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:url] || @view.polymorphic_url(record))
99
+
100
+ yield @xml
101
+ end
102
+ end
103
+
104
+ private
105
+ def method_missing(method, *arguments)
106
+ @xml.__send__(method, *arguments)
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -2,17 +2,24 @@ require 'benchmark'
2
2
 
3
3
  module ActionView
4
4
  module Helpers
5
+ # This helper offers a method to measure the execution time of a block
6
+ # in a template.
5
7
  module BenchmarkHelper
6
- # Measures the execution time of a block in a template and reports the result to the log. Example:
8
+ # Allows you to measure the execution time of a block
9
+ # in a template and records the result to the log. Wrap this block around
10
+ # expensive operations or possible bottlenecks to get a time reading
11
+ # for the operation. For example, let's say you thought your file
12
+ # processing method was taking too long; you could wrap it in a benchmark block.
7
13
  #
8
- # <% benchmark "Notes section" do %>
9
- # <%= expensive_notes_operation %>
14
+ # <% benchmark "Process data files" do %>
15
+ # <%= expensive_files_operation %>
10
16
  # <% end %>
11
17
  #
12
- # Will add something like "Notes section (0.34523)" to the log.
18
+ # That would add something like "Process data files (0.34523)" to the log,
19
+ # which you can then use to compare timings when optimizing your code.
13
20
  #
14
21
  # You may give an optional logger level as the second argument
15
- # (:debug, :info, :warn, :error). The default is :info.
22
+ # (:debug, :info, :warn, :error); the default value is :info.
16
23
  def benchmark(message = "Benchmarking", level = :info)
17
24
  if @logger
18
25
  real = Benchmark.realtime { yield }
@@ -1,7 +1,36 @@
1
1
  module ActionView
2
2
  module Helpers
3
+ # This helper to exposes a method for caching of view fragments.
3
4
  # See ActionController::Caching::Fragments for usage instructions.
4
5
  module CacheHelper
6
+ # A method for caching fragments of a view rather than an entire
7
+ # action or page. This technique is useful caching pieces like
8
+ # menus, lists of news topics, static HTML fragments, and so on.
9
+ # This method takes a block that contains the content you wish
10
+ # to cache. See ActionController::Caching::Fragments for more
11
+ # information.
12
+ #
13
+ # ==== Examples
14
+ # If you wanted to cache a navigation menu, you could do the
15
+ # following.
16
+ #
17
+ # <% cache do %>
18
+ # <%= render :partial => "menu" %>
19
+ # <% end %>
20
+ #
21
+ # You can also cache static content...
22
+ #
23
+ # <% cache do %>
24
+ # <p>Hello users! Welcome to our website!</p>
25
+ # <% end %>
26
+ #
27
+ # ...and static content mixed with RHTML content.
28
+ #
29
+ # <% cache do %>
30
+ # Topics:
31
+ # <%= render :partial => "topics", :collection => @topic_list %>
32
+ # <i>Topics listed alphabetically</i>
33
+ # <% end %>
5
34
  def cache(name = {}, &block)
6
35
  @controller.cache_erb_fragment(block, name)
7
36
  end
@@ -1,96 +1,130 @@
1
1
  module ActionView
2
2
  module Helpers
3
- # Capture lets you extract parts of code which
4
- # can be used in other points of the template or even layout file.
5
- #
6
- # == Capturing a block into an instance variable
7
- #
8
- # <% @script = capture do %>
9
- # [some html...]
10
- # <% end %>
11
- #
12
- # == Add javascript to header using content_for
13
- #
14
- # content_for("name") is a wrapper for capture which will
15
- # make the fragment available by name to a yielding layout or template.
16
- #
17
- # layout.rhtml:
18
- #
19
- # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
20
- # <head>
21
- # <title>layout with js</title>
22
- # <script type="text/javascript">
23
- # <%= yield :script %>
24
- # </script>
25
- # </head>
26
- # <body>
27
- # <%= yield %>
28
- # </body>
29
- # </html>
30
- #
31
- # view.rhtml
32
- #
33
- # This page shows an alert box!
34
- #
35
- # <% content_for("script") do %>
36
- # alert('hello world')
37
- # <% end %>
38
- #
39
- # Normal view text
3
+ # CaptureHelper exposes methods to let you extract generated markup which
4
+ # can be used in other parts of a template or layout file.
5
+ # It provides a method to capture blocks into variables through capture and
6
+ # a way to capture a block of markup for use in a layout through content_for.
40
7
  module CaptureHelper
41
- # Capture allows you to extract a part of the template into an
42
- # instance variable. You can use this instance variable anywhere
43
- # in your templates and even in your layout.
8
+ # The capture method allows you to extract part of a template into a
9
+ # variable. You can then use this variable anywhere in your templates or layout.
44
10
  #
45
- # Example of capture being used in a .rhtml page:
11
+ # ==== Examples
12
+ # The capture method can be used in ERb templates...
46
13
  #
47
14
  # <% @greeting = capture do %>
48
- # Welcome To my shiny new web page!
15
+ # Welcome to my shiny new web page! The date and time is
16
+ # <%= Time.now %>
49
17
  # <% end %>
50
18
  #
51
- # Example of capture being used in a .rxml page:
19
+ # ...and Builder (RXML) templates.
52
20
  #
53
- # @greeting = capture do
54
- # 'Welcome To my shiny new web page!'
21
+ # @timestamp = capture do
22
+ # "The current timestamp is #{Time.now}."
55
23
  # end
24
+ #
25
+ # You can then use that variable anywhere else. For example:
26
+ #
27
+ # <html>
28
+ # <head><title><%= @greeting %></title></head>
29
+ # <body>
30
+ # <b><%= @greeting %></b>
31
+ # </body></html>
32
+ #
56
33
  def capture(*args, &block)
57
34
  # execute the block
58
35
  begin
59
- buffer = eval("_erbout", block.binding)
36
+ buffer = eval(ActionView::Base.erb_variable, block.binding)
60
37
  rescue
61
38
  buffer = nil
62
39
  end
63
40
 
64
41
  if buffer.nil?
65
- capture_block(*args, &block)
42
+ capture_block(*args, &block).to_s
66
43
  else
67
- capture_erb_with_buffer(buffer, *args, &block)
44
+ capture_erb_with_buffer(buffer, *args, &block).to_s
68
45
  end
69
46
  end
70
47
 
71
- # Calling content_for stores the block of markup for later use.
72
- # Subsequently, you can make calls to it by name with <tt>yield</tt>
73
- # in another template or in the layout.
48
+ # Calling content_for stores a block of markup in an identifier for later use.
49
+ # You can make subsequent calls to the stored content in other templates or the layout
50
+ # by passing the identifier as an argument to <tt>yield</tt>.
74
51
  #
75
- # Example:
52
+ # ==== Examples
76
53
  #
77
- # <% content_for("header") do %>
78
- # alert('hello world')
54
+ # <% content_for :not_authorized do %>
55
+ # alert('You are not authorized to do that!')
79
56
  # <% end %>
80
57
  #
81
- # You can use yield :header anywhere in your templates.
58
+ # You can then use <tt>yield :not_authorized</tt> anywhere in your templates.
59
+ #
60
+ # <%= yield :not_authorized if current_user.nil? %>
61
+ #
62
+ # You can also use this syntax alongside an existing call to <tt>yield</tt> in a layout. For example:
63
+ #
64
+ # <%# This is the layout %>
65
+ # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
66
+ # <head>
67
+ # <title>My Website</title>
68
+ # <%= yield :script %>
69
+ # </head>
70
+ # <body>
71
+ # <%= yield %>
72
+ # </body>
73
+ # </html>
74
+ #
75
+ # And now, we'll create a view that has a content_for call that
76
+ # creates the <tt>script</tt> identifier.
77
+ #
78
+ # <%# This is our view %>
79
+ # Please login!
80
+ #
81
+ # <% content_for :script do %>
82
+ # <script type="text/javascript">alert('You are not authorized to view this page!')</script>
83
+ # <% end %>
84
+ #
85
+ # Then, in another view, you could to do something like this:
86
+ #
87
+ # <%= link_to_remote 'Logout', :action => 'logout' %>
88
+ #
89
+ # <% content_for :script do %>
90
+ # <%= javascript_include_tag :defaults %>
91
+ # <% end %>
92
+ #
93
+ # That will place <script> tags for Prototype, Scriptaculous, and application.js (if it exists)
94
+ # on the page; this technique is useful if you'll only be using these scripts in a few views.
95
+ #
96
+ # Note that content_for concatenates the blocks it is given for a particular
97
+ # identifier in order. For example:
98
+ #
99
+ # <% content_for :navigation do %>
100
+ # <li><%= link_to 'Home', :action => 'index' %></li>
101
+ # <% end %>
102
+ #
103
+ # <%# Add some other content, or use a different template: %>
104
+ #
105
+ # <% content_for :navigation do %>
106
+ # <li><%= link_to 'Login', :action => 'login' %></li>
107
+ # <% end %>
108
+ #
109
+ # Then, in another template or layout, this code would render both links in order:
110
+ #
111
+ # <ul><%= yield :navigation %></ul>
112
+ #
113
+ # Lastly, simple content can be passed as a parameter:
82
114
  #
83
- # <%= yield :header %>
115
+ # <% content_for :script, javascript_include_tag(:defaults) %>
84
116
  #
85
- # NOTE: Beware that content_for is ignored in caches. So you shouldn't use it
86
- # for elements that are going to be fragment cached.
117
+ # WARNING: content_for is ignored in caches. So you shouldn't use it
118
+ # for elements that will be fragment cached.
87
119
  #
88
- # The deprecated way of accessing a content_for block was to use a instance variable
89
- # named @@content_for_#{name_of_the_content_block}@. So <tt><%= content_for('footer') %></tt>
90
- # would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred notation now is
120
+ # The deprecated way of accessing a content_for block is to use an instance variable
121
+ # named <tt>@content_for_#{name_of_the_content_block}</tt>. So <tt><%= content_for :footer %></tt>
122
+ # would be available as <tt><%= @content_for_footer %></tt>. The preferred usage is now
91
123
  # <tt><%= yield :footer %></tt>.
92
124
  def content_for(name, content = nil, &block)
93
- eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"
125
+ existing_content_for = instance_variable_get("@content_for_#{name}").to_s
126
+ new_content_for = existing_content_for + (block_given? ? capture(&block) : content)
127
+ instance_variable_set("@content_for_#{name}", new_content_for)
94
128
  end
95
129
 
96
130
  private
@@ -99,7 +133,7 @@ module ActionView
99
133
  end
100
134
 
101
135
  def capture_erb(*args, &block)
102
- buffer = eval("_erbout", block.binding)
136
+ buffer = eval(ActionView::Base.erb_variable, block.binding)
103
137
  capture_erb_with_buffer(buffer, *args, &block)
104
138
  end
105
139
 
@@ -25,9 +25,9 @@ module ActionView
25
25
  # 23 hrs, 59 mins, 29 secs <-> 47 hrs, 59 mins, 29 secs # => 1 day
26
26
  # 47 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days
27
27
  # 29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month
28
- # 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 31 secs # => [2..12] months
29
- # 1 yr minus 30 secs <-> 2 yrs minus 31 secs # => about 1 year
30
- # 2 yrs minus 30 secs <-> max time or date # => over [2..X] years
28
+ # 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
29
+ # 1 yr <-> 2 yrs minus 1 secs # => about 1 year
30
+ # 2 yrs <-> max time or date # => over [2..X] years
31
31
  #
32
32
  # With include_seconds = true and the difference < 1 minute 29 seconds
33
33
  # 0-4 secs # => less than 5 seconds
@@ -37,14 +37,25 @@ module ActionView
37
37
  # 40-59 secs # => less than a minute
38
38
  # 60-89 secs # => 1 minute
39
39
  #
40
- # Examples:
41
- #
40
+ # ==== Examples
42
41
  # from_time = Time.now
43
- # distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
44
- # distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute
45
- # distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds
42
+ # distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
43
+ # distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
44
+ # distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute
45
+ # distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds
46
+ # distance_of_time_in_words(from_time, 3.years.from_now) # => over 3 years
47
+ # distance_of_time_in_words(from_time, from_time + 60.hours) # => about 3 days
48
+ # distance_of_time_in_words(from_time, from_time + 45.seconds, true) # => less than a minute
49
+ # distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute
50
+ # distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute
51
+ # distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
52
+ # distance_of_time_in_words(from_time, from_time + 4.years + 15.days + 30.minutes + 5.seconds) # => over 4 years
53
+ #
54
+ # to_time = Time.now + 6.years + 19.days
55
+ # distance_of_time_in_words(from_time, to_time, true) # => over 6 years
56
+ # distance_of_time_in_words(to_time, from_time, true) # => over 6 years
57
+ # distance_of_time_in_words(Time.now, Time.now) # => less than a minute
46
58
  #
47
- # Note: Rails calculates one year as 365.25 days.
48
59
  def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
49
60
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
50
61
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
@@ -69,13 +80,20 @@ module ActionView
69
80
  when 1440..2879 then '1 day'
70
81
  when 2880..43199 then "#{(distance_in_minutes / 1440).round} days"
71
82
  when 43200..86399 then 'about 1 month'
72
- when 86400..525959 then "#{(distance_in_minutes / 43200).round} months"
73
- when 525960..1051919 then 'about 1 year'
74
- else "over #{(distance_in_minutes / 525960).round} years"
83
+ when 86400..525599 then "#{(distance_in_minutes / 43200).round} months"
84
+ when 525600..1051199 then 'about 1 year'
85
+ else "over #{(distance_in_minutes / 525600).round} years"
75
86
  end
76
87
  end
77
88
 
78
89
  # Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
90
+ #
91
+ # ==== Examples
92
+ # time_ago_in_words(3.minutes.from_now) # => 3 minutes
93
+ # time_ago_in_words(Time.now - 15.hours) # => 15 hours
94
+ # time_ago_in_words(Time.now) # => less than a minute
95
+ #
96
+ # from_time = Time.now - 3.days - 14.minutes - 25.seconds # => 3 days
79
97
  def time_ago_in_words(from_time, include_seconds = false)
80
98
  distance_of_time_in_words(from_time, Time.now, include_seconds)
81
99
  end
@@ -90,20 +108,46 @@ module ActionView
90
108
  # set the order of the tags using the <tt>:order</tt> option with an array of symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in
91
109
  # the desired order. Symbols may be omitted and the respective select is not included.
92
110
  #
111
+ # Pass the <tt>:default</tt> option to set the default date. Use a Time object or a Hash of :year, :month, :day, :hour, :minute, and :second.
112
+ #
93
113
  # Passing :disabled => true as part of the +options+ will make elements inaccessible for change.
94
114
  #
95
115
  # NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed.
96
116
  #
97
- # Examples:
98
- #
117
+ # ==== Examples
118
+ # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute
99
119
  # date_select("post", "written_on")
120
+ #
121
+ # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute,
122
+ # # with the year in the year drop down box starting at 1995.
100
123
  # date_select("post", "written_on", :start_year => 1995)
124
+ #
125
+ # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute,
126
+ # # with the year in the year drop down box starting at 1995, numbers used for months instead of words,
127
+ # # and without a day select box.
101
128
  # date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
102
129
  # :discard_day => true, :include_blank => true)
130
+ #
131
+ # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute
132
+ # # with the fields ordered as day, month, year rather than month, day, year.
103
133
  # date_select("post", "written_on", :order => [:day, :month, :year])
104
- # date_select("user", "birthday", :order => [:month, :day])
134
+ #
135
+ # # Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
136
+ # # lacking a year field.
137
+ # date_select("user", "birthday", :order => [:month, :day])
138
+ #
139
+ # # Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
140
+ # # which is initially set to the date 3 days from the current date
141
+ # date_select("post", "written_on", :default => 3.days.from_now)
142
+ #
143
+ # # Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute
144
+ # # that will have a default day of 20.
145
+ # date_select("credit_card", "bill_due", :default => { :day => 20 })
105
146
  #
106
147
  # The selects are prepared for multi-parameter assignment to an Active Record object.
148
+ #
149
+ # 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 all month
150
+ # choices are valid.
107
151
  def date_select(object_name, method, options = {})
108
152
  InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_date_select_tag(options)
109
153
  end
@@ -111,12 +155,32 @@ module ActionView
111
155
  # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified
112
156
  # time-based attribute (identified by +method+) on an object assigned to the template (identified by +object+).
113
157
  # You can include the seconds with <tt>:include_seconds</tt>.
114
- # Examples:
115
- #
158
+ #
159
+ # ==== Examples
160
+ # # Creates a time select tag that, when POSTed, will be stored in the post variable in the sunrise attribute
116
161
  # time_select("post", "sunrise")
162
+ #
163
+ # # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
164
+ # time_select("order", "submitted")
165
+ #
166
+ # # Creates a time select tag that, when POSTed, will be stored in the mail variable in the sent_at attribute
167
+ # time_select("mail", "sent_at")
168
+ #
169
+ # # Creates a time select tag with a seconds field that, when POSTed, will be stored in the post variables in
170
+ # # the sunrise attribute.
117
171
  # time_select("post", "start_time", :include_seconds => true)
118
172
  #
173
+ # # Creates a time select tag with a seconds field that, when POSTed, will be stored in the entry variables in
174
+ # # the submission_time attribute.
175
+ # time_select("entry", "submission_time", :include_seconds => true)
176
+ #
177
+ # # You can set the :minute_step to 15 which will give you: 00, 15, 30 and 45.
178
+ # time_select 'game', 'game_time', {:minute_step => 15}
179
+ #
119
180
  # The selects are prepared for multi-parameter assignment to an Active Record object.
181
+ #
182
+ # 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 all month
183
+ # choices are valid.
120
184
  def time_select(object_name, method, options = {})
121
185
  InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_time_select_tag(options)
122
186
  end
@@ -124,9 +188,22 @@ module ActionView
124
188
  # Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based
125
189
  # attribute (identified by +method+) on an object assigned to the template (identified by +object+). Examples:
126
190
  #
191
+ # ==== Examples
192
+ # # Generates a datetime select that, when POSTed, will be stored in the post variable in the written_on attribute
127
193
  # datetime_select("post", "written_on")
194
+ #
195
+ # # Generates a datetime select with a year select that starts at 1995 that, when POSTed, will be stored in the
196
+ # # post variable in the written_on attribute.
128
197
  # datetime_select("post", "written_on", :start_year => 1995)
129
198
  #
199
+ # # Generates a datetime select with a default value of 3 days from the current time that, when POSTed, will be stored in the
200
+ # # trip variable in the departing attribute.
201
+ # datetime_select("trip", "departing", :default => 3.days.from_now)
202
+ #
203
+ # # Generates a datetime select that discards the type that, when POSTed, will be stored in the post variable as the written_on
204
+ # # attribute.
205
+ # datetime_select("post", "written_on", :discard_type => true)
206
+ #
130
207
  # The selects are prepared for multi-parameter assignment to an Active Record object.
131
208
  def datetime_select(object_name, method, options = {})
132
209
  InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_datetime_select_tag(options)
@@ -135,9 +212,35 @@ module ActionView
135
212
  # Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the +datetime+.
136
213
  # It's also possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
137
214
  # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol, it
138
- # will be appened onto the <tt>:order</tt> passed in. You can also add <tt>:date_separator</tt> and <tt>:time_separator</tt>
215
+ # will be appended onto the <tt>:order</tt> passed in. You can also add <tt>:date_separator</tt> and <tt>:time_separator</tt>
139
216
  # keys to the +options+ to control visual display of the elements.
140
- def select_datetime(datetime = Time.now, options = {})
217
+ #
218
+ # ==== Examples
219
+ # my_date_time = Time.now + 4.days
220
+ #
221
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
222
+ # select_datetime(my_date_time)
223
+ #
224
+ # # Generates a datetime select that defaults to today (no specified datetime)
225
+ # select_datetime()
226
+ #
227
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
228
+ # # with the fields ordered year, month, day rather than month, day, year.
229
+ # select_datetime(my_date_time, :order => [:year, :month, :day])
230
+ #
231
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
232
+ # # with a '/' between each date field.
233
+ # select_datetime(my_date_time, :date_separator => '/')
234
+ #
235
+ # # Generates a datetime select that discards the type of the field and defaults to the datetime in
236
+ # # my_date_time (four days after today)
237
+ # select_datetime(my_date_time, :discard_type => true)
238
+ #
239
+ # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today)
240
+ # # prefixed with 'payday' rather than 'date'
241
+ # select_datetime(my_date_time, :prefix => 'payday')
242
+ #
243
+ def select_datetime(datetime = Time.now, options = {})
141
244
  separator = options[:datetime_separator] || ''
142
245
  select_date(datetime, options) + separator + select_time(datetime, options)
143
246
  end
@@ -145,7 +248,29 @@ module ActionView
145
248
  # Returns a set of html select-tags (one for year, month, and day) pre-selected with the +date+.
146
249
  # It's possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of
147
250
  # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol, it
148
- # will be appened onto the <tt>:order</tt> passed in.
251
+ # will be appended onto the <tt>:order</tt> passed in.
252
+ #
253
+ # ==== Examples
254
+ # my_date = Time.today + 6.days
255
+ #
256
+ # # Generates a date select that defaults to the date in my_date (six days after today)
257
+ # select_date(my_date)
258
+ #
259
+ # # Generates a date select that defaults to today (no specified date)
260
+ # select_date()
261
+ #
262
+ # # Generates a date select that defaults to the date in my_date (six days after today)
263
+ # # with the fields ordered year, month, day rather than month, day, year.
264
+ # select_date(my_date, :order => [:year, :month, :day])
265
+ #
266
+ # # Generates a date select that discards the type of the field and defaults to the date in
267
+ # # my_date (six days after today)
268
+ # select_datetime(my_date_time, :discard_type => true)
269
+ #
270
+ # # Generates a date select that defaults to the datetime in my_date (six days after today)
271
+ # # prefixed with 'payday' rather than 'date'
272
+ # select_datetime(my_date_time, :prefix => 'payday')
273
+ #
149
274
  def select_date(date = Date.today, options = {})
150
275
  options[:order] ||= []
151
276
  [:year, :month, :day].each { |o| options[:order].push(o) unless options[:order].include?(o) }
@@ -158,7 +283,30 @@ module ActionView
158
283
  end
159
284
 
160
285
  # Returns a set of html select-tags (one for hour and minute)
161
- # You can set <tt>:add_separator</tt> key to format the output.
286
+ # You can set <tt>:time_separator</tt> key to format the output, and
287
+ # the <tt>:include_seconds</tt> option to include an input for seconds.
288
+ #
289
+ # ==== Examples
290
+ # my_time = Time.now + 5.days + 7.hours + 3.minutes + 14.seconds
291
+ #
292
+ # # Generates a time select that defaults to the time in my_time
293
+ # select_time(my_time)
294
+ #
295
+ # # Generates a time select that defaults to the current time (no specified time)
296
+ # select_time()
297
+ #
298
+ # # Generates a time select that defaults to the time in my_time,
299
+ # # which has fields separated by ':'
300
+ # select_time(my_time, :time_separator => ':')
301
+ #
302
+ # # Generates a time select that defaults to the time in my_time,
303
+ # # that also includes an input for seconds
304
+ # select_time(my_time, :include_seconds => true)
305
+ #
306
+ # # Generates a time select that defaults to the time in my_time, that has fields
307
+ # # separated by ':' and includes an input for seconds
308
+ # select_time(my_time, :time_separator => ':', :include_seconds => true)
309
+ #
162
310
  def select_time(datetime = Time.now, options = {})
163
311
  separator = options[:time_separator] || ''
164
312
  select_hour(datetime, options) + separator + select_minute(datetime, options) + (options[:include_seconds] ? separator + select_second(datetime, options) : '')
@@ -167,6 +315,20 @@ module ActionView
167
315
  # Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
168
316
  # The <tt>second</tt> can also be substituted for a second number.
169
317
  # Override the field name using the <tt>:field_name</tt> option, 'second' by default.
318
+ #
319
+ # ==== Examples
320
+ # my_time = Time.now + 16.minutes
321
+ #
322
+ # # Generates a select field for seconds that defaults to the seconds for the time in my_time
323
+ # select_second(my_time)
324
+ #
325
+ # # Generates a select field for seconds that defaults to the number given
326
+ # select_second(33)
327
+ #
328
+ # # Generates a select field for seconds that defaults to the seconds for the time in my_time
329
+ # # that is named 'interval' rather than 'second'
330
+ # select_second(my_time, :field_name => 'interval')
331
+ #
170
332
  def select_second(datetime, options = {})
171
333
  val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.sec) : ''
172
334
  if options[:use_hidden]
@@ -187,6 +349,20 @@ module ActionView
187
349
  # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute selected
188
350
  # The <tt>minute</tt> can also be substituted for a minute number.
189
351
  # Override the field name using the <tt>:field_name</tt> option, 'minute' by default.
352
+ #
353
+ # ==== Examples
354
+ # my_time = Time.now + 6.hours
355
+ #
356
+ # # Generates a select field for minutes that defaults to the minutes for the time in my_time
357
+ # select_minute(my_time)
358
+ #
359
+ # # Generates a select field for minutes that defaults to the number given
360
+ # select_minute(14)
361
+ #
362
+ # # Generates a select field for minutes that defaults to the minutes for the time in my_time
363
+ # # that is named 'stride' rather than 'second'
364
+ # select_minute(my_time, :field_name => 'stride')
365
+ #
190
366
  def select_minute(datetime, options = {})
191
367
  val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.min) : ''
192
368
  if options[:use_hidden]
@@ -206,6 +382,20 @@ module ActionView
206
382
  # Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
207
383
  # The <tt>hour</tt> can also be substituted for a hour number.
208
384
  # Override the field name using the <tt>:field_name</tt> option, 'hour' by default.
385
+ #
386
+ # ==== Examples
387
+ # my_time = Time.now + 6.hours
388
+ #
389
+ # # Generates a select field for minutes that defaults to the minutes for the time in my_time
390
+ # select_minute(my_time)
391
+ #
392
+ # # Generates a select field for minutes that defaults to the number given
393
+ # select_minute(14)
394
+ #
395
+ # # Generates a select field for minutes that defaults to the minutes for the time in my_time
396
+ # # that is named 'stride' rather than 'second'
397
+ # select_minute(my_time, :field_name => 'stride')
398
+ #
209
399
  def select_hour(datetime, options = {})
210
400
  val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
211
401
  if options[:use_hidden]
@@ -225,6 +415,20 @@ module ActionView
225
415
  # Returns a select tag with options for each of the days 1 through 31 with the current day selected.
226
416
  # The <tt>date</tt> can also be substituted for a hour number.
227
417
  # Override the field name using the <tt>:field_name</tt> option, 'day' by default.
418
+ #
419
+ # ==== Examples
420
+ # my_date = Time.today + 2.days
421
+ #
422
+ # # Generates a select field for days that defaults to the day for the date in my_date
423
+ # select_day(my_time)
424
+ #
425
+ # # Generates a select field for days that defaults to the number given
426
+ # select_day(5)
427
+ #
428
+ # # Generates a select field for days that defaults to the day for the date in my_date
429
+ # # that is named 'due' rather than 'day'
430
+ # select_day(my_time, :field_name => 'due')
431
+ #
228
432
  def select_day(date, options = {})
229
433
  val = date ? (date.kind_of?(Fixnum) ? date : date.day) : ''
230
434
  if options[:use_hidden]
@@ -247,17 +451,34 @@ module ActionView
247
451
  # set the <tt>:use_month_numbers</tt> key in +options+ to true for this to happen. If you want both numbers and names,
248
452
  # set the <tt>:add_month_numbers</tt> key in +options+ to true. If you would prefer to show month names as abbreviations,
249
453
  # set the <tt>:use_short_month</tt> key in +options+ to true. If you want to use your own month names, set the
250
- # <tt>:use_month_names</tt> key in +options+ to an array of 12 month names.
454
+ # <tt>:use_month_names</tt> key in +options+ to an array of 12 month names. Override the field name using the
455
+ # <tt>:field_name</tt> option, 'month' by default.
456
+ #
457
+ # ==== Examples
458
+ # # Generates a select field for months that defaults to the current month that
459
+ # # will use keys like "January", "March".
460
+ # select_month(Date.today)
461
+ #
462
+ # # Generates a select field for months that defaults to the current month that
463
+ # # is named "start" rather than "month"
464
+ # select_month(Date.today, :field_name => 'start')
251
465
  #
252
- # Examples:
466
+ # # Generates a select field for months that defaults to the current month that
467
+ # # will use keys like "1", "3".
468
+ # select_month(Date.today, :use_month_numbers => true)
253
469
  #
254
- # select_month(Date.today) # Will use keys like "January", "March"
255
- # select_month(Date.today, :use_month_numbers => true) # Will use keys like "1", "3"
256
- # select_month(Date.today, :add_month_numbers => true) # Will use keys like "1 - January", "3 - March"
257
- # select_month(Date.today, :use_short_month => true) # Will use keys like "Jan", "Mar"
258
- # select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...)) # Will use keys like "Januar", "Marts"
470
+ # # Generates a select field for months that defaults to the current month that
471
+ # # will use keys like "1 - January", "3 - March".
472
+ # select_month(Date.today, :add_month_numbers => true)
473
+ #
474
+ # # Generates a select field for months that defaults to the current month that
475
+ # # will use keys like "Jan", "Mar".
476
+ # select_month(Date.today, :use_short_month => true)
477
+ #
478
+ # # Generates a select field for months that defaults to the current month that
479
+ # # will use keys like "Januar", "Marts."
480
+ # select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...))
259
481
  #
260
- # Override the field name using the <tt>:field_name</tt> option, 'month' by default.
261
482
  def select_month(date, options = {})
262
483
  val = date ? (date.kind_of?(Fixnum) ? date : date.month) : ''
263
484
  if options[:use_hidden]
@@ -287,13 +508,25 @@ module ActionView
287
508
  # Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius
288
509
  # can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the +options+. Both ascending and descending year
289
510
  # lists are supported by making <tt>:start_year</tt> less than or greater than <tt>:end_year</tt>. The <tt>date</tt> can also be
290
- # substituted for a year given as a number. Example:
511
+ # substituted for a year given as a number. Override the field name using the <tt>:field_name</tt> option, 'year' by default.
512
+ #
513
+ # ==== Examples
514
+ # # Generates a select field for years that defaults to the current year that
515
+ # # has ascending year values
516
+ # select_year(Date.today, :start_year => 1992, :end_year => 2007)
517
+ #
518
+ # # Generates a select field for years that defaults to the current year that
519
+ # # is named 'birth' rather than 'year'
520
+ # select_year(Date.today, :field_name => 'birth')
521
+ #
522
+ # # Generates a select field for years that defaults to the current year that
523
+ # # has descending year values
524
+ # select_year(Date.today, :start_year => 2005, :end_year => 1900)
291
525
  #
292
- # select_year(Date.today, :start_year => 1992, :end_year => 2007) # ascending year values
293
- # select_year(Date.today, :start_year => 2005, :end_year => 1900) # descending year values
526
+ # # Generates a select field for years that defaults to the year 2006 that
527
+ # # has ascending year values
294
528
  # select_year(2006, :start_year => 2000, :end_year => 2010)
295
529
  #
296
- # Override the field name using the <tt>:field_name</tt> option, 'year' by default.
297
530
  def select_year(date, options = {})
298
531
  val = date ? (date.kind_of?(Fixnum) ? date : date.year) : ''
299
532
  if options[:use_hidden]
@@ -345,7 +578,7 @@ module ActionView
345
578
  include DateHelper
346
579
 
347
580
  def to_date_select_tag(options = {})
348
- date_or_time_select options.merge(:discard_hour => true)
581
+ date_or_time_select(options.merge(:discard_hour => true))
349
582
  end
350
583
 
351
584
  def to_time_select_tag(options = {})
@@ -361,7 +594,7 @@ module ActionView
361
594
  defaults = { :discard_type => true }
362
595
  options = defaults.merge(options)
363
596
  datetime = value(object)
364
- datetime ||= Time.now unless options[:include_blank]
597
+ datetime ||= default_time_from_options(options[:default]) unless options[:include_blank]
365
598
 
366
599
  position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
367
600
 
@@ -376,8 +609,15 @@ module ActionView
376
609
  discard[:minute] = true if options[:discard_minute] or discard[:hour]
377
610
  discard[:second] = true unless options[:include_seconds] && !discard[:minute]
378
611
 
612
+ # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are valid
613
+ # (otherwise it could be 31 and february wouldn't be a valid date)
614
+ if datetime && discard[:day] && !discard[:month]
615
+ datetime = datetime.change(:day => 1)
616
+ end
617
+
379
618
  # Maintain valid dates by including hidden fields for discarded elements
380
619
  [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
620
+
381
621
  # Ensure proper ordering of :hour, :minute and :second
382
622
  [:hour, :minute, :second].each { |o| order.delete(o); order.push(o) }
383
623
 
@@ -410,6 +650,26 @@ module ActionView
410
650
  end
411
651
  options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]")
412
652
  end
653
+
654
+ def default_time_from_options(default)
655
+ case default
656
+ when nil
657
+ Time.now
658
+ when Date, Time
659
+ default
660
+ else
661
+ # Rename :minute and :second to :min and :sec
662
+ default[:min] ||= default[:minute]
663
+ default[:sec] ||= default[:second]
664
+
665
+ [:year, :month, :day, :hour, :min, :sec].each do |key|
666
+ default[key] ||= Time.now.send(key)
667
+ end
668
+
669
+ Time.mktime(default[:year], default[:month], default[:day],
670
+ default[:hour], default[:min], default[:sec])
671
+ end
672
+ end
413
673
  end
414
674
 
415
675
  class FormBuilder