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,873 @@
1
+ require 'abstract_unit'
2
+
3
+ class ResourcesController < ActionController::Base
4
+ def index() render :nothing => true end
5
+ alias_method :show, :index
6
+ def rescue_action(e) raise e end
7
+ end
8
+
9
+ class ThreadsController < ResourcesController; end
10
+ class MessagesController < ResourcesController; end
11
+ class CommentsController < ResourcesController; end
12
+ class AuthorsController < ResourcesController; end
13
+ class LogosController < ResourcesController; end
14
+
15
+ class AccountsController < ResourcesController; end
16
+ class AdminController < ResourcesController; end
17
+
18
+ module Backoffice
19
+ class ProductsController < ResourcesController; end
20
+ class TagsController < ResourcesController; end
21
+ class ManufacturersController < ResourcesController; end
22
+ class ImagesController < ResourcesController; end
23
+
24
+ module Admin
25
+ class ProductsController < ResourcesController; end
26
+ class ImagesController < ResourcesController; end
27
+ end
28
+ end
29
+
30
+ class ResourcesTest < Test::Unit::TestCase
31
+ # The assertions in these tests are incompatible with the hash method
32
+ # optimisation. This could indicate user level problems
33
+ def setup
34
+ ActionController::Base.optimise_named_routes = false
35
+ end
36
+
37
+ def teardown
38
+ ActionController::Base.optimise_named_routes = true
39
+ end
40
+
41
+ def test_should_arrange_actions
42
+ resource = ActionController::Resources::Resource.new(:messages,
43
+ :collection => { :rss => :get, :reorder => :post, :csv => :post },
44
+ :member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
45
+ :new => { :preview => :get, :draft => :get })
46
+
47
+ assert_resource_methods [:rss], resource, :collection, :get
48
+ assert_resource_methods [:csv, :reorder], resource, :collection, :post
49
+ assert_resource_methods [:edit, :rss, :atom], resource, :member, :get
50
+ assert_resource_methods [:upload, :fix], resource, :member, :post
51
+ assert_resource_methods [:new, :preview, :draft], resource, :new, :get
52
+ end
53
+
54
+ def test_should_resource_controller_name_equal_resource_name_by_default
55
+ resource = ActionController::Resources::Resource.new(:messages, {})
56
+ assert_equal 'messages', resource.controller
57
+ end
58
+
59
+ def test_should_resource_controller_name_equal_controller_option
60
+ resource = ActionController::Resources::Resource.new(:messages, :controller => 'posts')
61
+ assert_equal 'posts', resource.controller
62
+ end
63
+
64
+ def test_should_all_singleton_paths_be_the_same
65
+ [ :path, :nesting_path_prefix, :member_path ].each do |method|
66
+ resource = ActionController::Resources::SingletonResource.new(:messages, :path_prefix => 'admin')
67
+ assert_equal 'admin/messages', resource.send(method)
68
+ end
69
+ end
70
+
71
+ def test_default_restful_routes
72
+ with_restful_routing :messages do
73
+ assert_simply_restful_for :messages
74
+ end
75
+ end
76
+
77
+ def test_override_paths_for_default_restful_actions
78
+ resource = ActionController::Resources::Resource.new(:messages,
79
+ :path_names => {:new => 'nuevo', :edit => 'editar'})
80
+ assert_equal resource.new_path, "#{resource.path}/nuevo"
81
+ end
82
+
83
+ def test_multiple_default_restful_routes
84
+ with_restful_routing :messages, :comments do
85
+ assert_simply_restful_for :messages
86
+ assert_simply_restful_for :comments
87
+ end
88
+ end
89
+
90
+ def test_with_custom_conditions
91
+ with_restful_routing :messages, :conditions => { :subdomain => 'app' } do
92
+ assert_equal 'app', ActionController::Routing::Routes.named_routes.routes[:messages].conditions[:subdomain]
93
+ end
94
+ end
95
+
96
+ def test_irregular_id_with_no_requirements_should_raise_error
97
+ expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
98
+
99
+ with_restful_routing :messages do
100
+ assert_raises(ActionController::RoutingError) do
101
+ assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
102
+ end
103
+ end
104
+ end
105
+
106
+ def test_irregular_id_with_requirements_should_pass
107
+ expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
108
+
109
+ with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
110
+ assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
111
+ end
112
+ end
113
+
114
+ def test_with_path_prefix_requirements
115
+ expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
116
+ with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
117
+ assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get)
118
+ end
119
+ end
120
+
121
+ def test_with_path_prefix
122
+ with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
123
+ assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
124
+ end
125
+ end
126
+
127
+ def test_multiple_with_path_prefix
128
+ with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
129
+ assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
130
+ assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
131
+ end
132
+ end
133
+
134
+ def test_with_name_prefix
135
+ with_restful_routing :messages, :name_prefix => 'post_' do
136
+ assert_simply_restful_for :messages, :name_prefix => 'post_'
137
+ end
138
+ end
139
+
140
+ def test_with_collection_actions
141
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
142
+
143
+ with_restful_routing :messages, :collection => actions do
144
+ assert_restful_routes_for :messages do |options|
145
+ actions.each do |action, method|
146
+ assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method)
147
+ end
148
+ end
149
+
150
+ assert_restful_named_routes_for :messages do |options|
151
+ actions.keys.each do |action|
152
+ assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ def test_with_collection_actions_and_name_prefix
159
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
160
+
161
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
162
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
163
+ actions.each do |action, method|
164
+ assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
165
+ end
166
+ end
167
+
168
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
169
+ actions.keys.each do |action|
170
+ assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ def test_with_collection_action_and_name_prefix_and_formatted
177
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
178
+
179
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
180
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
181
+ actions.each do |action, method|
182
+ assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method)
183
+ end
184
+ end
185
+
186
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
187
+ actions.keys.each do |action|
188
+ assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml'
189
+ end
190
+ end
191
+ end
192
+ end
193
+
194
+ def test_with_member_action
195
+ [:put, :post].each do |method|
196
+ with_restful_routing :messages, :member => { :mark => method } do
197
+ mark_options = {:action => 'mark', :id => '1'}
198
+ mark_path = "/messages/1/mark"
199
+ assert_restful_routes_for :messages do |options|
200
+ assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
201
+ end
202
+
203
+ assert_restful_named_routes_for :messages do |options|
204
+ assert_named_route mark_path, :mark_message_path, mark_options
205
+ end
206
+ end
207
+ end
208
+ end
209
+
210
+ def test_member_when_override_paths_for_default_restful_actions_with
211
+ [:put, :post].each do |method|
212
+ with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do
213
+ mark_options = {:action => 'mark', :id => '1', :controller => "messages"}
214
+ mark_path = "/messages/1/mark"
215
+
216
+ assert_restful_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
217
+ assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
218
+ end
219
+
220
+ assert_restful_named_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
221
+ assert_named_route mark_path, :mark_message_path, mark_options
222
+ end
223
+ end
224
+ end
225
+ end
226
+
227
+ def test_member_when_changed_default_restful_actions_and_path_names_not_specified
228
+ default_path_names = ActionController::Base.resources_path_names
229
+ ActionController::Base.resources_path_names = {:new => 'nuevo', :edit => 'editar'}
230
+
231
+ with_restful_routing :messages do
232
+ new_options = { :action => 'new', :controller => 'messages' }
233
+ new_path = "/messages/nuevo"
234
+ edit_options = { :action => 'edit', :id => '1', :controller => 'messages' }
235
+ edit_path = "/messages/1/editar"
236
+
237
+ assert_restful_routes_for :messages do |options|
238
+ assert_recognizes(options.merge(new_options), :path => new_path, :method => :get)
239
+ end
240
+
241
+ assert_restful_routes_for :messages do |options|
242
+ assert_recognizes(options.merge(edit_options), :path => edit_path, :method => :get)
243
+ end
244
+ end
245
+ ensure
246
+ ActionController::Base.resources_path_names = default_path_names
247
+ end
248
+
249
+ def test_with_two_member_actions_with_same_method
250
+ [:put, :post].each do |method|
251
+ with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
252
+ %w(mark unmark).each do |action|
253
+ action_options = {:action => action, :id => '1'}
254
+ action_path = "/messages/1/#{action}"
255
+ assert_restful_routes_for :messages do |options|
256
+ assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
257
+ end
258
+
259
+ assert_restful_named_routes_for :messages do |options|
260
+ assert_named_route action_path, "#{action}_message_path".to_sym, action_options
261
+ end
262
+ end
263
+ end
264
+ end
265
+ end
266
+
267
+ def test_with_new_action
268
+ with_restful_routing :messages, :new => { :preview => :post } do
269
+ preview_options = {:action => 'preview'}
270
+ preview_path = "/messages/new/preview"
271
+ assert_restful_routes_for :messages do |options|
272
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
273
+ end
274
+
275
+ assert_restful_named_routes_for :messages do |options|
276
+ assert_named_route preview_path, :preview_new_message_path, preview_options
277
+ end
278
+ end
279
+ end
280
+
281
+ def test_with_new_action_with_name_prefix
282
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
283
+ preview_options = {:action => 'preview', :thread_id => '1'}
284
+ preview_path = "/threads/1/messages/new/preview"
285
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
286
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
287
+ end
288
+
289
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
290
+ assert_named_route preview_path, :preview_new_thread_message_path, preview_options
291
+ end
292
+ end
293
+ end
294
+
295
+ def test_with_formatted_new_action_with_name_prefix
296
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
297
+ preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
298
+ preview_path = "/threads/1/messages/new/preview.xml"
299
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
300
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
301
+ end
302
+
303
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
304
+ assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options
305
+ end
306
+ end
307
+ end
308
+
309
+ def test_override_new_method
310
+ with_restful_routing :messages do
311
+ assert_restful_routes_for :messages do |options|
312
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
313
+ assert_raises(ActionController::MethodNotAllowed) do
314
+ ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
315
+ end
316
+ end
317
+ end
318
+
319
+ with_restful_routing :messages, :new => { :new => :any } do
320
+ assert_restful_routes_for :messages do |options|
321
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
322
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
323
+ end
324
+ end
325
+ end
326
+
327
+ def test_nested_restful_routes
328
+ with_routing do |set|
329
+ set.draw do |map|
330
+ map.resources :threads do |map|
331
+ map.resources :messages do |map|
332
+ map.resources :comments
333
+ end
334
+ end
335
+ end
336
+
337
+ assert_simply_restful_for :threads
338
+ assert_simply_restful_for :messages,
339
+ :name_prefix => 'thread_',
340
+ :path_prefix => 'threads/1/',
341
+ :options => { :thread_id => '1' }
342
+ assert_simply_restful_for :comments,
343
+ :name_prefix => 'thread_message_',
344
+ :path_prefix => 'threads/1/messages/2/',
345
+ :options => { :thread_id => '1', :message_id => '2' }
346
+ end
347
+ end
348
+
349
+ def test_nested_restful_routes_with_overwritten_defaults
350
+ with_routing do |set|
351
+ set.draw do |map|
352
+ map.resources :threads do |map|
353
+ map.resources :messages, :name_prefix => nil do |map|
354
+ map.resources :comments, :name_prefix => nil
355
+ end
356
+ end
357
+ end
358
+
359
+ assert_simply_restful_for :threads
360
+ assert_simply_restful_for :messages,
361
+ :path_prefix => 'threads/1/',
362
+ :options => { :thread_id => '1' }
363
+ assert_simply_restful_for :comments,
364
+ :path_prefix => 'threads/1/messages/2/',
365
+ :options => { :thread_id => '1', :message_id => '2' }
366
+ end
367
+ end
368
+
369
+ def test_restful_routes_dont_generate_duplicates
370
+ with_restful_routing :messages do
371
+ routes = ActionController::Routing::Routes.routes
372
+ routes.each do |route|
373
+ routes.each do |r|
374
+ next if route === r # skip the comparison instance
375
+ assert distinct_routes?(route, r), "Duplicate Route: #{route}"
376
+ end
377
+ end
378
+ end
379
+ end
380
+
381
+ def test_should_create_singleton_resource_routes
382
+ with_singleton_resources :account do
383
+ assert_singleton_restful_for :account
384
+ end
385
+ end
386
+
387
+ def test_should_create_multiple_singleton_resource_routes
388
+ with_singleton_resources :account, :logo do
389
+ assert_singleton_restful_for :account
390
+ assert_singleton_restful_for :logo
391
+ end
392
+ end
393
+
394
+ def test_should_create_nested_singleton_resource_routes
395
+ with_routing do |set|
396
+ set.draw do |map|
397
+ map.resource :admin, :controller => 'admin' do |admin|
398
+ admin.resource :account
399
+ end
400
+ end
401
+
402
+ assert_singleton_restful_for :admin, :controller => 'admin'
403
+ assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
404
+ end
405
+ end
406
+
407
+ def test_resource_has_many_should_become_nested_resources
408
+ with_routing do |set|
409
+ set.draw do |map|
410
+ map.resources :messages, :has_many => [ :comments, :authors ]
411
+ end
412
+
413
+ assert_simply_restful_for :messages
414
+ assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
415
+ assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
416
+ end
417
+ end
418
+
419
+ def test_resource_has_one_should_become_nested_resources
420
+ with_routing do |set|
421
+ set.draw do |map|
422
+ map.resources :messages, :has_one => :logo
423
+ end
424
+
425
+ assert_simply_restful_for :messages
426
+ assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
427
+ end
428
+ end
429
+
430
+ def test_singleton_resource_with_member_action
431
+ [:put, :post].each do |method|
432
+ with_singleton_resources :account, :member => { :reset => method } do
433
+ reset_options = {:action => 'reset'}
434
+ reset_path = "/account/reset"
435
+ assert_singleton_routes_for :account do |options|
436
+ assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method)
437
+ end
438
+
439
+ assert_singleton_named_routes_for :account do |options|
440
+ assert_named_route reset_path, :reset_account_path, reset_options
441
+ end
442
+ end
443
+ end
444
+ end
445
+
446
+ def test_singleton_resource_with_two_member_actions_with_same_method
447
+ [:put, :post].each do |method|
448
+ with_singleton_resources :account, :member => { :reset => method, :disable => method } do
449
+ %w(reset disable).each do |action|
450
+ action_options = {:action => action}
451
+ action_path = "/account/#{action}"
452
+ assert_singleton_routes_for :account do |options|
453
+ assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
454
+ end
455
+
456
+ assert_singleton_named_routes_for :account do |options|
457
+ assert_named_route action_path, "#{action}_account_path".to_sym, action_options
458
+ end
459
+ end
460
+ end
461
+ end
462
+ end
463
+
464
+ def test_should_nest_resources_in_singleton_resource
465
+ with_routing do |set|
466
+ set.draw do |map|
467
+ map.resource :account do |account|
468
+ account.resources :messages
469
+ end
470
+ end
471
+
472
+ assert_singleton_restful_for :account
473
+ assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
474
+ end
475
+ end
476
+
477
+ def test_should_nest_resources_in_singleton_resource_with_path_prefix
478
+ with_routing do |set|
479
+ set.draw do |map|
480
+ map.resource(:account, :path_prefix => ':site_id') do |account|
481
+ account.resources :messages
482
+ end
483
+ end
484
+
485
+ assert_singleton_restful_for :account, :path_prefix => '7/', :options => { :site_id => '7' }
486
+ assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
487
+ end
488
+ end
489
+
490
+ def test_should_nest_singleton_resource_in_resources
491
+ with_routing do |set|
492
+ set.draw do |map|
493
+ map.resources :threads do |thread|
494
+ thread.resource :admin, :controller => 'admin'
495
+ end
496
+ end
497
+
498
+ assert_simply_restful_for :threads
499
+ assert_singleton_restful_for :admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
500
+ end
501
+ end
502
+
503
+ def test_should_not_allow_delete_or_put_on_collection_path
504
+ controller_name = :messages
505
+ with_restful_routing controller_name do
506
+ options = { :controller => controller_name.to_s }
507
+ collection_path = "/#{controller_name}"
508
+
509
+ assert_raises(ActionController::MethodNotAllowed) do
510
+ assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
511
+ end
512
+
513
+ assert_raises(ActionController::MethodNotAllowed) do
514
+ assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
515
+ end
516
+ end
517
+ end
518
+
519
+ def test_resource_action_separator
520
+ with_routing do |set|
521
+ set.draw do |map|
522
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
523
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
524
+ end
525
+
526
+ action_separator = ActionController::Base.resource_action_separator
527
+
528
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
529
+ assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
530
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
531
+ assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
532
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
533
+ assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {}
534
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
535
+ assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
536
+ end
537
+ end
538
+
539
+ def test_new_style_named_routes_for_resource
540
+ with_routing do |set|
541
+ set.draw do |map|
542
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
543
+ end
544
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
545
+ assert_named_route "/threads/1/messages/search", "search_thread_messages_path", {}
546
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
547
+ assert_named_route "/threads/1/messages/new/preview", "preview_new_thread_message_path", {}
548
+ end
549
+ end
550
+
551
+ def test_new_style_named_routes_for_singleton_resource
552
+ with_routing do |set|
553
+ set.draw do |map|
554
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
555
+ end
556
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
557
+ assert_named_route "/admin/account/login", "login_admin_account_path", {}
558
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
559
+ assert_named_route "/admin/account/new/preview", "preview_new_admin_account_path", {}
560
+ end
561
+ end
562
+
563
+ def test_resources_in_namespace
564
+ with_routing do |set|
565
+ set.draw do |map|
566
+ map.namespace :backoffice do |backoffice|
567
+ backoffice.resources :products
568
+ end
569
+ end
570
+
571
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
572
+ end
573
+ end
574
+
575
+ def test_resource_has_many_in_namespace
576
+ with_routing do |set|
577
+ set.draw do |map|
578
+ map.namespace :backoffice do |backoffice|
579
+ backoffice.resources :products, :has_many => :tags
580
+ end
581
+ end
582
+
583
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
584
+ assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
585
+ end
586
+ end
587
+
588
+ def test_resource_has_one_in_namespace
589
+ with_routing do |set|
590
+ set.draw do |map|
591
+ map.namespace :backoffice do |backoffice|
592
+ backoffice.resources :products, :has_one => :manufacturer
593
+ end
594
+ end
595
+
596
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
597
+ assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
598
+ end
599
+ end
600
+
601
+ def test_resources_in_nested_namespace
602
+ with_routing do |set|
603
+ set.draw do |map|
604
+ map.namespace :backoffice do |backoffice|
605
+ backoffice.namespace :admin do |admin|
606
+ admin.resources :products
607
+ end
608
+ end
609
+ end
610
+
611
+ assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
612
+ end
613
+ end
614
+
615
+ def test_resources_using_namespace
616
+ with_routing do |set|
617
+ set.draw do |map|
618
+ map.resources :products, :namespace => "backoffice/"
619
+ end
620
+
621
+ assert_simply_restful_for :products, :controller => "backoffice/products"
622
+ end
623
+ end
624
+
625
+ def test_nested_resources_using_namespace
626
+ with_routing do |set|
627
+ set.draw do |map|
628
+ map.namespace :backoffice do |backoffice|
629
+ backoffice.resources :products do |products|
630
+ products.resources :images
631
+ end
632
+ end
633
+ end
634
+
635
+ assert_simply_restful_for :images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
636
+ end
637
+ end
638
+
639
+ def test_nested_resources_in_nested_namespace
640
+ with_routing do |set|
641
+ set.draw do |map|
642
+ map.namespace :backoffice do |backoffice|
643
+ backoffice.namespace :admin do |admin|
644
+ admin.resources :products do |products|
645
+ products.resources :images
646
+ end
647
+ end
648
+ end
649
+ end
650
+
651
+ assert_simply_restful_for :images, :controller => "backoffice/admin/images", :name_prefix => 'backoffice_admin_product_', :path_prefix => 'backoffice/admin/products/1/', :options => {:product_id => '1'}
652
+ end
653
+ end
654
+
655
+ def test_with_path_segment
656
+ with_restful_routing :messages, :as => 'reviews' do
657
+ assert_simply_restful_for :messages, :as => 'reviews'
658
+ end
659
+ end
660
+
661
+ def test_multiple_with_path_segment_and_controller
662
+ with_routing do |set|
663
+ set.draw do |map|
664
+ map.resources :products do |products|
665
+ products.resources :product_reviews, :as => 'reviews', :controller => 'messages'
666
+ end
667
+ map.resources :tutors do |tutors|
668
+ tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments'
669
+ end
670
+ end
671
+
672
+ assert_simply_restful_for :product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
673
+ assert_simply_restful_for :tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
674
+ end
675
+ end
676
+
677
+ def test_with_path_segment_path_prefix_requirements
678
+ expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
679
+ with_restful_routing :messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
680
+ assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get)
681
+ end
682
+ end
683
+
684
+ protected
685
+ def with_restful_routing(*args)
686
+ with_routing do |set|
687
+ set.draw { |map| map.resources(*args) }
688
+ yield
689
+ end
690
+ end
691
+
692
+ def with_singleton_resources(*args)
693
+ with_routing do |set|
694
+ set.draw { |map| map.resource(*args) }
695
+ yield
696
+ end
697
+ end
698
+
699
+ # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
700
+ def assert_simply_restful_for(controller_name, options = {})
701
+ assert_restful_routes_for controller_name, options
702
+ assert_restful_named_routes_for controller_name, nil, options
703
+ end
704
+
705
+ def assert_singleton_restful_for(singleton_name, options = {})
706
+ assert_singleton_routes_for singleton_name, options
707
+ assert_singleton_named_routes_for singleton_name, options
708
+ end
709
+
710
+ def assert_restful_routes_for(controller_name, options = {})
711
+ options[:options] ||= {}
712
+ options[:options][:controller] = options[:controller] || controller_name.to_s
713
+
714
+ new_action = ActionController::Base.resources_path_names[:new] || "new"
715
+ edit_action = ActionController::Base.resources_path_names[:edit] || "edit"
716
+ if options[:path_names]
717
+ new_action = options[:path_names][:new] if options[:path_names][:new]
718
+ edit_action = options[:path_names][:edit] if options[:path_names][:edit]
719
+ end
720
+
721
+ collection_path = "/#{options[:path_prefix]}#{options[:as] || controller_name}"
722
+ member_path = "#{collection_path}/1"
723
+ new_path = "#{collection_path}/#{new_action}"
724
+ edit_member_path = "#{member_path}/#{edit_action}"
725
+ formatted_edit_member_path = "#{member_path}/#{edit_action}.xml"
726
+
727
+ with_options(options[:options]) do |controller|
728
+ controller.assert_routing collection_path, :action => 'index'
729
+ controller.assert_routing new_path, :action => 'new'
730
+ controller.assert_routing member_path, :action => 'show', :id => '1'
731
+ controller.assert_routing edit_member_path, :action => 'edit', :id => '1'
732
+ controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
733
+ controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
734
+ controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
735
+ controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
736
+ end
737
+
738
+ assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
739
+ assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
740
+ assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
741
+ assert_recognizes(options[:options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
742
+ assert_recognizes(options[:options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
743
+ assert_recognizes(options[:options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
744
+ assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
745
+
746
+ assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
747
+ assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
748
+ assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
749
+ assert_recognizes(options[:options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
750
+ assert_recognizes(options[:options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
751
+ assert_recognizes(options[:options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
752
+ assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
753
+
754
+ yield options[:options] if block_given?
755
+ end
756
+
757
+ # test named routes like foo_path and foos_path map to the correct options.
758
+ def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
759
+ if singular_name.is_a?(Hash)
760
+ options = singular_name
761
+ singular_name = nil
762
+ end
763
+ singular_name ||= controller_name.to_s.singularize
764
+
765
+ options[:options] ||= {}
766
+ options[:options][:controller] = options[:controller] || controller_name.to_s
767
+
768
+ @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
769
+ @request = ActionController::TestRequest.new
770
+ @response = ActionController::TestResponse.new
771
+ get :index, options[:options]
772
+ options[:options].delete :action
773
+
774
+ full_prefix = "/#{options[:path_prefix]}#{options[:as] || controller_name}"
775
+ name_prefix = options[:name_prefix]
776
+
777
+ new_action = "new"
778
+ edit_action = "edit"
779
+ if options[:path_names]
780
+ new_action = options[:path_names][:new] || "new"
781
+ edit_action = options[:path_names][:edit] || "edit"
782
+ end
783
+
784
+ assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options]
785
+ assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml')
786
+ assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
787
+ assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
788
+
789
+ assert_named_route "#{full_prefix}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options]
790
+ assert_named_route "#{full_prefix}/#{new_action}.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml')
791
+ assert_named_route "#{full_prefix}/1/#{edit_action}", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
792
+ assert_named_route "#{full_prefix}/1/#{edit_action}.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
793
+
794
+ yield options[:options] if block_given?
795
+ end
796
+
797
+ def assert_singleton_routes_for(singleton_name, options = {})
798
+ options[:options] ||= {}
799
+ options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
800
+
801
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
802
+ new_path = "#{full_path}/new"
803
+ edit_path = "#{full_path}/edit"
804
+ formatted_edit_path = "#{full_path}/edit.xml"
805
+
806
+ with_options options[:options] do |controller|
807
+ controller.assert_routing full_path, :action => 'show'
808
+ controller.assert_routing new_path, :action => 'new'
809
+ controller.assert_routing edit_path, :action => 'edit'
810
+ controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
811
+ controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
812
+ controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
813
+ end
814
+
815
+ assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
816
+ assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
817
+ assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
818
+ assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
819
+ assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
820
+ assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
821
+
822
+ assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
823
+ assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
824
+ assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
825
+ assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
826
+ assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
827
+ assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
828
+
829
+ yield options[:options] if block_given?
830
+ end
831
+
832
+ def assert_singleton_named_routes_for(singleton_name, options = {})
833
+ (options[:options] ||= {})[:controller] ||= singleton_name.to_s.pluralize
834
+ @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
835
+ @request = ActionController::TestRequest.new
836
+ @response = ActionController::TestResponse.new
837
+ get :show, options[:options]
838
+ options[:options].delete :action
839
+
840
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
841
+ name_prefix = options[:name_prefix]
842
+
843
+ assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
844
+ assert_named_route "#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
845
+
846
+ assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
847
+ assert_named_route "#{full_path}/new.xml", "formatted_new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
848
+ assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
849
+ assert_named_route "#{full_path}/edit.xml", "formatted_edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
850
+ end
851
+
852
+ def assert_named_route(expected, route, options)
853
+ actual = @controller.send(route, options) rescue $!.class.name
854
+ assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
855
+ end
856
+
857
+ def assert_resource_methods(expected, resource, action_method, method)
858
+ assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
859
+ expected.each do |action|
860
+ assert resource.send("#{action_method}_methods")[method].include?(action),
861
+ "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
862
+ end
863
+ end
864
+
865
+ def distinct_routes? (r1, r2)
866
+ if r1.conditions == r2.conditions and r1.requirements == r2.requirements then
867
+ if r1.segments.collect(&:to_s) == r2.segments.collect(&:to_s) then
868
+ return false
869
+ end
870
+ end
871
+ true
872
+ end
873
+ end