typus 3.0.6 → 3.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (295) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +15 -0
  3. data/Gemfile +9 -5
  4. data/Rakefile +34 -21
  5. data/app/controllers/admin/base_controller.rb +0 -4
  6. data/app/controllers/admin/resources_controller.rb +77 -213
  7. data/app/controllers/admin/session_controller.rb +1 -1
  8. data/app/helpers/admin/base_helper.rb +4 -4
  9. data/app/helpers/admin/dashboard_helper.rb +1 -3
  10. data/app/helpers/admin/display_helper.rb +49 -0
  11. data/app/helpers/admin/file_preview_helper.rb +10 -12
  12. data/app/helpers/admin/filters_helper.rb +1 -1
  13. data/app/helpers/admin/form_helper.rb +2 -13
  14. data/app/helpers/admin/list_helper.rb +14 -3
  15. data/app/helpers/admin/relationships_helper.rb +79 -62
  16. data/app/helpers/admin/resources_helper.rb +17 -16
  17. data/app/helpers/admin/search_helper.rb +1 -1
  18. data/app/helpers/admin/sidebar_helper.rb +1 -2
  19. data/app/helpers/admin/table_helper.rb +9 -4
  20. data/app/views/admin/helpers/_file_preview.html.erb +1 -1
  21. data/app/views/admin/helpers/base/_apps.html.erb +23 -0
  22. data/app/views/admin/helpers/{_flash_message.html.erb → base/_flash_message.html.erb} +0 -0
  23. data/app/views/admin/helpers/{_header.html.erb → base/_header.html.erb} +0 -0
  24. data/app/views/admin/helpers/{_login_info.html.erb → base/_login_info.html.erb} +0 -0
  25. data/app/views/admin/helpers/resources/_display_link_to_previous.html.erb +1 -1
  26. data/app/views/admin/resources/show.html.erb +4 -27
  27. data/app/views/admin/templates/_belongs_to_with_autocomplete.html.erb +11 -0
  28. data/app/views/admin/templates/_has_n.html.erb +1 -1
  29. data/app/views/admin/templates/_has_one.html.erb +3 -3
  30. data/app/views/admin/templates/_relate_form.html.erb +1 -1
  31. data/app/views/admin/templates/_relate_form_with_autocomplete.html.erb +24 -0
  32. data/app/views/layouts/admin/base.html.erb +15 -9
  33. data/app/views/layouts/admin/headless.html.erb +44 -0
  34. data/app/views/layouts/admin/session.html.erb +3 -2
  35. data/doc/hudson_setup.md +19 -0
  36. data/lib/generators/typus/assets_generator.rb +0 -4
  37. data/lib/generators/typus/config_generator.rb +107 -0
  38. data/lib/generators/typus/controller_generator.rb +0 -4
  39. data/lib/generators/typus/initializers_generator.rb +22 -0
  40. data/lib/generators/typus/migration_generator.rb +0 -4
  41. data/lib/generators/typus/typus_generator.rb +8 -101
  42. data/lib/generators/typus/views_generator.rb +0 -4
  43. data/lib/support/active_record.rb +8 -2
  44. data/lib/support/fake_user.rb +2 -0
  45. data/lib/support/hash.rb +1 -1
  46. data/lib/support/string.rb +3 -3
  47. data/lib/tasks/typus.rake +1 -1
  48. data/lib/typus.rb +15 -6
  49. data/lib/typus/controller/actions.rb +69 -0
  50. data/lib/typus/controller/associations.rb +82 -0
  51. data/lib/typus/controller/autocomplete.rb +16 -0
  52. data/lib/typus/controller/filters.rb +24 -0
  53. data/lib/typus/controller/format.rb +70 -0
  54. data/lib/typus/orm/active_record.rb +9 -240
  55. data/lib/typus/orm/active_record/class_methods.rb +227 -0
  56. data/lib/typus/orm/active_record/instance_methods.rb +13 -0
  57. data/lib/typus/orm/active_record/search.rb +17 -5
  58. data/lib/typus/orm/active_record/user.rb +157 -0
  59. data/lib/typus/regex.rb +11 -0
  60. data/lib/typus/version.rb +1 -1
  61. data/test/app/controllers/admin/account_controller_test.rb +104 -0
  62. data/test/app/controllers/admin/assets_controller_test.rb +153 -0
  63. data/test/app/controllers/admin/base_controller_test.rb +41 -0
  64. data/test/app/controllers/admin/birds_controller_test.rb +42 -0
  65. data/test/app/controllers/admin/cases_controller_test.rb +21 -0
  66. data/test/app/controllers/admin/categories_controller_test.rb +141 -0
  67. data/test/app/controllers/admin/comments_controller_test.rb +79 -0
  68. data/test/app/controllers/admin/dashboard_controller_test.rb +162 -0
  69. data/test/app/controllers/admin/image_holders_controller_test.rb +75 -0
  70. data/test/app/controllers/admin/invoices_controller_test.rb +78 -0
  71. data/test/app/controllers/admin/orders_controller_test.rb +18 -0
  72. data/test/app/controllers/admin/pages_controller_test.rb +37 -0
  73. data/test/app/controllers/admin/posts_controller_test.rb +745 -0
  74. data/test/app/controllers/admin/projects_controller_test.rb +52 -0
  75. data/test/app/controllers/admin/session_controller_test.rb +92 -0
  76. data/test/app/controllers/admin/status_controller_test.rb +54 -0
  77. data/test/app/controllers/admin/typus_users_controller_test.rb +160 -0
  78. data/test/app/controllers/admin/users_controller_test.rb +106 -0
  79. data/test/app/helpers/admin/base_helper_test.rb +83 -0
  80. data/test/app/helpers/admin/dashboard_helper_test.rb +32 -0
  81. data/test/app/helpers/admin/file_preview_helper_test.rb +82 -0
  82. data/test/app/helpers/admin/filters_helper_test.rb +161 -0
  83. data/test/app/helpers/admin/form_helper_test.rb +126 -0
  84. data/test/app/helpers/admin/list_helper_test.rb +59 -0
  85. data/test/app/helpers/admin/relationships_helper_test.rb +19 -0
  86. data/test/app/helpers/admin/resources_helper_test.rb +46 -0
  87. data/test/app/helpers/admin/search_helper_test.rb +60 -0
  88. data/test/app/helpers/admin/sidebar_helper_test.rb +34 -0
  89. data/test/app/helpers/admin/table_helper_test.rb +215 -0
  90. data/test/app/mailers/admin/mailer_test.rb +32 -0
  91. data/test/app/models/typus_user_roles_test.rb +124 -0
  92. data/test/app/models/typus_user_test.rb +194 -0
  93. data/test/config/routes_test.rb +29 -0
  94. data/test/factories.rb +111 -0
  95. data/test/fixtures/config/broken/application.yml +68 -0
  96. data/test/fixtures/config/broken/application_roles.yml +20 -0
  97. data/test/fixtures/config/broken/empty.yml +0 -0
  98. data/test/fixtures/config/broken/empty_roles.yml +0 -0
  99. data/test/fixtures/config/broken/undefined.yml +3 -0
  100. data/test/fixtures/config/broken/undefined_roles.yml +6 -0
  101. data/test/fixtures/config/default/typus.yml +13 -0
  102. data/test/fixtures/config/default/typus_roles.yml +2 -0
  103. data/test/fixtures/config/empty/empty_01.yml +0 -0
  104. data/test/fixtures/config/empty/empty_01_roles.yml +0 -0
  105. data/test/fixtures/config/empty/empty_02.yml +0 -0
  106. data/test/fixtures/config/empty/empty_02_roles.yml +0 -0
  107. data/test/fixtures/config/locales/es.yml +10 -0
  108. data/test/fixtures/config/namespaced/application.yml +5 -0
  109. data/test/fixtures/config/namespaced/application_roles.yml +2 -0
  110. data/test/fixtures/config/ordered/001_roles.yml +2 -0
  111. data/test/fixtures/config/ordered/002_roles.yml +2 -0
  112. data/test/fixtures/config/unordered/app_one_roles.yml +2 -0
  113. data/test/fixtures/config/unordered/app_two_roles.yml +2 -0
  114. data/test/fixtures/rails_app/.gitignore +4 -0
  115. data/test/fixtures/rails_app/Capfile +6 -0
  116. data/test/fixtures/rails_app/README +256 -0
  117. data/test/fixtures/rails_app/Rakefile +7 -0
  118. data/test/fixtures/rails_app/app/controllers/admin/animals_controller.rb +2 -0
  119. data/test/fixtures/rails_app/app/controllers/admin/articles_controller.rb +2 -0
  120. data/test/fixtures/rails_app/app/controllers/admin/assets_controller.rb +51 -0
  121. data/test/fixtures/rails_app/app/controllers/admin/birds_controller.rb +2 -0
  122. data/test/fixtures/rails_app/app/controllers/admin/cases_controller.rb +2 -0
  123. data/test/fixtures/rails_app/app/controllers/admin/categories_controller.rb +2 -0
  124. data/test/fixtures/rails_app/app/controllers/admin/comments_controller.rb +2 -0
  125. data/test/fixtures/rails_app/app/controllers/admin/dogs_controller.rb +2 -0
  126. data/test/fixtures/rails_app/app/controllers/admin/entries_controller.rb +2 -0
  127. data/test/fixtures/rails_app/app/controllers/admin/image_holders_controller.rb +2 -0
  128. data/test/fixtures/rails_app/app/controllers/admin/invoices_controller.rb +2 -0
  129. data/test/fixtures/rails_app/app/controllers/admin/orders_controller.rb +2 -0
  130. data/test/fixtures/rails_app/app/controllers/admin/pages_controller.rb +7 -0
  131. data/test/fixtures/rails_app/app/controllers/admin/posts_controller.rb +2 -0
  132. data/test/fixtures/rails_app/app/controllers/admin/project_collaborators_controller.rb +2 -0
  133. data/test/fixtures/rails_app/app/controllers/admin/projects_controller.rb +2 -0
  134. data/test/fixtures/rails_app/app/controllers/admin/status_controller.rb +2 -0
  135. data/test/fixtures/rails_app/app/controllers/admin/typus_users_controller.rb +2 -0
  136. data/test/fixtures/rails_app/app/controllers/admin/users_controller.rb +2 -0
  137. data/test/fixtures/rails_app/app/controllers/admin/views_controller.rb +2 -0
  138. data/test/fixtures/rails_app/app/controllers/admin/watch_dog_controller.rb +2 -0
  139. data/test/fixtures/rails_app/app/controllers/application_controller.rb +3 -0
  140. data/test/fixtures/rails_app/app/helpers/application_helper.rb +2 -0
  141. data/test/fixtures/rails_app/app/helpers/form_helper.rb +3 -0
  142. data/test/fixtures/rails_app/app/models/animal.rb +21 -0
  143. data/test/fixtures/rails_app/app/models/article.rb +10 -0
  144. data/test/fixtures/rails_app/app/models/asset.rb +39 -0
  145. data/test/fixtures/rails_app/app/models/bird.rb +2 -0
  146. data/test/fixtures/rails_app/app/models/case.rb +2 -0
  147. data/test/fixtures/rails_app/app/models/category.rb +31 -0
  148. data/test/fixtures/rails_app/app/models/comment.rb +17 -0
  149. data/test/fixtures/rails_app/app/models/delayed/task.rb +5 -0
  150. data/test/fixtures/rails_app/app/models/dog.rb +2 -0
  151. data/test/fixtures/rails_app/app/models/entry.rb +23 -0
  152. data/test/fixtures/rails_app/app/models/image_holder.rb +21 -0
  153. data/test/fixtures/rails_app/app/models/invoice.rb +10 -0
  154. data/test/fixtures/rails_app/app/models/order.rb +9 -0
  155. data/test/fixtures/rails_app/app/models/page.rb +24 -0
  156. data/test/fixtures/rails_app/app/models/post.rb +32 -0
  157. data/test/fixtures/rails_app/app/models/project.rb +17 -0
  158. data/test/fixtures/rails_app/app/models/project_collaborator.rb +16 -0
  159. data/test/fixtures/rails_app/app/models/typus_user.rb +22 -0
  160. data/test/fixtures/rails_app/app/models/user.rb +10 -0
  161. data/test/fixtures/rails_app/app/models/view.rb +17 -0
  162. data/test/fixtures/rails_app/app/views/admin/assets/_index.html.erb +3 -0
  163. data/test/fixtures/rails_app/app/views/admin/cases/_index.html.erb +3 -0
  164. data/test/fixtures/rails_app/app/views/admin/categories/_index.html.erb +3 -0
  165. data/test/fixtures/rails_app/app/views/admin/dashboard/_sidebar.html.erb +11 -0
  166. data/test/fixtures/rails_app/app/views/admin/entries/_index.html.erb +3 -0
  167. data/test/fixtures/rails_app/app/views/admin/pages/_index.html.erb +3 -0
  168. data/test/fixtures/rails_app/app/views/admin/posts/_edit.html.erb +1 -0
  169. data/test/fixtures/rails_app/app/views/admin/posts/_index.html.erb +5 -0
  170. data/test/fixtures/rails_app/app/views/admin/posts/_new.html.erb +1 -0
  171. data/test/fixtures/rails_app/app/views/admin/posts/_show.html.erb +1 -0
  172. data/test/fixtures/rails_app/app/views/admin/resources/_sidebar.html.erb +1 -0
  173. data/test/fixtures/rails_app/app/views/admin/status/index.html.erb +9 -0
  174. data/test/fixtures/rails_app/app/views/admin/templates/_datepicker.html.erb +1 -0
  175. data/test/fixtures/rails_app/app/views/admin/watch_dog/index.html.erb +9 -0
  176. data/test/fixtures/rails_app/app/views/layouts/admin/headless.html.erb +38 -0
  177. data/test/fixtures/rails_app/config.ru +4 -0
  178. data/test/fixtures/rails_app/config/application.rb +44 -0
  179. data/test/fixtures/rails_app/config/boot.rb +13 -0
  180. data/test/fixtures/rails_app/config/database.yml +34 -0
  181. data/test/fixtures/rails_app/config/deploy.rb +32 -0
  182. data/test/fixtures/rails_app/config/environment.rb +5 -0
  183. data/test/fixtures/rails_app/config/environments/development.rb +32 -0
  184. data/test/fixtures/rails_app/config/environments/production.rb +54 -0
  185. data/test/fixtures/rails_app/config/environments/test.rb +40 -0
  186. data/test/fixtures/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  187. data/test/fixtures/rails_app/config/initializers/dragonfly.rb +1 -0
  188. data/test/fixtures/rails_app/config/initializers/inflections.rb +12 -0
  189. data/test/fixtures/rails_app/config/initializers/mime_types.rb +5 -0
  190. data/test/fixtures/rails_app/config/initializers/secret_token.rb +7 -0
  191. data/test/fixtures/rails_app/config/initializers/session_store.rb +8 -0
  192. data/test/fixtures/rails_app/config/initializers/typus.rb +5 -0
  193. data/test/fixtures/rails_app/config/initializers/typus_resources.rb +27 -0
  194. data/test/fixtures/rails_app/config/locales/en.yml +5 -0
  195. data/test/fixtures/rails_app/config/routes.rb +61 -0
  196. data/test/fixtures/rails_app/config/typus/README +68 -0
  197. data/test/fixtures/rails_app/config/typus/crud_basic.yml +10 -0
  198. data/test/fixtures/rails_app/config/typus/crud_basic_roles.yml +2 -0
  199. data/test/fixtures/rails_app/config/typus/crud_extended.yml +64 -0
  200. data/test/fixtures/rails_app/config/typus/crud_extended_roles.yml +18 -0
  201. data/test/fixtures/rails_app/config/typus/has_many.yml +0 -0
  202. data/test/fixtures/rails_app/config/typus/has_many_roles.yml +0 -0
  203. data/test/fixtures/rails_app/config/typus/has_many_through.yml +23 -0
  204. data/test/fixtures/rails_app/config/typus/has_many_through_roles.yml +4 -0
  205. data/test/fixtures/rails_app/config/typus/has_one.yml +12 -0
  206. data/test/fixtures/rails_app/config/typus/has_one_roles.yml +3 -0
  207. data/test/fixtures/rails_app/config/typus/polymorphic.yml +17 -0
  208. data/test/fixtures/rails_app/config/typus/polymorphic_roles.yml +5 -0
  209. data/test/fixtures/rails_app/config/typus/resource_roles.yml +7 -0
  210. data/test/fixtures/rails_app/config/typus/sti.yml +5 -0
  211. data/test/fixtures/rails_app/config/typus/sti_roles.yml +2 -0
  212. data/test/fixtures/rails_app/config/typus/typus.yml +13 -0
  213. data/test/fixtures/rails_app/config/typus/typus_roles.yml +2 -0
  214. data/test/fixtures/rails_app/config/typus/unknown.yml +3 -0
  215. data/test/fixtures/rails_app/config/typus/unknown_roles.yml +5 -0
  216. data/test/fixtures/rails_app/db/.gitkeep +0 -0
  217. data/test/fixtures/rails_app/db/schema.rb +174 -0
  218. data/test/fixtures/rails_app/db/seeds.rb +54 -0
  219. data/test/fixtures/rails_app/db/seeds/assets/000.jpg +0 -0
  220. data/test/fixtures/rails_app/db/seeds/assets/001.jpg +0 -0
  221. data/test/fixtures/rails_app/db/seeds/assets/002.jpg +0 -0
  222. data/test/fixtures/rails_app/db/seeds/assets/003.jpg +0 -0
  223. data/test/fixtures/rails_app/db/seeds/assets/004.jpg +0 -0
  224. data/test/fixtures/rails_app/public/404.html +26 -0
  225. data/test/fixtures/rails_app/public/422.html +26 -0
  226. data/test/fixtures/rails_app/public/500.html +26 -0
  227. data/test/fixtures/rails_app/public/favicon.ico +0 -0
  228. data/test/fixtures/rails_app/public/images/rails.png +0 -0
  229. data/test/fixtures/rails_app/public/javascripts/application.js +2 -0
  230. data/test/fixtures/rails_app/public/javascripts/controls.js +965 -0
  231. data/test/fixtures/rails_app/public/javascripts/dragdrop.js +974 -0
  232. data/test/fixtures/rails_app/public/javascripts/effects.js +1123 -0
  233. data/test/fixtures/rails_app/public/javascripts/prototype.js +6001 -0
  234. data/test/fixtures/rails_app/public/javascripts/rails.js +175 -0
  235. data/test/fixtures/rails_app/public/robots.txt +5 -0
  236. data/test/fixtures/rails_app/public/stylesheets/.gitkeep +0 -0
  237. data/test/fixtures/rails_app/script/rails +6 -0
  238. data/test/lib/support/active_record_test.rb +150 -0
  239. data/test/lib/support/fake_user_test.rb +59 -0
  240. data/test/lib/support/hash_test.rb +29 -0
  241. data/test/lib/support/object_test.rb +17 -0
  242. data/test/lib/support/string_test.rb +116 -0
  243. data/test/lib/typus/configuration_test.rb +46 -0
  244. data/test/lib/typus/controller/actions_test.rb +141 -0
  245. data/test/lib/typus/controller/associations_test.rb +7 -0
  246. data/test/lib/typus/controller/autocomplete_test.rb +7 -0
  247. data/test/lib/typus/controller/filters_test.rb +73 -0
  248. data/test/lib/typus/controller/format_test.rb +7 -0
  249. data/test/lib/typus/i18n_test.rb +9 -0
  250. data/test/lib/typus/orm/active_record/class_methods_test.rb +378 -0
  251. data/test/lib/typus/orm/active_record/search_test.rb +330 -0
  252. data/test/lib/typus/regex_test.rb +53 -0
  253. data/test/lib/typus/resources_test.rb +41 -0
  254. data/test/lib/typus_test.rb +141 -0
  255. data/test/test_helper.rb +15 -0
  256. data/typus.gemspec +25 -0
  257. metadata +586 -64
  258. data/Gemfile.lock +0 -149
  259. data/app/views/admin/helpers/_apps.html.erb +0 -9
  260. data/lib/generators/templates/public/admin/javascripts/application.js +0 -2
  261. data/lib/generators/templates/public/admin/javascripts/jquery-1.4.4.min.js +0 -167
  262. data/lib/generators/templates/public/admin/javascripts/jquery.application.js +0 -16
  263. data/lib/generators/templates/public/admin/javascripts/jquery.rails.js +0 -160
  264. data/lib/generators/templates/public/admin/javascripts/jquery.searchField.js +0 -91
  265. data/lib/generators/templates/public/admin/stylesheets/application.css +0 -11
  266. data/lib/generators/templates/public/admin/stylesheets/screen.css +0 -367
  267. data/lib/generators/templates/public/admin/vendor/fancybox/blank.gif +0 -0
  268. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_close.png +0 -0
  269. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_loading.png +0 -0
  270. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_nav_left.png +0 -0
  271. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_nav_right.png +0 -0
  272. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_e.png +0 -0
  273. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_n.png +0 -0
  274. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_ne.png +0 -0
  275. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_nw.png +0 -0
  276. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_s.png +0 -0
  277. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_se.png +0 -0
  278. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_sw.png +0 -0
  279. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_shadow_w.png +0 -0
  280. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_title_left.png +0 -0
  281. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_title_main.png +0 -0
  282. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_title_over.png +0 -0
  283. data/lib/generators/templates/public/admin/vendor/fancybox/fancy_title_right.png +0 -0
  284. data/lib/generators/templates/public/admin/vendor/fancybox/fancybox-x.png +0 -0
  285. data/lib/generators/templates/public/admin/vendor/fancybox/fancybox-y.png +0 -0
  286. data/lib/generators/templates/public/admin/vendor/fancybox/fancybox.png +0 -0
  287. data/lib/generators/templates/public/admin/vendor/fancybox/jquery.easing-1.3.pack.js +0 -72
  288. data/lib/generators/templates/public/admin/vendor/fancybox/jquery.fancybox-1.3.4.css +0 -359
  289. data/lib/generators/templates/public/admin/vendor/fancybox/jquery.fancybox-1.3.4.js +0 -1156
  290. data/lib/generators/templates/public/admin/vendor/fancybox/jquery.fancybox-1.3.4.pack.js +0 -46
  291. data/lib/generators/templates/public/admin/vendor/fancybox/jquery.mousewheel-3.0.4.pack.js +0 -14
  292. data/lib/typus/actions.rb +0 -41
  293. data/lib/typus/filters.rb +0 -12
  294. data/lib/typus/format.rb +0 -70
  295. data/lib/typus/user.rb +0 -157
@@ -0,0 +1,75 @@
1
+ require "test_helper"
2
+
3
+ =begin
4
+
5
+ What's being tested here?
6
+
7
+ - Polymorphic associations.
8
+
9
+ =end
10
+
11
+ class Admin::ImageHoldersControllerTest < ActionController::TestCase
12
+
13
+ setup do
14
+ @typus_user = Factory(:typus_user)
15
+ @request.session[:typus_user_id] = @typus_user.id
16
+ end
17
+
18
+ context "create polymorphic association" do
19
+
20
+ setup do
21
+ @bird = Factory(:bird)
22
+ end
23
+
24
+ should "contain a message" do
25
+ get :new, { :resource => @bird.class.name, :resource_id => @bird.id }
26
+ assert_select 'body div#flash', "Cancel adding a new image holder?"
27
+ end
28
+
29
+ should "work" do
30
+ assert_difference('@bird.image_holders.count') do
31
+ post :create, { :image_holder => { :name => "ImageHolder" },
32
+ :resource => "Bird", :resource_id => @bird.id }
33
+ end
34
+
35
+ assert_response :redirect
36
+ assert_redirected_to "http://test.host/admin/birds/edit/#{@bird.id}"
37
+ assert_equal "Bird successfully updated.", flash[:notice]
38
+ end
39
+
40
+ end
41
+
42
+ ##
43
+ # TODO: Eventually this code should be run in the Original model, so ideally
44
+ # a Bird unrelates ImageHolder and not ImageHolder unrelates Bird.
45
+ #
46
+ context "unrelate" do
47
+
48
+ ##
49
+ # We are in:
50
+ #
51
+ # /admin/birds/edit/1
52
+ #
53
+ # And we see a list of comments under it:
54
+ #
55
+ # /admin/image_holders/unrelate/1?resource=Bird&resource_id=1
56
+ # /admin/image_holders/unrelate/2?resource=Bird&resource_id=1
57
+ # ...
58
+ ##
59
+
60
+ setup do
61
+ @image_holder = Factory(:image_holder)
62
+ @bird = Factory(:bird)
63
+ @bird.image_holders << @image_holder
64
+ @request.env['HTTP_REFERER'] = "/admin/birds/edit/#{@bird.id}"
65
+ end
66
+
67
+ should "work" do
68
+ assert_difference('@bird.image_holders.count', -1) do
69
+ post :unrelate, { :id => @image_holder.id, :resource => "Bird", :resource_id => @bird.id }
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,78 @@
1
+ require "test_helper"
2
+
3
+ =begin
4
+
5
+ What's being tested here?
6
+
7
+ - Invoice belongs_to Order
8
+
9
+ =end
10
+
11
+ class Admin::InvoicesControllerTest < ActionController::TestCase
12
+
13
+ setup do
14
+ @typus_user = Factory(:typus_user)
15
+ @request.session[:typus_user_id] = @typus_user.id
16
+ end
17
+
18
+ ##
19
+ # Here we are using the create_with_back_to method.
20
+ #
21
+ # /admin/invoices/new?back_to=&order_id=4&resource=Order&resource_id=4
22
+ #
23
+ context "Create an invoice from an Order" do
24
+
25
+ setup do
26
+ @order = Factory(:order)
27
+ @invoice = { :number => "Invoice#0000001" }
28
+ end
29
+
30
+ should "initialize the object with the params passed on the url" do
31
+ get :new, { :resource => "Order", :resource => "Order", :order_id => @order.id }
32
+ assert assigns(:item)
33
+ assert_equal @order.id, assigns(:item).order_id
34
+ end
35
+
36
+ # TODO: Take a look at this to refactor the create method.
37
+ should "create new invoice assign it to order and redirect to order" do
38
+ @invoice = { :number => "Invoice#0000001", :order_id => @order.id }
39
+
40
+ assert_difference('Invoice.count') do
41
+ post :create, { :invoice => @invoice, :resource => "Order", :order_id => @order.id }
42
+ end
43
+ assert_response :redirect
44
+ assert_redirected_to "/admin/orders/edit/#{@order.id}"
45
+ end
46
+
47
+ should_eventually "raise an error if we try to add an invoice to an order which already has an invoice" do
48
+ @invoice = Factory(:invoice)
49
+ get :new, { :resource => "Order", :resource_id => @invoice.order.id, :order_id => @invoice.order.id }
50
+ assert_response :unprocessable_entity
51
+ end
52
+
53
+ should_eventually "raise an error if we try to create an invoice to an order which already has an invoice" do
54
+ @invoice = Factory(:invoice)
55
+ post :create, { :invoice => { :number => "Invoice#0000001" },
56
+ :resource => "Order", :resource_id => @invoice.order.id, :order_id => @invoice.order.id }
57
+ assert_response :unprocessable_entity
58
+ end
59
+
60
+ end
61
+
62
+ context "Unrelate" do
63
+
64
+ setup do
65
+ @invoice = Factory(:invoice)
66
+ @order = @invoice.order
67
+ @request.env['HTTP_REFERER'] = "/admin/orders/edit/#{@order.id}"
68
+ end
69
+
70
+ should "work for unrelate Invoice from Order" do
71
+ post :unrelate, { :id => @invoice.id, :resource => 'Order', :resource_id => @order.id }
72
+ assert @order.reload.invoice.nil?
73
+ assert_equal "Order successfully updated.", flash[:notice]
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,18 @@
1
+ require "test_helper"
2
+
3
+ =begin
4
+
5
+ What's being tested here?
6
+
7
+ - Order has_one Invoice
8
+
9
+ =end
10
+
11
+ class Admin::OrdersControllerTest < ActionController::TestCase
12
+
13
+ setup do
14
+ @typus_user = Factory(:typus_user)
15
+ @request.session[:typus_user_id] = @typus_user.id
16
+ end
17
+
18
+ end
@@ -0,0 +1,37 @@
1
+ require "test_helper"
2
+
3
+ =begin
4
+
5
+ What's being tested here?
6
+
7
+ - Scopes
8
+
9
+ =end
10
+
11
+ class Admin::PagesControllerTest < ActionController::TestCase
12
+
13
+ setup do
14
+ @typus_user = Factory(:typus_user)
15
+ @request.session[:typus_user_id] = @typus_user.id
16
+ end
17
+
18
+ teardown do
19
+ Page.delete_all
20
+ TypusUser.delete_all
21
+ end
22
+
23
+ context "index" do
24
+
25
+ setup do
26
+ Factory(:page)
27
+ Factory(:page, :status => false)
28
+ end
29
+
30
+ should "return unscoped results" do
31
+ assert Page.count.eql?(1)
32
+ get :index
33
+ assert assigns(:items).size.eql?(2)
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,745 @@
1
+ require "test_helper"
2
+
3
+ =begin
4
+
5
+ What's being tested here?
6
+
7
+ - CRUD: Create, read, update, destroy
8
+ - CRUD Extras: toggle
9
+ - Filters
10
+ - Forms
11
+ - Overrides
12
+ - Permissions
13
+ - Roles
14
+ - Views
15
+
16
+ =end
17
+
18
+ class Admin::PostsControllerTest < ActionController::TestCase
19
+
20
+ setup do
21
+ @typus_user = Factory(:typus_user)
22
+ @request.session[:typus_user_id] = @typus_user.id
23
+ @post = Factory(:post)
24
+ end
25
+
26
+ teardown do
27
+ Post.delete_all
28
+ TypusUser.delete_all
29
+ end
30
+
31
+ ##############################################################################
32
+ #
33
+ ##############################################################################
34
+
35
+ context "CRUD" do
36
+
37
+ should "render index" do
38
+ get :index
39
+ assert_response :success
40
+ assert_template 'index'
41
+ end
42
+
43
+ context "new" do
44
+
45
+ should "render" do
46
+ get :new
47
+ assert_response :success
48
+ assert_template 'new'
49
+ end
50
+
51
+ should "reject params which are not included in @resource.columns.map(&:name)" do
52
+ %w(chunky_bacon).each do |param|
53
+ get :new, {param => param}
54
+ assert_response :success
55
+ assert_template 'new'
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ should "create" do
62
+ assert_difference('Post.count') do
63
+ post :create, { :post => { :title => 'This is another title', :body => 'Body' } }
64
+ assert_response :redirect
65
+ assert_redirected_to "/admin/posts/edit/#{Post.last.id}"
66
+ end
67
+ end
68
+
69
+ should "render show" do
70
+ get :show, { :id => @post.id }
71
+ assert_response :success
72
+ assert_template 'show'
73
+ end
74
+
75
+ should "render edit" do
76
+ get :edit, { :id => @post.id }
77
+ assert_response :success
78
+ assert_template 'edit'
79
+ end
80
+
81
+ should "update" do
82
+ post :update, { :id => @post.id, :title => 'Updated' }
83
+ assert_response :redirect
84
+ assert_redirected_to "/admin/posts/edit/#{@post.id}"
85
+ end
86
+
87
+ end
88
+
89
+ context "CRUD extras" do
90
+
91
+ setup do
92
+ @request.env['HTTP_REFERER'] = "/admin/posts"
93
+ end
94
+
95
+ context "toggle" do
96
+
97
+ should "work" do
98
+ assert !@post.published
99
+ get :toggle, { :id => @post.id, :field => "published" }
100
+
101
+ assert_response :redirect
102
+ assert_redirected_to @request.env["HTTP_REFERER"]
103
+ assert_equal "Post successfully updated.", flash[:notice]
104
+ assert @post.reload.published
105
+ end
106
+
107
+ should "render edit post when validation fails" do
108
+ @post.body = nil
109
+ @post.save(:validate => false)
110
+ get :toggle, { :id => @post.id, :field => "published" }
111
+ assert_response :success
112
+ assert_template "admin/resources/edit"
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ context "Filters" do
120
+
121
+ should "render index with accepted params" do
122
+ @post.update_attributes(:published => true)
123
+ get :index, { :published => 'true' }
124
+ assert_response :success
125
+ assert_template 'index'
126
+ assert assigns(:items).size.eql?(1)
127
+
128
+ get :index, { :published => 'false' }
129
+ assert assigns(:items).size.eql?(0)
130
+ end
131
+
132
+ should "render index with accepted params - search" do
133
+ @post.update_attributes(:title => "neinonon")
134
+ get :index, { :search => 'neinonon' }
135
+ assert_response :success
136
+ assert_template 'index'
137
+ assert assigns(:items).size.eql?(1)
138
+
139
+ get :index, { :search => 'unexisting' }
140
+ assert assigns(:items).size.eql?(0)
141
+ end
142
+
143
+ should "render index with non-accepted params" do
144
+ get :index, { :non_accepted_param => 'non_accepted_param' }
145
+ assert_response :success
146
+ assert_template 'index'
147
+ end
148
+
149
+ end
150
+
151
+ context "Filters" do
152
+
153
+ should "be included in index" do
154
+ get :index
155
+ expected = [["All", "index", "unscoped"]]
156
+ assert_equal expected, assigns(:predefined_filters)
157
+ end
158
+
159
+ end
160
+
161
+ context "Actions" do
162
+
163
+ should "be edit and trash on index" do
164
+ get :index
165
+
166
+ expected = [["Edit", {"action"=>"edit"}, {}],
167
+ ["Trash", {"action"=>"destroy"}, {"method"=>"delete", "confirm"=>"Trash?"}]]
168
+
169
+ assert_equal expected, assigns(:resource_actions)
170
+ end
171
+
172
+ context "with overriden default action on item" do
173
+
174
+ setup do
175
+ Typus::Resources.expects(:default_action_on_item).at_least_once.returns('show')
176
+ end
177
+
178
+ should "be show and trash on index" do
179
+ get :index
180
+
181
+ expected = [["Show", {"action"=>"show"}, {}],
182
+ ["Trash", {"action"=>"destroy"}, {"method"=>"delete", "confirm"=>"Trash?"}]]
183
+
184
+ assert_equal expected, assigns(:resource_actions)
185
+ end
186
+
187
+ end
188
+
189
+ end
190
+
191
+ context "Forms" do
192
+
193
+ setup do
194
+ get :new
195
+ end
196
+
197
+ should "verify forms" do
198
+ assert_select "form"
199
+ end
200
+
201
+ # We have 3 inputs: 1 hidden which is the UTF8 stuff, one which is the
202
+ # Post#title and finally the submit button.
203
+ should "have 3 inputs" do
204
+ assert_select "form input", 3
205
+
206
+ # Post#title: Input
207
+ assert_select 'label[for="post_title"]'
208
+ assert_select 'input#post_title[type="text"]'
209
+ end
210
+
211
+ should "have 1 textarea" do
212
+ assert_select "form textarea", 1
213
+
214
+ # Post#body: Text Area
215
+ assert_select 'label[for="post_body"]'
216
+ assert_select 'textarea#post_body'
217
+ end
218
+
219
+ should "have 6 selectors" do
220
+ assert_select "form select", 6
221
+
222
+ # Post#created_at: Datetime
223
+ assert_select 'label[for="post_created_at"]'
224
+ assert_select 'select#post_created_at_1i'
225
+ assert_select 'select#post_created_at_2i'
226
+ assert_select 'select#post_created_at_3i'
227
+ assert_select 'select#post_created_at_4i'
228
+ assert_select 'select#post_created_at_5i'
229
+
230
+ # Post#status: Selector
231
+ assert_select 'label[for="post_status"]'
232
+ assert_select 'select#post_status'
233
+ end
234
+
235
+ should "have 1 template" do
236
+ assert_match "templates#datepicker_template_published_at", @response.body
237
+ end
238
+
239
+ end
240
+
241
+ context "Overwrite action_after_save" do
242
+
243
+ setup do
244
+ Typus::Resources.expects(:action_after_save).returns("index")
245
+ @post = Factory(:post)
246
+ end
247
+
248
+ should "create an item and redirect to index" do
249
+ assert_difference('Post.count') do
250
+ post :create, { :post => { :title => 'This is another title', :body => 'Body' } }
251
+ assert_response :redirect
252
+ assert_redirected_to :action => 'index'
253
+ end
254
+ end
255
+
256
+ should "update an item and redirect to index" do
257
+ post :update, { :id => @post.id, :title => 'Updated' }
258
+ assert_response :redirect
259
+ assert_redirected_to :action => 'index'
260
+ end
261
+
262
+ end
263
+
264
+ context "Formats" do
265
+
266
+ should "render index and return xml" do
267
+ get :index, :format => "xml"
268
+
269
+ assert_response :success
270
+
271
+ assert_match %Q[<?xml version="1.0" encoding="UTF-8"?>], @response.body
272
+ assert_match %Q[<posts type="array">], @response.body
273
+ assert_match "<status>#{@post.status}</status>", @response.body
274
+ assert_match "<title>#{@post.title}</title>", @response.body
275
+ end
276
+
277
+ should "render index and return csv" do
278
+ expected = <<-RAW
279
+ title;status
280
+ #{@post.title};#{@post.status}
281
+ RAW
282
+
283
+ get :index, :format => "csv"
284
+
285
+ assert_response :success
286
+ assert_equal expected, @response.body
287
+ end
288
+
289
+ should "not instantiate resource_actions" do
290
+ get :index, :format => "xml"
291
+ assert assigns(:resource_actions).nil?
292
+ end
293
+
294
+ should "not instantiate predefined_filters" do
295
+ get :index, :format => "xml"
296
+ assert assigns(:predefined_filters).nil?
297
+ end
298
+
299
+ end
300
+
301
+ context "Permissions" do
302
+
303
+ context "Root" do
304
+
305
+ setup do
306
+ @editor = Factory(:typus_user, :email => "editor@example.com", :role => "editor")
307
+ @post = Factory(:post, :typus_user => @editor)
308
+ end
309
+
310
+ should "should list all posts no matter who is the owner" do
311
+ Post.delete_all
312
+ admin = TypusUser.where(:role => 'admin').first
313
+ 2.times { Factory(:post, :typus_user => @editor) }
314
+ 2.times { Factory(:post, :typus_user => @typus_user) }
315
+ Typus::Resources.expects(:only_user_items).returns(true)
316
+
317
+ get :index
318
+
319
+ assert_equal 4, Post.count
320
+ assert_equal 4, assigns(:items).size
321
+ assert_equal [@editor.id, @editor.id, @typus_user.id, @typus_user.id], assigns(:items).map(&:typus_user_id)
322
+ end
323
+
324
+ should "be able to edit any record" do
325
+ Post.all.each do |post|
326
+ get :edit, { :id => post.id }
327
+ assert_response :success
328
+ assert_template 'edit'
329
+ end
330
+ end
331
+
332
+ should "verify_admin_updating_an_item_does_not_change_typus_user_id_if_not_defined" do
333
+ _post = @post
334
+ post :update, { :id => @post.id, :post => { :title => 'Updated by admin' } }
335
+ assert_equal _post.typus_user_id, @post.reload.typus_user_id
336
+ end
337
+
338
+ should "verify_admin_updating_an_item_does_change_typus_user_id_to_whatever_admin_wants" do
339
+ post :update, { :id => @post.id, :post => { :title => 'Updated', :typus_user_id => 108 } }
340
+ assert_equal 108, @post.reload.typus_user_id
341
+ end
342
+
343
+ end
344
+
345
+ context "No root" do
346
+
347
+ setup do
348
+ @typus_user = Factory(:typus_user, :email => "editor@example.com", :role => "editor")
349
+ @request.session[:typus_user_id] = @typus_user.id
350
+ @request.env['HTTP_REFERER'] = '/admin/posts'
351
+ end
352
+
353
+ should "not be root" do
354
+ assert @typus_user.is_not_root?
355
+ end
356
+
357
+ should "verify_editor_can_show_any_record" do
358
+ Post.all.each do |post|
359
+ get :show, { :id => post.id }
360
+ assert_response :success
361
+ assert_template 'show'
362
+ end
363
+ end
364
+
365
+ should "verify_editor_tried_to_edit_a_post_owned_by_himself" do
366
+ post_ = Factory(:post, :typus_user => @typus_user)
367
+ get :edit, { :id => post_.id }
368
+ assert_response :success
369
+ end
370
+
371
+ should "verify_editor_tries_to_edit_a_post_owned_by_the_admin" do
372
+ get :edit, { :id => Factory(:post).id }
373
+ assert_response :unprocessable_entity
374
+ end
375
+
376
+ should "verify_editor_tries_to_show_a_post_owned_by_the_admin" do
377
+ get :show, { :id => Factory(:post).id }
378
+ assert_response :success
379
+ end
380
+
381
+ should "only list editor posts" do
382
+ Post.delete_all
383
+ admin = TypusUser.where(:role => 'admin').first
384
+ 2.times { Factory(:post, :typus_user => admin) }
385
+ 2.times { Factory(:post, :typus_user => @typus_user) }
386
+ Typus::Resources.expects(:only_user_items).returns(true)
387
+
388
+ get :index
389
+
390
+ assert_equal 4, Post.count
391
+ assert_equal 2, assigns(:items).size
392
+ assert_equal [@typus_user.id, @typus_user.id], assigns(:items).map(&:typus_user_id)
393
+ end
394
+
395
+ should "verify_editor_tries_to_show_a_post_owned_by_the_admin when only user items" do
396
+ Typus::Resources.expects(:only_user_items).returns(true)
397
+ post = Factory(:post)
398
+ get :show, { :id => post.id }
399
+ assert_response :unprocessable_entity
400
+ end
401
+
402
+ should "verify_typus_user_id_of_item_when_creating_record" do
403
+ post :create, { :post => { :title => "Chunky Bacon", :body => "Lorem ipsum ..." } }
404
+ post_ = Post.find_by_title("Chunky Bacon")
405
+
406
+ assert_equal @request.session[:typus_user_id], post_.typus_user_id
407
+ end
408
+
409
+ should "verify_editor_updating_an_item_does_not_change_typus_user_id" do
410
+ [ 108, nil ].each do |typus_user_id|
411
+ post_ = Factory(:post, :typus_user => @typus_user)
412
+ post :update, { :id => post_.id, :post => { :title => 'Updated', :typus_user_id => @typus_user.id } }
413
+ post_updated = Post.find(post_.id)
414
+ assert_equal @request.session[:typus_user_id], post_updated.typus_user_id
415
+ end
416
+ end
417
+
418
+ end
419
+
420
+ end
421
+
422
+ context "Roles" do
423
+
424
+ setup do
425
+ @request.env['HTTP_REFERER'] = '/admin/posts'
426
+ end
427
+
428
+ context "Admin" do
429
+
430
+ should "be able to add posts" do
431
+ assert @typus_user.can?("create", "Post")
432
+ end
433
+
434
+ should "be able to destroy posts" do
435
+ get :destroy, { :id => Factory(:post).id, :method => :delete }
436
+
437
+ assert_response :redirect
438
+ assert_equal "Post successfully removed.", flash[:notice]
439
+ assert_redirected_to :action => :index
440
+ end
441
+
442
+ end
443
+
444
+ context "Designer" do
445
+
446
+ setup do
447
+ Typus.user_class.delete_all
448
+ @designer = Factory(:typus_user, :role => "designer")
449
+ @request.session[:typus_user_id] = @designer.id
450
+ @post = Factory(:post)
451
+ end
452
+
453
+ should "not be able to add posts" do
454
+ get :new
455
+
456
+ assert_response :unprocessable_entity
457
+ end
458
+
459
+ should "not be able to destroy posts" do
460
+ assert_no_difference('Post.count') do
461
+ get :destroy, { :id => @post.id, :method => :delete }
462
+ end
463
+ assert_response :unprocessable_entity
464
+ end
465
+
466
+ end
467
+
468
+ end
469
+
470
+ context "Relationships (relate)" do
471
+
472
+ setup do
473
+ @post = Factory(:post)
474
+ end
475
+
476
+ should "relate comment to post (has_many)" do
477
+ comment = Factory(:comment, :post => nil)
478
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{@post.id}#comments"
479
+
480
+ assert_difference('@post.comments.count') do
481
+ post :relate, { :id => @post.id, :related => { :model => 'Comment', :id => comment.id, :association_name => 'comments' } }
482
+ end
483
+
484
+ assert_response :redirect
485
+ assert_redirected_to @request.env['HTTP_REFERER']
486
+ assert_equal "Post successfully updated.", flash[:notice]
487
+ end
488
+
489
+ should "relate category to post (has_and_belongs_to_many)" do
490
+ category = Factory(:category)
491
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{@post.id}#categories"
492
+
493
+ assert_difference('category.posts.count') do
494
+ post :relate, { :id => @post.id, :related => { :model => 'Category', :id => category.id, :association_name => 'categories' } }
495
+ end
496
+
497
+ assert_response :redirect
498
+ assert_redirected_to @request.env['HTTP_REFERER']
499
+ assert_equal "Post successfully updated.", flash[:notice]
500
+ end
501
+
502
+ end
503
+
504
+ context "Views" do
505
+
506
+ context "Index" do
507
+
508
+ setup do
509
+ get :index
510
+ end
511
+
512
+ should "render index and validates_presence_of_custom_partials" do
513
+ assert_match "posts#_index.html.erb", @response.body
514
+ end
515
+
516
+ should "render_index_and_verify_page_title" do
517
+ assert_select "title", "Typus &mdash; Posts"
518
+ end
519
+
520
+ should "render index_and_show_add_entry_link" do
521
+ assert_select "#sidebar ul" do
522
+ assert_select "li", "Add new"
523
+ end
524
+ end
525
+
526
+ end
527
+
528
+ context "New" do
529
+
530
+ setup do
531
+ get :new
532
+ end
533
+
534
+ should "render new and partials_on_new" do
535
+ assert_match "posts#_new.html.erb", @response.body
536
+ end
537
+
538
+ should "render new and verify page title" do
539
+ assert_select "title", "Typus &mdash; New Post"
540
+ end
541
+
542
+ end
543
+
544
+ context "Edit" do
545
+
546
+ setup do
547
+ get :edit, { :id => Factory(:post).id }
548
+ end
549
+
550
+ should "render_edit_and_verify_presence_of_custom_partials" do
551
+ assert_match "posts#_edit.html.erb", @response.body
552
+ end
553
+
554
+ should "render_edit_and_verify_page_title" do
555
+ assert_select "title", "Typus &mdash; Edit Post"
556
+ end
557
+
558
+ end
559
+
560
+ context "Show" do
561
+
562
+ setup do
563
+ get :show, { :id => Factory(:post).id }
564
+ end
565
+
566
+ should "render_show_and_verify_presence_of_custom_partials" do
567
+ assert_match "posts#_show.html.erb", @response.body
568
+ end
569
+
570
+ should "render show and verify page title" do
571
+ assert_select "title", "Typus &mdash; Show Post"
572
+ end
573
+
574
+ end
575
+
576
+ should "get_index_and_render_edit_or_show_links" do
577
+ %w(edit show).each do |action|
578
+ Typus::Resources.expects(:default_action_on_item).at_least_once.returns(action)
579
+ get :index
580
+ Post.all.each do |post|
581
+ assert_match "/posts/#{action}/#{post.id}", @response.body
582
+ end
583
+ end
584
+ end
585
+
586
+ context "Designer" do
587
+
588
+ setup do
589
+ @typus_user = Factory(:typus_user, :email => "designer@example.com", :role => "designer")
590
+ @request.session[:typus_user_id] = @typus_user.id
591
+ end
592
+
593
+ should "render_index_and_not_show_add_entry_link" do
594
+ get :index
595
+ assert_response :success
596
+ assert_no_match /Add Post/, @response.body
597
+ end
598
+
599
+ end
600
+
601
+ context "Editor" do
602
+
603
+ setup do
604
+ @typus_user = Factory(:typus_user, :email => "editor@example.com", :role => "editor")
605
+ @request.session[:typus_user_id] = @typus_user.id
606
+ end
607
+
608
+ =begin
609
+
610
+ ##
611
+ # This feature is no longer available. I've to decide if I want to take
612
+ # it back.
613
+ #
614
+ should "get_index_and_render_edit_or_show_links_on_owned_records" do
615
+ get :index
616
+ Post.all.each do |post|
617
+ action = post.owned_by?(@typus_user) ? "edit" : "show"
618
+ assert_match "/posts/#{action}/#{post.id}", @response.body
619
+ end
620
+ end
621
+
622
+ =end
623
+
624
+ should "get_index_and_render_edit_or_show_on_only_user_items" do
625
+ %w(edit show).each do |action|
626
+ Typus::Resources.stubs(:only_user_items).returns(true)
627
+ Typus::Resources.stubs(:default_action_on_item).returns(action)
628
+ get :index
629
+ Post.all.each do |post|
630
+ if post.owned_by?(@typus_user)
631
+ assert_match "/posts/#{action}/#{post.id}", @response.body
632
+ else
633
+ assert_no_match /\/posts\/#{action}\/#{post.id}/, @response.body
634
+ end
635
+ end
636
+ end
637
+ end
638
+
639
+ end
640
+
641
+ end
642
+
643
+ ##
644
+ # We are in a View and we want to create a new Post from there to be able
645
+ # to assign it. There are two cases:
646
+ #
647
+ # - We are creating the view.
648
+ # - We are editing the view.
649
+ #
650
+ context "create_with_back_to" do
651
+
652
+ setup do
653
+ @post = { :title => 'This is another title', :body => 'Body' }
654
+ end
655
+
656
+ context "when creating an item" do
657
+
658
+ ##
659
+ # We click on the "Add new" link and we are redirected to:
660
+ #
661
+ # /admin/posts/new
662
+ #
663
+ # With a collection of params which will be used to create the
664
+ # association if everything works as expected.
665
+ #
666
+ # Once the association is created we are redirected back to where we
667
+ # started with a param which selects the Post on the View form.
668
+ #
669
+ # /admin/views/new?post_id=1
670
+ #
671
+ # So we end up having a new Post and if we save the form will be
672
+ # assigned to the view.
673
+ #
674
+
675
+ should "create new post and redirect to view" do
676
+ assert_difference('Post.count') do
677
+ post :create, { :post => @post,
678
+ :resource => "View" }
679
+ end
680
+ assert_response :redirect
681
+ assert_redirected_to "/admin/views/new?post_id=#{Post.last.id}"
682
+ end
683
+
684
+ end
685
+
686
+ context "when editing an item" do
687
+
688
+ ##
689
+ # We click on the "Add new" link and we are redirected to:
690
+ #
691
+ # /admin/posts/new
692
+ #
693
+ # The important thing here is that we are passing the `resource_id`
694
+ # because we will assign the newly created Post to the View.
695
+ #
696
+ # So we will end up having a new Post assigned to the View.
697
+ #
698
+
699
+ setup do
700
+ @view = Factory(:view, :post => nil)
701
+ end
702
+
703
+ should "create new post and redirect to view" do
704
+ assert_difference('Post.count') do
705
+ post :create, { :post => @post, :resource => "View", :resource_id => @view.id }
706
+ end
707
+ assert_response :redirect
708
+ assert_redirected_to "/admin/views/edit/#{@view.id}"
709
+
710
+ # Make sure the association is created!
711
+ assert @view.reload.post
712
+ end
713
+
714
+ end
715
+
716
+ end
717
+
718
+ context "autocomplete" do
719
+
720
+ setup do
721
+ 25.times { Factory(:post) }
722
+ end
723
+
724
+ should "work and return a json hash with ten items" do
725
+ get :autocomplete, { :term => "Post" }
726
+ assert_response :success
727
+ assert_equal 20, assigns(:items).size
728
+ end
729
+
730
+ should "work and return json hash with one item" do
731
+ post = Post.first
732
+ post.update_attributes(:title => "fesplugas")
733
+
734
+ get :autocomplete, { :term => "jmeiss" }
735
+ assert_response :success
736
+ assert_equal 0, assigns(:items).size
737
+
738
+ get :autocomplete, { :term => "fesplugas" }
739
+ assert_response :success
740
+ assert_equal 1, assigns(:items).size
741
+ end
742
+
743
+ end
744
+
745
+ end