eactionpack 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (338) hide show
  1. data/CHANGELOG +7 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +469 -0
  4. data/RUNNING_UNIT_TESTS +24 -0
  5. data/Rakefile +146 -0
  6. data/install.rb +30 -0
  7. data/lib/action_controller.rb +79 -0
  8. data/lib/action_controller/assertions.rb +69 -0
  9. data/lib/action_controller/assertions/dom_assertions.rb +39 -0
  10. data/lib/action_controller/assertions/model_assertions.rb +20 -0
  11. data/lib/action_controller/assertions/response_assertions.rb +172 -0
  12. data/lib/action_controller/assertions/routing_assertions.rb +146 -0
  13. data/lib/action_controller/assertions/selector_assertions.rb +491 -0
  14. data/lib/action_controller/assertions/tag_assertions.rb +130 -0
  15. data/lib/action_controller/base.rb +1288 -0
  16. data/lib/action_controller/benchmarking.rb +94 -0
  17. data/lib/action_controller/caching.rb +72 -0
  18. data/lib/action_controller/caching/actions.rb +144 -0
  19. data/lib/action_controller/caching/fragments.rb +138 -0
  20. data/lib/action_controller/caching/pages.rb +154 -0
  21. data/lib/action_controller/caching/sql_cache.rb +18 -0
  22. data/lib/action_controller/caching/sweeping.rb +97 -0
  23. data/lib/action_controller/cgi_ext.rb +16 -0
  24. data/lib/action_controller/cgi_ext/cookie.rb +110 -0
  25. data/lib/action_controller/cgi_ext/query_extension.rb +22 -0
  26. data/lib/action_controller/cgi_ext/session.rb +73 -0
  27. data/lib/action_controller/cgi_ext/stdinput.rb +24 -0
  28. data/lib/action_controller/cgi_process.rb +223 -0
  29. data/lib/action_controller/components.rb +166 -0
  30. data/lib/action_controller/cookies.rb +96 -0
  31. data/lib/action_controller/dispatcher.rb +162 -0
  32. data/lib/action_controller/filters.rb +642 -0
  33. data/lib/action_controller/flash.rb +172 -0
  34. data/lib/action_controller/headers.rb +31 -0
  35. data/lib/action_controller/helpers.rb +221 -0
  36. data/lib/action_controller/http_authentication.rb +124 -0
  37. data/lib/action_controller/integration.rb +634 -0
  38. data/lib/action_controller/layout.rb +309 -0
  39. data/lib/action_controller/mime_responds.rb +173 -0
  40. data/lib/action_controller/mime_type.rb +186 -0
  41. data/lib/action_controller/mime_types.rb +20 -0
  42. data/lib/action_controller/polymorphic_routes.rb +191 -0
  43. data/lib/action_controller/record_identifier.rb +102 -0
  44. data/lib/action_controller/request.rb +764 -0
  45. data/lib/action_controller/request_forgery_protection.rb +140 -0
  46. data/lib/action_controller/request_profiler.rb +169 -0
  47. data/lib/action_controller/rescue.rb +258 -0
  48. data/lib/action_controller/resources.rb +572 -0
  49. data/lib/action_controller/response.rb +76 -0
  50. data/lib/action_controller/routing.rb +387 -0
  51. data/lib/action_controller/routing/builder.rb +203 -0
  52. data/lib/action_controller/routing/optimisations.rb +120 -0
  53. data/lib/action_controller/routing/recognition_optimisation.rb +162 -0
  54. data/lib/action_controller/routing/route.rb +240 -0
  55. data/lib/action_controller/routing/route_set.rb +436 -0
  56. data/lib/action_controller/routing/routing_ext.rb +46 -0
  57. data/lib/action_controller/routing/segments.rb +283 -0
  58. data/lib/action_controller/session/active_record_store.rb +340 -0
  59. data/lib/action_controller/session/cookie_store.rb +166 -0
  60. data/lib/action_controller/session/drb_server.rb +32 -0
  61. data/lib/action_controller/session/drb_store.rb +35 -0
  62. data/lib/action_controller/session/mem_cache_store.rb +98 -0
  63. data/lib/action_controller/session_management.rb +158 -0
  64. data/lib/action_controller/status_codes.rb +88 -0
  65. data/lib/action_controller/streaming.rb +155 -0
  66. data/lib/action_controller/templates/rescues/_request_and_response.erb +24 -0
  67. data/lib/action_controller/templates/rescues/_trace.erb +26 -0
  68. data/lib/action_controller/templates/rescues/diagnostics.erb +11 -0
  69. data/lib/action_controller/templates/rescues/layout.erb +29 -0
  70. data/lib/action_controller/templates/rescues/missing_template.erb +2 -0
  71. data/lib/action_controller/templates/rescues/routing_error.erb +10 -0
  72. data/lib/action_controller/templates/rescues/template_error.erb +21 -0
  73. data/lib/action_controller/templates/rescues/unknown_action.erb +2 -0
  74. data/lib/action_controller/test_case.rb +83 -0
  75. data/lib/action_controller/test_process.rb +526 -0
  76. data/lib/action_controller/url_rewriter.rb +142 -0
  77. data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
  78. data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
  79. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +173 -0
  80. data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
  81. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
  82. data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
  83. data/lib/action_controller/verification.rb +130 -0
  84. data/lib/action_pack.rb +24 -0
  85. data/lib/action_pack/version.rb +9 -0
  86. data/lib/action_view.rb +44 -0
  87. data/lib/action_view/base.rb +335 -0
  88. data/lib/action_view/helpers/active_record_helper.rb +276 -0
  89. data/lib/action_view/helpers/asset_tag_helper.rb +599 -0
  90. data/lib/action_view/helpers/atom_feed_helper.rb +143 -0
  91. data/lib/action_view/helpers/benchmark_helper.rb +33 -0
  92. data/lib/action_view/helpers/cache_helper.rb +40 -0
  93. data/lib/action_view/helpers/capture_helper.rb +161 -0
  94. data/lib/action_view/helpers/date_helper.rb +711 -0
  95. data/lib/action_view/helpers/debug_helper.rb +31 -0
  96. data/lib/action_view/helpers/form_helper.rb +767 -0
  97. data/lib/action_view/helpers/form_options_helper.rb +458 -0
  98. data/lib/action_view/helpers/form_tag_helper.rb +458 -0
  99. data/lib/action_view/helpers/javascript_helper.rb +148 -0
  100. data/lib/action_view/helpers/number_helper.rb +186 -0
  101. data/lib/action_view/helpers/record_identification_helper.rb +20 -0
  102. data/lib/action_view/helpers/record_tag_helper.rb +59 -0
  103. data/lib/action_view/helpers/sanitize_helper.rb +229 -0
  104. data/lib/action_view/helpers/tag_helper.rb +134 -0
  105. data/lib/action_view/helpers/text_helper.rb +507 -0
  106. data/lib/action_view/helpers/url_helper.rb +573 -0
  107. data/lib/action_view/inline_template.rb +20 -0
  108. data/lib/action_view/partial_template.rb +70 -0
  109. data/lib/action_view/partials.rb +158 -0
  110. data/lib/action_view/template.rb +125 -0
  111. data/lib/action_view/template_error.rb +110 -0
  112. data/lib/action_view/template_finder.rb +176 -0
  113. data/lib/action_view/template_handler.rb +34 -0
  114. data/lib/action_view/template_handlers/builder.rb +27 -0
  115. data/lib/action_view/template_handlers/compilable.rb +128 -0
  116. data/lib/action_view/template_handlers/erb.rb +56 -0
  117. data/lib/action_view/test_case.rb +58 -0
  118. data/lib/actionpack.rb +1 -0
  119. data/test/abstract_unit.rb +36 -0
  120. data/test/active_record_unit.rb +105 -0
  121. data/test/activerecord/active_record_store_test.rb +141 -0
  122. data/test/activerecord/render_partial_with_record_identification_test.rb +191 -0
  123. data/test/adv_attr_test.rb +20 -0
  124. data/test/controller/action_pack_assertions_test.rb +543 -0
  125. data/test/controller/addresses_render_test.rb +43 -0
  126. data/test/controller/assert_select_test.rb +331 -0
  127. data/test/controller/base_test.rb +219 -0
  128. data/test/controller/benchmark_test.rb +32 -0
  129. data/test/controller/caching_test.rb +581 -0
  130. data/test/controller/capture_test.rb +89 -0
  131. data/test/controller/cgi_test.rb +116 -0
  132. data/test/controller/components_test.rb +140 -0
  133. data/test/controller/content_type_test.rb +139 -0
  134. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  135. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  136. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  137. data/test/controller/cookie_test.rb +146 -0
  138. data/test/controller/custom_handler_test.rb +45 -0
  139. data/test/controller/deprecation/deprecated_base_methods_test.rb +37 -0
  140. data/test/controller/dispatcher_test.rb +105 -0
  141. data/test/controller/fake_controllers.rb +33 -0
  142. data/test/controller/fake_models.rb +11 -0
  143. data/test/controller/filter_params_test.rb +49 -0
  144. data/test/controller/filters_test.rb +881 -0
  145. data/test/controller/flash_test.rb +146 -0
  146. data/test/controller/header_test.rb +14 -0
  147. data/test/controller/helper_test.rb +210 -0
  148. data/test/controller/html-scanner/cdata_node_test.rb +15 -0
  149. data/test/controller/html-scanner/document_test.rb +148 -0
  150. data/test/controller/html-scanner/node_test.rb +89 -0
  151. data/test/controller/html-scanner/sanitizer_test.rb +269 -0
  152. data/test/controller/html-scanner/tag_node_test.rb +238 -0
  153. data/test/controller/html-scanner/text_node_test.rb +50 -0
  154. data/test/controller/html-scanner/tokenizer_test.rb +131 -0
  155. data/test/controller/http_authentication_test.rb +54 -0
  156. data/test/controller/integration_test.rb +252 -0
  157. data/test/controller/integration_upload_test.rb +43 -0
  158. data/test/controller/layout_test.rb +255 -0
  159. data/test/controller/mime_responds_test.rb +514 -0
  160. data/test/controller/mime_type_test.rb +84 -0
  161. data/test/controller/new_render_test.rb +843 -0
  162. data/test/controller/polymorphic_routes_test.rb +174 -0
  163. data/test/controller/record_identifier_test.rb +139 -0
  164. data/test/controller/redirect_test.rb +289 -0
  165. data/test/controller/render_test.rb +484 -0
  166. data/test/controller/request_forgery_protection_test.rb +305 -0
  167. data/test/controller/request_test.rb +928 -0
  168. data/test/controller/rescue_test.rb +517 -0
  169. data/test/controller/resources_test.rb +873 -0
  170. data/test/controller/routing_test.rb +2464 -0
  171. data/test/controller/selector_test.rb +628 -0
  172. data/test/controller/send_file_test.rb +138 -0
  173. data/test/controller/session/cookie_store_test.rb +258 -0
  174. data/test/controller/session/mem_cache_store_test.rb +181 -0
  175. data/test/controller/session_fixation_test.rb +89 -0
  176. data/test/controller/session_management_test.rb +178 -0
  177. data/test/controller/test_test.rb +695 -0
  178. data/test/controller/url_rewriter_test.rb +310 -0
  179. data/test/controller/verification_test.rb +270 -0
  180. data/test/controller/view_paths_test.rb +140 -0
  181. data/test/controller/webservice_test.rb +229 -0
  182. data/test/fixtures/addresses/list.erb +1 -0
  183. data/test/fixtures/bad_customers/_bad_customer.html.erb +1 -0
  184. data/test/fixtures/companies.yml +24 -0
  185. data/test/fixtures/company.rb +10 -0
  186. data/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +1 -0
  187. data/test/fixtures/content_type/render_default_for_js.js.erb +1 -0
  188. data/test/fixtures/content_type/render_default_for_rhtml.rhtml +1 -0
  189. data/test/fixtures/content_type/render_default_for_rxml.rxml +1 -0
  190. data/test/fixtures/customers/_customer.html.erb +1 -0
  191. data/test/fixtures/db_definitions/sqlite.sql +49 -0
  192. data/test/fixtures/developer.rb +9 -0
  193. data/test/fixtures/developers.yml +21 -0
  194. data/test/fixtures/developers_projects.yml +13 -0
  195. data/test/fixtures/fun/games/hello_world.erb +1 -0
  196. data/test/fixtures/functional_caching/_partial.erb +3 -0
  197. data/test/fixtures/functional_caching/fragment_cached.html.erb +2 -0
  198. data/test/fixtures/functional_caching/html_fragment_cached_with_partial.html.erb +1 -0
  199. data/test/fixtures/functional_caching/js_fragment_cached_with_partial.js.rjs +1 -0
  200. data/test/fixtures/good_customers/_good_customer.html.erb +1 -0
  201. data/test/fixtures/helpers/abc_helper.rb +5 -0
  202. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  203. data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
  204. data/test/fixtures/layout_tests/alt/hello.rhtml +1 -0
  205. data/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml +1 -0
  206. data/test/fixtures/layout_tests/layouts/item.rhtml +1 -0
  207. data/test/fixtures/layout_tests/layouts/layout_test.rhtml +1 -0
  208. data/test/fixtures/layout_tests/layouts/multiple_extensions.html.erb +1 -0
  209. data/test/fixtures/layout_tests/layouts/third_party_template_library.mab +1 -0
  210. data/test/fixtures/layout_tests/views/hello.rhtml +1 -0
  211. data/test/fixtures/layouts/block_with_layout.erb +3 -0
  212. data/test/fixtures/layouts/builder.builder +3 -0
  213. data/test/fixtures/layouts/partial_with_layout.erb +3 -0
  214. data/test/fixtures/layouts/standard.erb +1 -0
  215. data/test/fixtures/layouts/talk_from_action.erb +2 -0
  216. data/test/fixtures/layouts/yield.erb +2 -0
  217. data/test/fixtures/mascot.rb +3 -0
  218. data/test/fixtures/mascots.yml +4 -0
  219. data/test/fixtures/mascots/_mascot.html.erb +1 -0
  220. data/test/fixtures/multipart/binary_file +0 -0
  221. data/test/fixtures/multipart/boundary_problem_file +10 -0
  222. data/test/fixtures/multipart/bracketed_param +5 -0
  223. data/test/fixtures/multipart/large_text_file +10 -0
  224. data/test/fixtures/multipart/mixed_files +0 -0
  225. data/test/fixtures/multipart/mona_lisa.jpg +0 -0
  226. data/test/fixtures/multipart/single_parameter +5 -0
  227. data/test/fixtures/multipart/text_file +10 -0
  228. data/test/fixtures/override/test/hello_world.erb +1 -0
  229. data/test/fixtures/override2/layouts/test/sub.erb +1 -0
  230. data/test/fixtures/post_test/layouts/post.html.erb +1 -0
  231. data/test/fixtures/post_test/layouts/super_post.iphone.erb +1 -0
  232. data/test/fixtures/post_test/post/index.html.erb +1 -0
  233. data/test/fixtures/post_test/post/index.iphone.erb +1 -0
  234. data/test/fixtures/post_test/super_post/index.html.erb +1 -0
  235. data/test/fixtures/post_test/super_post/index.iphone.erb +1 -0
  236. data/test/fixtures/project.rb +3 -0
  237. data/test/fixtures/projects.yml +7 -0
  238. data/test/fixtures/public/404.html +1 -0
  239. data/test/fixtures/public/500.html +1 -0
  240. data/test/fixtures/public/images/rails.png +0 -0
  241. data/test/fixtures/public/javascripts/application.js +1 -0
  242. data/test/fixtures/public/javascripts/bank.js +1 -0
  243. data/test/fixtures/public/javascripts/robber.js +1 -0
  244. data/test/fixtures/public/javascripts/version.1.0.js +1 -0
  245. data/test/fixtures/public/stylesheets/bank.css +1 -0
  246. data/test/fixtures/public/stylesheets/robber.css +1 -0
  247. data/test/fixtures/public/stylesheets/version.1.0.css +1 -0
  248. data/test/fixtures/replies.yml +15 -0
  249. data/test/fixtures/reply.rb +7 -0
  250. data/test/fixtures/respond_to/all_types_with_layout.html.erb +1 -0
  251. data/test/fixtures/respond_to/custom_constant_handling_without_block.mobile.erb +1 -0
  252. data/test/fixtures/respond_to/iphone_with_html_response_type.html.erb +1 -0
  253. data/test/fixtures/respond_to/iphone_with_html_response_type.iphone.erb +1 -0
  254. data/test/fixtures/respond_to/layouts/missing.html.erb +1 -0
  255. data/test/fixtures/respond_to/layouts/standard.html.erb +1 -0
  256. data/test/fixtures/respond_to/layouts/standard.iphone.erb +1 -0
  257. data/test/fixtures/respond_to/using_defaults.html.erb +1 -0
  258. data/test/fixtures/respond_to/using_defaults.js.rjs +1 -0
  259. data/test/fixtures/respond_to/using_defaults.xml.builder +1 -0
  260. data/test/fixtures/respond_to/using_defaults_with_type_list.html.erb +1 -0
  261. data/test/fixtures/respond_to/using_defaults_with_type_list.js.rjs +1 -0
  262. data/test/fixtures/respond_to/using_defaults_with_type_list.xml.builder +1 -0
  263. data/test/fixtures/scope/test/modgreet.erb +1 -0
  264. data/test/fixtures/shared.html.erb +1 -0
  265. data/test/fixtures/symlink_parent/symlinked_layout.erb +5 -0
  266. data/test/fixtures/test/_customer.erb +1 -0
  267. data/test/fixtures/test/_customer_counter.erb +1 -0
  268. data/test/fixtures/test/_customer_greeting.erb +1 -0
  269. data/test/fixtures/test/_form.erb +1 -0
  270. data/test/fixtures/test/_hash_greeting.erb +1 -0
  271. data/test/fixtures/test/_hash_object.erb +2 -0
  272. data/test/fixtures/test/_hello.builder +1 -0
  273. data/test/fixtures/test/_labelling_form.erb +1 -0
  274. data/test/fixtures/test/_layout_for_partial.html.erb +3 -0
  275. data/test/fixtures/test/_partial.erb +1 -0
  276. data/test/fixtures/test/_partial.html.erb +1 -0
  277. data/test/fixtures/test/_partial.js.erb +1 -0
  278. data/test/fixtures/test/_partial_for_use_in_layout.html.erb +1 -0
  279. data/test/fixtures/test/_partial_only.erb +1 -0
  280. data/test/fixtures/test/_person.erb +2 -0
  281. data/test/fixtures/test/_raise.html.erb +1 -0
  282. data/test/fixtures/test/action_talk_to_layout.erb +2 -0
  283. data/test/fixtures/test/block_content_for.erb +2 -0
  284. data/test/fixtures/test/calling_partial_with_layout.html.erb +1 -0
  285. data/test/fixtures/test/capturing.erb +4 -0
  286. data/test/fixtures/test/content_for.erb +2 -0
  287. data/test/fixtures/test/content_for_concatenated.erb +3 -0
  288. data/test/fixtures/test/content_for_with_parameter.erb +2 -0
  289. data/test/fixtures/test/delete_with_js.rjs +2 -0
  290. data/test/fixtures/test/dot.directory/render_file_with_ivar.erb +1 -0
  291. data/test/fixtures/test/enum_rjs_test.rjs +6 -0
  292. data/test/fixtures/test/erb_content_for.erb +2 -0
  293. data/test/fixtures/test/formatted_html_erb.html.erb +1 -0
  294. data/test/fixtures/test/formatted_xml_erb.builder +1 -0
  295. data/test/fixtures/test/formatted_xml_erb.html.erb +1 -0
  296. data/test/fixtures/test/formatted_xml_erb.xml.erb +1 -0
  297. data/test/fixtures/test/greeting.erb +1 -0
  298. data/test/fixtures/test/greeting.js.rjs +1 -0
  299. data/test/fixtures/test/hello.builder +4 -0
  300. data/test/fixtures/test/hello_world.erb +1 -0
  301. data/test/fixtures/test/hello_world_container.builder +3 -0
  302. data/test/fixtures/test/hello_world_from_rxml.builder +4 -0
  303. data/test/fixtures/test/hello_world_with_layout_false.erb +1 -0
  304. data/test/fixtures/test/hello_xml_world.builder +11 -0
  305. data/test/fixtures/test/list.erb +1 -0
  306. data/test/fixtures/test/non_erb_block_content_for.builder +4 -0
  307. data/test/fixtures/test/potential_conflicts.erb +4 -0
  308. data/test/fixtures/test/render_file_from_template.html.erb +1 -0
  309. data/test/fixtures/test/render_file_with_ivar.erb +1 -0
  310. data/test/fixtures/test/render_file_with_locals.erb +1 -0
  311. data/test/fixtures/test/render_to_string_test.erb +1 -0
  312. data/test/fixtures/test/update_element_with_capture.erb +9 -0
  313. data/test/fixtures/test/using_layout_around_block.html.erb +1 -0
  314. data/test/fixtures/topic.rb +3 -0
  315. data/test/fixtures/topics.yml +22 -0
  316. data/test/fixtures/topics/_topic.html.erb +1 -0
  317. data/test/template/active_record_helper_test.rb +268 -0
  318. data/test/template/asset_tag_helper_test.rb +514 -0
  319. data/test/template/atom_feed_helper_test.rb +179 -0
  320. data/test/template/benchmark_helper_test.rb +60 -0
  321. data/test/template/date_helper_test.rb +1791 -0
  322. data/test/template/deprecated_erb_variable_test.rb +9 -0
  323. data/test/template/erb_util_test.rb +24 -0
  324. data/test/template/form_helper_test.rb +885 -0
  325. data/test/template/form_options_helper_test.rb +1333 -0
  326. data/test/template/form_tag_helper_test.rb +272 -0
  327. data/test/template/javascript_helper_test.rb +73 -0
  328. data/test/template/number_helper_test.rb +97 -0
  329. data/test/template/record_tag_helper_test.rb +54 -0
  330. data/test/template/sanitize_helper_test.rb +48 -0
  331. data/test/template/tag_helper_test.rb +77 -0
  332. data/test/template/template_finder_test.rb +73 -0
  333. data/test/template/template_object_test.rb +95 -0
  334. data/test/template/test_test.rb +56 -0
  335. data/test/template/text_helper_test.rb +367 -0
  336. data/test/template/url_helper_test.rb +544 -0
  337. data/test/testing_sandbox.rb +15 -0
  338. metadata +469 -0
@@ -0,0 +1,458 @@
1
+ require 'cgi'
2
+ require 'erb'
3
+ require 'action_view/helpers/form_helper'
4
+
5
+ module ActionView
6
+ module Helpers
7
+ # Provides a number of methods for turning different kinds of containers into a set of option tags.
8
+ # == Options
9
+ # The <tt>collection_select</tt>, <tt>country_select</tt>, <tt>select</tt>,
10
+ # and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter,
11
+ # a hash.
12
+ #
13
+ # * <tt>:include_blank</tt> - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
14
+ #
15
+ # For example,
16
+ #
17
+ # select("post", "category", Post::CATEGORIES, {:include_blank => true})
18
+ #
19
+ # could become:
20
+ #
21
+ # <select name="post[category]">
22
+ # <option></option>
23
+ # <option>joke</option>
24
+ # <option>poem</option>
25
+ # </select>
26
+ #
27
+ # Another common case is a select tag for an <tt>belongs_to</tt>-associated object.
28
+ #
29
+ # Example with @post.person_id => 2:
30
+ #
31
+ # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'})
32
+ #
33
+ # could become:
34
+ #
35
+ # <select name="post[person_id]">
36
+ # <option value="">None</option>
37
+ # <option value="1">David</option>
38
+ # <option value="2" selected="selected">Sam</option>
39
+ # <option value="3">Tobias</option>
40
+ # </select>
41
+ #
42
+ # * <tt>:prompt</tt> - set to true or a prompt string. When the select element doesn't have a value yet, this prepends an option with a generic prompt -- "Please select" -- or the given prompt string.
43
+ #
44
+ # Example:
45
+ #
46
+ # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
47
+ #
48
+ # could become:
49
+ #
50
+ # <select name="post[person_id]">
51
+ # <option value="">Select Person</option>
52
+ # <option value="1">David</option>
53
+ # <option value="2">Sam</option>
54
+ # <option value="3">Tobias</option>
55
+ # </select>
56
+ #
57
+ # Like the other form helpers, +select+ can accept an <tt>:index</tt> option to manually set the ID used in the resulting output. Unlike other helpers, +select+ expects this
58
+ # option to be in the +html_options+ parameter.
59
+ #
60
+ # Example:
61
+ #
62
+ # select("album[]", "genre", %w[rap rock country], {}, { :index => nil })
63
+ #
64
+ # becomes:
65
+ #
66
+ # <select name="album[][genre]" id="album__genre">
67
+ # <option value="rap">rap</option>
68
+ # <option value="rock">rock</option>
69
+ # <option value="country">country</option>
70
+ # </select>
71
+ module FormOptionsHelper
72
+ include ERB::Util
73
+
74
+ # Create a select tag and a series of contained option tags for the provided object and method.
75
+ # The option currently held by the object will be selected, provided that the object is available.
76
+ # See options_for_select for the required format of the choices parameter.
77
+ #
78
+ # Example with @post.person_id => 1:
79
+ # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, { :include_blank => true })
80
+ #
81
+ # could become:
82
+ #
83
+ # <select name="post[person_id]">
84
+ # <option value=""></option>
85
+ # <option value="1" selected="selected">David</option>
86
+ # <option value="2">Sam</option>
87
+ # <option value="3">Tobias</option>
88
+ # </select>
89
+ #
90
+ # This can be used to provide a default set of options in the standard way: before rendering the create form, a
91
+ # new model instance is assigned the default options and bound to @model_name. Usually this model is not saved
92
+ # to the database. Instead, a second model object is created when the create request is received.
93
+ # This allows the user to submit a form page more than once with the expected results of creating multiple records.
94
+ # In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
95
+ #
96
+ # By default, <tt>post.person_id</tt> is the selected option. Specify <tt>:selected => value</tt> to use a different selection
97
+ # or <tt>:selected => nil</tt> to leave all options unselected.
98
+ def select(object, method, choices, options = {}, html_options = {})
99
+ InstanceTag.new(object, method, self, nil, options.delete(:object)).to_select_tag(choices, options, html_options)
100
+ end
101
+
102
+ # Returns <tt><select></tt> and <tt><option></tt> tags for the collection of existing return values of
103
+ # +method+ for +object+'s class. The value returned from calling +method+ on the instance +object+ will
104
+ # be selected. If calling +method+ returns +nil+, no selection is made without including <tt>:prompt</tt>
105
+ # or <tt>:include_blank</tt> in the +options+ hash.
106
+ #
107
+ # The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods to be called on each member
108
+ # of +collection+. The return values are used as the +value+ attribute and contents of each
109
+ # <tt><option></tt> tag, respectively.
110
+ #
111
+ # Example object structure for use with this method:
112
+ # class Post < ActiveRecord::Base
113
+ # belongs_to :author
114
+ # end
115
+ # class Author < ActiveRecord::Base
116
+ # has_many :posts
117
+ # def name_with_initial
118
+ # "#{first_name.first}. #{last_name}"
119
+ # end
120
+ # end
121
+ #
122
+ # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
123
+ # collection_select(:post, :author_id, Author.find(:all), :id, :name_with_initial, {:prompt => true})
124
+ #
125
+ # If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
126
+ # <select name="post[author_id]">
127
+ # <option value="">Please select</option>
128
+ # <option value="1" selected="selected">D. Heinemeier Hansson</option>
129
+ # <option value="2">D. Thomas</option>
130
+ # <option value="3">M. Clark</option>
131
+ # </select>
132
+ def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
133
+ InstanceTag.new(object, method, self, nil, options.delete(:object)).to_collection_select_tag(collection, value_method, text_method, options, html_options)
134
+ end
135
+
136
+ # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
137
+ def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
138
+ InstanceTag.new(object, method, self, nil, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
139
+ end
140
+
141
+ # Return select and option tags for the given object and method, using
142
+ # #time_zone_options_for_select to generate the list of option tags.
143
+ #
144
+ # In addition to the <tt>:include_blank</tt> option documented above,
145
+ # this method also supports a <tt>:model</tt> option, which defaults
146
+ # to TimeZone. This may be used by users to specify a different time
147
+ # zone model object. (See +time_zone_options_for_select+ for more
148
+ # information.)
149
+ #
150
+ # You can also supply an array of TimeZone objects
151
+ # as +priority_zones+, so that they will be listed above the rest of the
152
+ # (long) list. (You can use TimeZone.us_zones as a convenience for
153
+ # obtaining a list of the US time zones.)
154
+ #
155
+ # Finally, this method supports a <tt>:default</tt> option, which selects
156
+ # a default TimeZone if the object's time zone is +nil+.
157
+ #
158
+ # Examples:
159
+ # time_zone_select( "user", "time_zone", nil, :include_blank => true)
160
+ #
161
+ # time_zone_select( "user", "time_zone", nil, :default => "Pacific Time (US & Canada)" )
162
+ #
163
+ # time_zone_select( "user", 'time_zone', TimeZone.us_zones, :default => "Pacific Time (US & Canada)")
164
+ #
165
+ # time_zone_select( "user", 'time_zone', [ TimeZone['Alaska'], TimeZone['Hawaii'] ])
166
+ #
167
+ # time_zone_select( "user", "time_zone", TZInfo::Timezone.all.sort, :model => TZInfo::Timezone)
168
+ def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {})
169
+ InstanceTag.new(object, method, self, nil, options.delete(:object)).to_time_zone_select_tag(priority_zones, options, html_options)
170
+ end
171
+
172
+ # Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container
173
+ # where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and
174
+ # the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values
175
+ # become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag. +selected+
176
+ # may also be an array of values to be selected when using a multiple select.
177
+ #
178
+ # Examples (call, result):
179
+ # options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])
180
+ # <option value="$">Dollar</option>\n<option value="DKK">Kroner</option>
181
+ #
182
+ # options_for_select([ "VISA", "MasterCard" ], "MasterCard")
183
+ # <option>VISA</option>\n<option selected="selected">MasterCard</option>
184
+ #
185
+ # options_for_select({ "Basic" => "$20", "Plus" => "$40" }, "$40")
186
+ # <option value="$20">Basic</option>\n<option value="$40" selected="selected">Plus</option>
187
+ #
188
+ # options_for_select([ "VISA", "MasterCard", "Discover" ], ["VISA", "Discover"])
189
+ # <option selected="selected">VISA</option>\n<option>MasterCard</option>\n<option selected="selected">Discover</option>
190
+ #
191
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
192
+ def options_for_select(container, selected = nil)
193
+ container = container.to_a if Hash === container
194
+
195
+ options_for_select = container.inject([]) do |options, element|
196
+ text, value = option_text_and_value(element)
197
+ selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
198
+ options << %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}>#{html_escape(text.to_s)}</option>)
199
+ end
200
+
201
+ options_for_select.join("\n")
202
+ end
203
+
204
+ # Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the
205
+ # the result of a call to the +value_method+ as the option value and the +text_method+ as the option text.
206
+ # If +selected+ is specified, the element returning a match on +value_method+ will get the selected option tag.
207
+ #
208
+ # Example (call, result). Imagine a loop iterating over each +person+ in <tt>@project.people</tt> to generate an input tag:
209
+ # options_from_collection_for_select(@project.people, "id", "name")
210
+ # <option value="#{person.id}">#{person.name}</option>
211
+ #
212
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
213
+ def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
214
+ options = collection.map do |element|
215
+ [element.send(text_method), element.send(value_method)]
216
+ end
217
+ options_for_select(options, selected)
218
+ end
219
+
220
+ # Returns a string of <tt><option></tt> tags, like <tt>options_from_collection_for_select</tt>, but
221
+ # groups them by <tt><optgroup></tt> tags based on the object relationships of the arguments.
222
+ #
223
+ # Parameters:
224
+ # * +collection+ - An array of objects representing the <tt><optgroup></tt> tags.
225
+ # * +group_method+ - The name of a method which, when called on a member of +collection+, returns an
226
+ # array of child objects representing the <tt><option></tt> tags.
227
+ # * group_label_method+ - The name of a method which, when called on a member of +collection+, returns a
228
+ # string to be used as the +label+ attribute for its <tt><optgroup></tt> tag.
229
+ # * +option_key_method+ - The name of a method which, when called on a child object of a member of
230
+ # +collection+, returns a value to be used as the +value+ attribute for its <tt><option></tt> tag.
231
+ # * +option_value_method+ - The name of a method which, when called on a child object of a member of
232
+ # +collection+, returns a value to be used as the contents of its <tt><option></tt> tag.
233
+ # * +selected_key+ - A value equal to the +value+ attribute for one of the <tt><option></tt> tags,
234
+ # which will have the +selected+ attribute set. Corresponds to the return value of one of the calls
235
+ # to +option_key_method+. If +nil+, no selection is made.
236
+ #
237
+ # Example object structure for use with this method:
238
+ # class Continent < ActiveRecord::Base
239
+ # has_many :countries
240
+ # # attribs: id, name
241
+ # end
242
+ # class Country < ActiveRecord::Base
243
+ # belongs_to :continent
244
+ # # attribs: id, name, continent_id
245
+ # end
246
+ #
247
+ # Sample usage:
248
+ # option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
249
+ #
250
+ # Possible output:
251
+ # <optgroup label="Africa">
252
+ # <option value="1">Egypt</option>
253
+ # <option value="4">Rwanda</option>
254
+ # ...
255
+ # </optgroup>
256
+ # <optgroup label="Asia">
257
+ # <option value="3" selected="selected">China</option>
258
+ # <option value="12">India</option>
259
+ # <option value="5">Japan</option>
260
+ # ...
261
+ # </optgroup>
262
+ #
263
+ # <b>Note:</b> Only the <tt><optgroup></tt> and <tt><option></tt> tags are returned, so you still have to
264
+ # wrap the output in an appropriate <tt><select></tt> tag.
265
+ def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)
266
+ collection.inject("") do |options_for_select, group|
267
+ group_label_string = eval("group.#{group_label_method}")
268
+ options_for_select += "<optgroup label=\"#{html_escape(group_label_string)}\">"
269
+ options_for_select += options_from_collection_for_select(eval("group.#{group_method}"), option_key_method, option_value_method, selected_key)
270
+ options_for_select += '</optgroup>'
271
+ end
272
+ end
273
+
274
+ # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
275
+ # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so
276
+ # that they will be listed above the rest of the (long) list.
277
+ #
278
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
279
+ def country_options_for_select(selected = nil, priority_countries = nil)
280
+ country_options = ""
281
+
282
+ if priority_countries
283
+ country_options += options_for_select(priority_countries, selected)
284
+ country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
285
+ end
286
+
287
+ return country_options + options_for_select(COUNTRIES, selected)
288
+ end
289
+
290
+ # Returns a string of option tags for pretty much any time zone in the
291
+ # world. Supply a TimeZone name as +selected+ to have it marked as the
292
+ # selected option tag. You can also supply an array of TimeZone objects
293
+ # as +priority_zones+, so that they will be listed above the rest of the
294
+ # (long) list. (You can use TimeZone.us_zones as a convenience for
295
+ # obtaining a list of the US time zones.)
296
+ #
297
+ # The +selected+ parameter must be either +nil+, or a string that names
298
+ # a TimeZone.
299
+ #
300
+ # By default, +model+ is the TimeZone constant (which can be obtained
301
+ # in Active Record as a value object). The only requirement is that the
302
+ # +model+ parameter be an object that responds to +all+, and returns
303
+ # an array of objects that represent time zones.
304
+ #
305
+ # NOTE: Only the option tags are returned, you have to wrap this call in
306
+ # a regular HTML select tag.
307
+ def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
308
+ zone_options = ""
309
+
310
+ zones = model.all
311
+ convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }
312
+
313
+ if priority_zones
314
+ zone_options += options_for_select(convert_zones[priority_zones], selected)
315
+ zone_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
316
+
317
+ zones = zones.reject { |z| priority_zones.include?( z ) }
318
+ end
319
+
320
+ zone_options += options_for_select(convert_zones[zones], selected)
321
+ zone_options
322
+ end
323
+
324
+ private
325
+ def option_text_and_value(option)
326
+ # Options are [text, value] pairs or strings used for both.
327
+ if !option.is_a?(String) and option.respond_to?(:first) and option.respond_to?(:last)
328
+ [option.first, option.last]
329
+ else
330
+ [option, option]
331
+ end
332
+ end
333
+
334
+ def option_value_selected?(value, selected)
335
+ if selected.respond_to?(:include?) && !selected.is_a?(String)
336
+ selected.include? value
337
+ else
338
+ value == selected
339
+ end
340
+ end
341
+
342
+ # All the countries included in the country_options output.
343
+ COUNTRIES = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola",
344
+ "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria",
345
+ "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
346
+ "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil",
347
+ "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
348
+ "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
349
+ "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
350
+ "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba",
351
+ "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt",
352
+ "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)",
353
+ "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia",
354
+ "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea",
355
+ "Guinea-Bissau", "Guyana", "Haiti", "Heard and McDonald Islands", "Holy See (Vatican City State)",
356
+ "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq",
357
+ "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya",
358
+ "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan",
359
+ "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya",
360
+ "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, The Former Yugoslav Republic Of",
361
+ "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
362
+ "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
363
+ "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru",
364
+ "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
365
+ "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau",
366
+ "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
367
+ "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation",
368
+ "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",
369
+ "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
370
+ "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore",
371
+ "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa",
372
+ "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname",
373
+ "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic",
374
+ "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Timor-Leste",
375
+ "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
376
+ "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
377
+ "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela",
378
+ "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.", "Wallis and Futuna", "Western Sahara",
379
+ "Yemen", "Zambia", "Zimbabwe"] unless const_defined?("COUNTRIES")
380
+ end
381
+
382
+ class InstanceTag #:nodoc:
383
+ include FormOptionsHelper
384
+
385
+ def to_select_tag(choices, options, html_options)
386
+ html_options = html_options.stringify_keys
387
+ add_default_name_and_id(html_options)
388
+ value = value(object)
389
+ selected_value = options.has_key?(:selected) ? options[:selected] : value
390
+ content_tag("select", add_options(options_for_select(choices, selected_value), options, selected_value), html_options)
391
+ end
392
+
393
+ def to_collection_select_tag(collection, value_method, text_method, options, html_options)
394
+ html_options = html_options.stringify_keys
395
+ add_default_name_and_id(html_options)
396
+ value = value(object)
397
+ content_tag(
398
+ "select", add_options(options_from_collection_for_select(collection, value_method, text_method, value), options, value), html_options
399
+ )
400
+ end
401
+
402
+ def to_country_select_tag(priority_countries, options, html_options)
403
+ ActiveSupport::Deprecation.warn("country_select will be removed from 2.2.0. http://www.rubyonrails.org/deprecation/list-of-countries has more information.", caller)
404
+ html_options = html_options.stringify_keys
405
+ add_default_name_and_id(html_options)
406
+ value = value(object)
407
+ content_tag("select",
408
+ add_options(
409
+ country_options_for_select(value, priority_countries),
410
+ options, value
411
+ ), html_options
412
+ )
413
+ end
414
+
415
+ def to_time_zone_select_tag(priority_zones, options, html_options)
416
+ html_options = html_options.stringify_keys
417
+ add_default_name_and_id(html_options)
418
+ value = value(object)
419
+ content_tag("select",
420
+ add_options(
421
+ time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone),
422
+ options, value
423
+ ), html_options
424
+ )
425
+ end
426
+
427
+ private
428
+ def add_options(option_tags, options, value = nil)
429
+ if options[:include_blank]
430
+ option_tags = "<option value=\"\">#{options[:include_blank] if options[:include_blank].kind_of?(String)}</option>\n" + option_tags
431
+ end
432
+ if value.blank? && options[:prompt]
433
+ ("<option value=\"\">#{options[:prompt].kind_of?(String) ? options[:prompt] : 'Please select'}</option>\n") + option_tags
434
+ else
435
+ option_tags
436
+ end
437
+ end
438
+ end
439
+
440
+ class FormBuilder
441
+ def select(method, choices, options = {}, html_options = {})
442
+ @template.select(@object_name, method, choices, options.merge(:object => @object), html_options)
443
+ end
444
+
445
+ def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
446
+ @template.collection_select(@object_name, method, collection, value_method, text_method, options.merge(:object => @object), html_options)
447
+ end
448
+
449
+ def country_select(method, priority_countries = nil, options = {}, html_options = {})
450
+ @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
451
+ end
452
+
453
+ def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
454
+ @template.time_zone_select(@object_name, method, priority_zones, options.merge(:object => @object), html_options)
455
+ end
456
+ end
457
+ end
458
+ end