resources_controller 1.0.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 (249) hide show
  1. data/LICENSE +22 -0
  2. data/README.rdoc +330 -0
  3. data/TODO +0 -0
  4. data/VERSION.yml +4 -0
  5. data/generators/scaffold_resource/USAGE +29 -0
  6. data/generators/scaffold_resource/scaffold_resource_generator.rb +179 -0
  7. data/generators/scaffold_resource/templates/controller.rb +2 -0
  8. data/generators/scaffold_resource/templates/fixtures.yml +10 -0
  9. data/generators/scaffold_resource/templates/functional_test.rb +57 -0
  10. data/generators/scaffold_resource/templates/helper.rb +2 -0
  11. data/generators/scaffold_resource/templates/migration.rb +15 -0
  12. data/generators/scaffold_resource/templates/model.rb +2 -0
  13. data/generators/scaffold_resource/templates/old_migration.rb +13 -0
  14. data/generators/scaffold_resource/templates/rspec/functional_spec.rb +255 -0
  15. data/generators/scaffold_resource/templates/rspec/helper_spec.rb +11 -0
  16. data/generators/scaffold_resource/templates/rspec/routing_spec.rb +61 -0
  17. data/generators/scaffold_resource/templates/rspec/unit_spec.rb +11 -0
  18. data/generators/scaffold_resource/templates/rspec/views/edit_spec.rb +28 -0
  19. data/generators/scaffold_resource/templates/rspec/views/index_spec.rb +26 -0
  20. data/generators/scaffold_resource/templates/rspec/views/new_spec.rb +30 -0
  21. data/generators/scaffold_resource/templates/rspec/views/show_spec.rb +25 -0
  22. data/generators/scaffold_resource/templates/shoulda_functional_test.rb +19 -0
  23. data/generators/scaffold_resource/templates/unit_test.rb +7 -0
  24. data/generators/scaffold_resource/templates/view__form.erb +6 -0
  25. data/generators/scaffold_resource/templates/view__form.haml +5 -0
  26. data/generators/scaffold_resource/templates/view_edit.erb +16 -0
  27. data/generators/scaffold_resource/templates/view_edit.haml +11 -0
  28. data/generators/scaffold_resource/templates/view_index.erb +22 -0
  29. data/generators/scaffold_resource/templates/view_index.haml +19 -0
  30. data/generators/scaffold_resource/templates/view_new.erb +12 -0
  31. data/generators/scaffold_resource/templates/view_new.haml +9 -0
  32. data/generators/scaffold_resource/templates/view_show.erb +9 -0
  33. data/generators/scaffold_resource/templates/view_show.haml +9 -0
  34. data/lib/resource_controller/accessors.rb +77 -0
  35. data/lib/resource_controller/action_options.rb +40 -0
  36. data/lib/resource_controller/actions.rb +75 -0
  37. data/lib/resource_controller/base.rb +15 -0
  38. data/lib/resource_controller/controller.rb +69 -0
  39. data/lib/resource_controller/failable_action_options.rb +25 -0
  40. data/lib/resource_controller/helpers/current_objects.rb +82 -0
  41. data/lib/resource_controller/helpers/internal.rb +80 -0
  42. data/lib/resource_controller/helpers/nested.rb +82 -0
  43. data/lib/resource_controller/helpers/singleton_customizations.rb +64 -0
  44. data/lib/resource_controller/helpers/urls.rb +132 -0
  45. data/lib/resource_controller/helpers.rb +34 -0
  46. data/lib/resource_controller/response_collector.rb +27 -0
  47. data/lib/resource_controller/singleton.rb +15 -0
  48. data/lib/resource_controller.rb +54 -0
  49. data/lib/urligence.rb +52 -0
  50. data/rails/init.rb +6 -0
  51. data/test/Gemfile +11 -0
  52. data/test/Rakefile +10 -0
  53. data/test/app/controllers/accounts_controller.rb +6 -0
  54. data/test/app/controllers/application_controller.rb +5 -0
  55. data/test/app/controllers/cms/options_controller.rb +3 -0
  56. data/test/app/controllers/cms/personnel_controller.rb +2 -0
  57. data/test/app/controllers/cms/photos_controller.rb +6 -0
  58. data/test/app/controllers/cms/products_controller.rb +3 -0
  59. data/test/app/controllers/comments_controller.rb +3 -0
  60. data/test/app/controllers/images_controller.rb +4 -0
  61. data/test/app/controllers/options_controller.rb +8 -0
  62. data/test/app/controllers/people_controller.rb +9 -0
  63. data/test/app/controllers/photos_controller.rb +12 -0
  64. data/test/app/controllers/posts_controller.rb +10 -0
  65. data/test/app/controllers/projects_controller.rb +3 -0
  66. data/test/app/controllers/somethings_controller.rb +3 -0
  67. data/test/app/controllers/tags_controller.rb +13 -0
  68. data/test/app/controllers/users_controller.rb +12 -0
  69. data/test/app/helpers/accounts_helper.rb +2 -0
  70. data/test/app/helpers/application_helper.rb +3 -0
  71. data/test/app/helpers/cms/products_helper.rb +2 -0
  72. data/test/app/helpers/comments_helper.rb +2 -0
  73. data/test/app/helpers/images_helper.rb +2 -0
  74. data/test/app/helpers/options_helper.rb +2 -0
  75. data/test/app/helpers/people_helper.rb +2 -0
  76. data/test/app/helpers/photos_helper.rb +2 -0
  77. data/test/app/helpers/posts_helper.rb +2 -0
  78. data/test/app/helpers/projects_helper.rb +2 -0
  79. data/test/app/helpers/somethings_helper.rb +2 -0
  80. data/test/app/helpers/tags_helper.rb +2 -0
  81. data/test/app/helpers/users_helper.rb +2 -0
  82. data/test/app/models/account.rb +4 -0
  83. data/test/app/models/comment.rb +3 -0
  84. data/test/app/models/image.rb +3 -0
  85. data/test/app/models/option.rb +3 -0
  86. data/test/app/models/personnel.rb +3 -0
  87. data/test/app/models/photo.rb +5 -0
  88. data/test/app/models/post.rb +3 -0
  89. data/test/app/models/product.rb +3 -0
  90. data/test/app/models/project.rb +2 -0
  91. data/test/app/models/something.rb +2 -0
  92. data/test/app/models/tag.rb +3 -0
  93. data/test/app/models/user.rb +3 -0
  94. data/test/app/views/accounts/_form.html.erb +4 -0
  95. data/test/app/views/accounts/edit.html.erb +14 -0
  96. data/test/app/views/accounts/new.html.erb +12 -0
  97. data/test/app/views/accounts/show.html.erb +5 -0
  98. data/test/app/views/cms/options/edit.rhtml +17 -0
  99. data/test/app/views/cms/options/index.rhtml +20 -0
  100. data/test/app/views/cms/options/new.rhtml +16 -0
  101. data/test/app/views/cms/options/show.rhtml +8 -0
  102. data/test/app/views/cms/photos/edit.rhtml +17 -0
  103. data/test/app/views/cms/photos/index.rhtml +20 -0
  104. data/test/app/views/cms/photos/new.rhtml +16 -0
  105. data/test/app/views/cms/photos/show.rhtml +8 -0
  106. data/test/app/views/cms/products/edit.rhtml +17 -0
  107. data/test/app/views/cms/products/index.rhtml +20 -0
  108. data/test/app/views/cms/products/new.rhtml +16 -0
  109. data/test/app/views/cms/products/show.rhtml +8 -0
  110. data/test/app/views/comments/edit.rhtml +27 -0
  111. data/test/app/views/comments/index.rhtml +24 -0
  112. data/test/app/views/comments/new.rhtml +26 -0
  113. data/test/app/views/comments/show.rhtml +18 -0
  114. data/test/app/views/images/_form.html.erb +4 -0
  115. data/test/app/views/images/edit.html.erb +14 -0
  116. data/test/app/views/images/new.html.erb +12 -0
  117. data/test/app/views/layouts/application.rhtml +17 -0
  118. data/test/app/views/layouts/comments.rhtml +17 -0
  119. data/test/app/views/layouts/options.rhtml +17 -0
  120. data/test/app/views/layouts/people.rhtml +17 -0
  121. data/test/app/views/layouts/photos.rhtml +17 -0
  122. data/test/app/views/layouts/projects.rhtml +17 -0
  123. data/test/app/views/layouts/somethings.rhtml +17 -0
  124. data/test/app/views/layouts/tags.rhtml +17 -0
  125. data/test/app/views/options/_form.html.erb +8 -0
  126. data/test/app/views/options/edit.html.erb +16 -0
  127. data/test/app/views/options/index.html.erb +21 -0
  128. data/test/app/views/options/new.html.erb +12 -0
  129. data/test/app/views/options/show.html.erb +10 -0
  130. data/test/app/views/people/edit.rhtml +17 -0
  131. data/test/app/views/people/index.rhtml +20 -0
  132. data/test/app/views/people/new.rhtml +16 -0
  133. data/test/app/views/people/show.rhtml +8 -0
  134. data/test/app/views/photos/edit.rhtml +17 -0
  135. data/test/app/views/photos/index.rhtml +20 -0
  136. data/test/app/views/photos/new.rhtml +16 -0
  137. data/test/app/views/photos/show.rhtml +8 -0
  138. data/test/app/views/posts/edit.rhtml +22 -0
  139. data/test/app/views/posts/index.rhtml +22 -0
  140. data/test/app/views/posts/new.rhtml +21 -0
  141. data/test/app/views/posts/show.rhtml +13 -0
  142. data/test/app/views/projects/edit.rhtml +17 -0
  143. data/test/app/views/projects/index.rhtml +20 -0
  144. data/test/app/views/projects/new.rhtml +16 -0
  145. data/test/app/views/projects/show.rhtml +8 -0
  146. data/test/app/views/somethings/edit.rhtml +17 -0
  147. data/test/app/views/somethings/index.rhtml +20 -0
  148. data/test/app/views/somethings/new.rhtml +16 -0
  149. data/test/app/views/somethings/show.rhtml +8 -0
  150. data/test/app/views/tags/edit.rhtml +17 -0
  151. data/test/app/views/tags/index.rhtml +20 -0
  152. data/test/app/views/tags/index.rjs +0 -0
  153. data/test/app/views/tags/new.rhtml +16 -0
  154. data/test/app/views/tags/show.rhtml +8 -0
  155. data/test/app/views/users/edit.rhtml +17 -0
  156. data/test/app/views/users/index.rhtml +20 -0
  157. data/test/app/views/users/new.rhtml +16 -0
  158. data/test/app/views/users/show.rhtml +8 -0
  159. data/test/config/application.rb +41 -0
  160. data/test/config/boot.rb +6 -0
  161. data/test/config/database.yml +9 -0
  162. data/test/config/environment.rb +5 -0
  163. data/test/config/environments/development.rb +19 -0
  164. data/test/config/environments/test.rb +32 -0
  165. data/test/config/initializers/inflections.rb +14 -0
  166. data/test/config/initializers/secret_token.rb +7 -0
  167. data/test/config/initializers/session_store.rb +8 -0
  168. data/test/config/routes.rb +35 -0
  169. data/test/config.ru +4 -0
  170. data/test/db/migrate/001_create_posts.rb +12 -0
  171. data/test/db/migrate/002_create_products.rb +11 -0
  172. data/test/db/migrate/003_create_comments.rb +13 -0
  173. data/test/db/migrate/004_create_options.rb +13 -0
  174. data/test/db/migrate/005_create_photos.rb +11 -0
  175. data/test/db/migrate/006_create_tags.rb +17 -0
  176. data/test/db/migrate/007_create_somethings.rb +11 -0
  177. data/test/db/migrate/008_create_accounts.rb +11 -0
  178. data/test/db/migrate/009_add_account_id_to_photos.rb +9 -0
  179. data/test/db/migrate/010_create_projects.rb +11 -0
  180. data/test/db/migrate/011_create_images.rb +12 -0
  181. data/test/db/migrate/012_create_users.rb +11 -0
  182. data/test/db/migrate/013_create_personnel.rb +11 -0
  183. data/test/db/migrate/014_add_personnel_id_to_photos.rb +9 -0
  184. data/test/db/schema.rb +78 -0
  185. data/test/script/console +3 -0
  186. data/test/script/destroy +3 -0
  187. data/test/script/generate +3 -0
  188. data/test/script/rails +6 -0
  189. data/test/script/server +3 -0
  190. data/test/test/fixtures/accounts.yml +7 -0
  191. data/test/test/fixtures/comments.yml +11 -0
  192. data/test/test/fixtures/images.yml +6 -0
  193. data/test/test/fixtures/options.yml +9 -0
  194. data/test/test/fixtures/personnel.yml +5 -0
  195. data/test/test/fixtures/photos.yml +9 -0
  196. data/test/test/fixtures/photos_tags.yml +3 -0
  197. data/test/test/fixtures/posts.yml +9 -0
  198. data/test/test/fixtures/products.yml +7 -0
  199. data/test/test/fixtures/projects.yml +7 -0
  200. data/test/test/fixtures/somethings.yml +7 -0
  201. data/test/test/fixtures/tags.yml +7 -0
  202. data/test/test/fixtures/users.yml +5 -0
  203. data/test/test/functional/cms/options_controller_test.rb +140 -0
  204. data/test/test/functional/cms/photos_controller_test.rb +37 -0
  205. data/test/test/functional/cms/products_controller_test.rb +37 -0
  206. data/test/test/functional/comments_controller_test.rb +140 -0
  207. data/test/test/functional/images_controller_test.rb +30 -0
  208. data/test/test/functional/people_controller_test.rb +147 -0
  209. data/test/test/functional/photos_controller_test.rb +199 -0
  210. data/test/test/functional/posts_controller_test.rb +21 -0
  211. data/test/test/functional/projects_controller_test.rb +137 -0
  212. data/test/test/functional/somethings_controller_test.rb +15 -0
  213. data/test/test/functional/tags_controller_test.rb +53 -0
  214. data/test/test/functional/users_controller_test.rb +137 -0
  215. data/test/test/test_helper.rb +14 -0
  216. data/test/test/unit/accessors_test.rb +110 -0
  217. data/test/test/unit/account_test.rb +7 -0
  218. data/test/test/unit/action_options_test.rb +109 -0
  219. data/test/test/unit/base_test.rb +11 -0
  220. data/test/test/unit/comment_test.rb +10 -0
  221. data/test/test/unit/failable_action_options_test.rb +77 -0
  222. data/test/test/unit/helpers/current_objects_test.rb +133 -0
  223. data/test/test/unit/helpers/internal_test.rb +106 -0
  224. data/test/test/unit/helpers/nested_test.rb +86 -0
  225. data/test/test/unit/helpers/singleton_current_objects_test.rb +68 -0
  226. data/test/test/unit/helpers/singleton_nested_test.rb +77 -0
  227. data/test/test/unit/helpers/singleton_urls_test.rb +67 -0
  228. data/test/test/unit/helpers/urls_test.rb +75 -0
  229. data/test/test/unit/helpers_test.rb +25 -0
  230. data/test/test/unit/image_test.rb +7 -0
  231. data/test/test/unit/option_test.rb +10 -0
  232. data/test/test/unit/photo_test.rb +10 -0
  233. data/test/test/unit/post_test.rb +10 -0
  234. data/test/test/unit/project_test.rb +10 -0
  235. data/test/test/unit/response_collector_test.rb +49 -0
  236. data/test/test/unit/something_test.rb +10 -0
  237. data/test/test/unit/tag_test.rb +10 -0
  238. data/test/test/unit/urligence_test.rb +205 -0
  239. data/test/vendor/plugins/d +1 -0
  240. data/test/vendor/plugins/dynamic_form/MIT-LICENSE +20 -0
  241. data/test/vendor/plugins/dynamic_form/README +13 -0
  242. data/test/vendor/plugins/dynamic_form/Rakefile +10 -0
  243. data/test/vendor/plugins/dynamic_form/init.rb +5 -0
  244. data/test/vendor/plugins/dynamic_form/lib/action_view/helpers/dynamic_form.rb +300 -0
  245. data/test/vendor/plugins/dynamic_form/lib/action_view/locale/en.yml +8 -0
  246. data/test/vendor/plugins/dynamic_form/test/dynamic_form_i18n_test.rb +42 -0
  247. data/test/vendor/plugins/dynamic_form/test/dynamic_form_test.rb +370 -0
  248. data/test/vendor/plugins/dynamic_form/test/test_helper.rb +9 -0
  249. metadata +403 -0
@@ -0,0 +1,205 @@
1
+ require 'test_helper'
2
+ require 'urligence'
3
+
4
+ class PhotosController
5
+ include ResourceController::Urligence
6
+ end
7
+
8
+ class UrligenceTest < ActiveSupport::TestCase
9
+ def setup
10
+ @controller = PhotosController.new
11
+ @tag = Tag.new
12
+ @tag.stubs(:to_param).returns('awesomestuff')
13
+ @photo = Photo.new
14
+ @photo.stubs(:to_param).returns(1)
15
+ end
16
+
17
+ context "with one object" do
18
+ setup do
19
+ setup_mocks "/photos/#{@photo.to_param}", :photo, @photo
20
+ end
21
+
22
+ context "urligence" do
23
+ should "return the correct path" do
24
+ assert_equal @expected_path, @controller.urligence(@photo, :path)
25
+ end
26
+ end
27
+
28
+ context "smart_url" do
29
+ should "return the correct url" do
30
+ assert_equal @expected_url, @controller.smart_url(@photo)
31
+ end
32
+ end
33
+
34
+ context "smart_path" do
35
+ should "return the correct path" do
36
+ assert_equal @expected_path, @controller.smart_path(@photo)
37
+ end
38
+ end
39
+ end
40
+
41
+ context "with two objects" do
42
+ setup do
43
+ setup_mocks "/tags/#{@tag.to_param}/photos/#{@photo.to_param}", :tag_photo, @tag, @photo
44
+ end
45
+
46
+ should "return the correct path" do
47
+ assert_equal @expected_path, @controller.urligence(@tag, @photo, :path)
48
+ end
49
+ end
50
+
51
+ context "with a namespace as first param" do
52
+ setup do
53
+ setup_mocks "/admin/tags/#{@tag.to_param}/photos/#{@photo.to_param}", :admin_tag_photo, @tag, @photo
54
+ end
55
+
56
+ should "return the correct path" do
57
+ assert_equal @expected_path, @controller.urligence(:admin, @tag, @photo, :path)
58
+ end
59
+ end
60
+
61
+ context "with many nil options anywhere in the arguments" do
62
+ setup do
63
+ setup_mocks "/tags/#{@tag.to_param}/photos/#{@photo.to_param}", :tag_photo, @tag, @photo
64
+ end
65
+
66
+ should "return the correct path" do
67
+ assert_equal @expected_path, @controller.urligence(nil, nil, nil, @tag, nil, @photo, nil, :path)
68
+ end
69
+ end
70
+
71
+ context "with a symbol as the last parameter" do
72
+ setup do
73
+ setup_mocks "/tags/#{@tag.to_param}/photos", :tag_photos, @tag
74
+ end
75
+
76
+ should "use that as the last fragment of the url" do
77
+ assert_equal @expected_path, @controller.urligence(@tag, :photos, :path)
78
+ end
79
+ end
80
+
81
+ context "with a symbol as the only parameter" do
82
+ setup do
83
+ setup_mocks "/photos", :photos
84
+ end
85
+
86
+ should "use that as the only url fragment" do
87
+ assert_equal @expected_path, @controller.urligence(nil, :photos, :path)
88
+ end
89
+ end
90
+
91
+ context "with a namespace, and a plural symbol" do
92
+ setup do
93
+ setup_mocks "/admin/products", :admin_products
94
+ end
95
+
96
+ should "call the correct url handler" do
97
+ assert_equal @expected_path, @controller.urligence(:admin, :products, :path)
98
+ end
99
+ end
100
+
101
+ context "with only symbols" do
102
+ setup do
103
+ setup_mocks '/admin/products/new', :new_admin_products
104
+ end
105
+
106
+ should "return the correct path" do
107
+ assert_equal @expected_path, @controller.urligence(:new, :admin, :products, :path)
108
+ end
109
+ end
110
+
111
+ context "with array parameters for specifying the names of routes that don't match the class name of the object" do
112
+ setup do
113
+ setup_mocks '/something_tags/1', :something_tag, @tag
114
+ end
115
+
116
+ context "urligence" do
117
+ should "use the name of the symbol as the url fragment" do
118
+ assert_equal @expected_path, @controller.urligence([:something_tag, @tag], :path)
119
+ end
120
+ end
121
+
122
+ context "smart_url" do
123
+ should "return the correct path" do
124
+ assert_equal @expected_url, @controller.smart_url([:something_tag, @tag])
125
+ end
126
+ end
127
+
128
+ context "smart_path" do
129
+ should "return the correct path" do
130
+ assert_equal @expected_path, @controller.smart_path([:something_tag, @tag])
131
+ end
132
+ end
133
+ end
134
+
135
+ context "with array parameters and a namespace" do
136
+ setup do
137
+ setup_mocks '/admin/something_tags/1', :admin_something_tag, @tag
138
+ end
139
+
140
+ should "return the correct url" do
141
+ assert_equal @expected_path, @controller.urligence(:admin, [:something_tag, @tag], :path)
142
+ end
143
+ end
144
+
145
+ context "with array parameters, a namespace, and an ending symbol" do
146
+ setup do
147
+ setup_mocks '/admin/something_tags/1/photos', :admin_something_tag_photos, @tag
148
+ end
149
+
150
+ should "return the correct url" do
151
+ assert_equal @expected_path, @controller.urligence(:admin, [:something_tag, @tag], :photos, :path)
152
+ end
153
+ end
154
+
155
+ context "with array parameters, a symbol namespace, and normal model parameters" do
156
+ setup do
157
+ setup_mocks '/admin/something_tags/1/photos/1', :admin_something_tag_photo, @tag, @photo
158
+ end
159
+
160
+ should "return the correct url" do
161
+ assert_equal @expected_path, @controller.urligence(:admin, [:something_tag, @tag], @photo, :path)
162
+ end
163
+ end
164
+
165
+ context "hash_for" do
166
+ context "url" do
167
+ setup do
168
+ @controller.stubs(:hash_for_photo_tag_url).with(:id => @tag.to_param, :photo_id => @photo.to_param).returns("something")
169
+ end
170
+
171
+ should "return the correct hash" do
172
+ assert_equal "something", @controller.hash_for_smart_url(@photo, @tag)
173
+ end
174
+ end
175
+
176
+ context "path" do
177
+ setup do
178
+ @photo_tag = stub(:class => stub(:name => "PhotoTag"), :to_param => 'awesomestuff')
179
+ @controller.stubs(:hash_for_photo_tag_path).with(:id => @tag.to_param, :photo_id => @photo.to_param).returns("something")
180
+ end
181
+
182
+ should "return the correct hash" do
183
+ assert_equal "something", @controller.hash_for_smart_path(@photo, [:tag, @photo_tag])
184
+ end
185
+ end
186
+
187
+ context "collection path" do
188
+ setup do
189
+ @controller.stubs(:hash_for_photos_path).with({}).returns('something')
190
+ end
191
+
192
+ should "call the correct methods" do
193
+ assert_equal 'something', @controller.hash_for_smart_path(:photos)
194
+ end
195
+ end
196
+ end
197
+
198
+ private
199
+ def setup_mocks(expected_path, method, *params)
200
+ @expected_path = expected_path
201
+ @controller.stubs("#{method}_path".to_sym).with(*params).returns(@expected_path)
202
+ @controller.stubs("#{method}_url".to_sym).with(*params).returns(@expected_url = "http://localhost#{@expected_path}")
203
+ @controller.stubs("hash_for_#{method}_url".to_sym).with(*params).returns(@expected_hash = @expected_url)
204
+ end
205
+ end
@@ -0,0 +1 @@
1
+ S
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ DynamicForm
2
+ ===========
3
+
4
+ DynamicForm holds a few helpers method to help you deal with your models, they are:
5
+
6
+ * input(record, method, options = {})
7
+ * form(record, options = {})
8
+ * error_message_on(object, method, options={})
9
+ * error_messages_for(record, options={})
10
+
11
+ It also adds f.error_messages and f.error_messages_on to your form builders.
12
+
13
+ Copyright (c) 2010 David Heinemeier Hansson, released under the MIT license
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ desc 'Default: run unit tests.'
4
+ task :default => :test
5
+
6
+ desc 'Test the active_model_helper plugin.'
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ end
@@ -0,0 +1,5 @@
1
+ require 'action_view/helpers/dynamic_form'
2
+
3
+ class ActionView::Base
4
+ include DynamicForm
5
+ end
@@ -0,0 +1,300 @@
1
+ require 'action_view/helpers'
2
+ require 'active_support/i18n'
3
+ require 'active_support/core_ext/enumerable'
4
+ require 'active_support/core_ext/object/blank'
5
+
6
+ module ActionView
7
+ module Helpers
8
+ # The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the +form+
9
+ # method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This
10
+ # is a great way of making the record quickly available for editing, but likely to prove lackluster for a complicated real-world form.
11
+ # In that case, it's better to use the +input+ method and the specialized +form+ methods in link:classes/ActionView/Helpers/FormHelper.html
12
+ module DynamicForm
13
+ # Returns a default input tag for the type of object returned by the method. For example, if <tt>@post</tt>
14
+ # has an attribute +title+ mapped to a +VARCHAR+ column that holds "Hello World":
15
+ #
16
+ # input("post", "title")
17
+ # # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
18
+ def input(record_name, method, options = {})
19
+ InstanceTag.new(record_name, method, self).to_tag(options)
20
+ end
21
+
22
+ # Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
23
+ # has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
24
+ #
25
+ # form("post")
26
+ #
27
+ # would yield a form like the following (modulus formatting):
28
+ #
29
+ # <form action='/posts/create' method='post'>
30
+ # <p>
31
+ # <label for="post_title">Title</label><br />
32
+ # <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
33
+ # </p>
34
+ # <p>
35
+ # <label for="post_body">Body</label><br />
36
+ # <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
37
+ # </p>
38
+ # <input name="commit" type="submit" value="Create" />
39
+ # </form>
40
+ #
41
+ # It's possible to specialize the form builder by using a different action name and by supplying another
42
+ # block renderer. For example, if <tt>@entry</tt> has an attribute +message+ of type +VARCHAR+ then
43
+ #
44
+ # form("entry",
45
+ # :action => "sign",
46
+ # :input_block => Proc.new { |record, column|
47
+ # "#{column.human_name}: #{input(record, column.name)}<br />"
48
+ # })
49
+ #
50
+ # would yield a form like the following (modulus formatting):
51
+ #
52
+ # <form action="/entries/sign" method="post">
53
+ # Message:
54
+ # <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
55
+ # <input name="commit" type="submit" value="Sign" />
56
+ # </form>
57
+ #
58
+ # It's also possible to add additional content to the form by giving it a block, such as:
59
+ #
60
+ # form("entry", :action => "sign") do |form|
61
+ # form << content_tag("b", "Department")
62
+ # form << collection_select("department", "id", @departments, "id", "name")
63
+ # end
64
+ #
65
+ # The following options are available:
66
+ #
67
+ # * <tt>:action</tt> - The action used when submitting the form (default: +create+ if a new record, otherwise +update+).
68
+ # * <tt>:input_block</tt> - Specialize the output using a different block, see above.
69
+ # * <tt>:method</tt> - The method used when submitting the form (default: +post+).
70
+ # * <tt>:multipart</tt> - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: +false+).
71
+ # * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
72
+ def form(record_name, options = {})
73
+ record = instance_variable_get("@#{record_name}")
74
+ record = convert_to_model(record)
75
+
76
+ options = options.symbolize_keys
77
+ options[:action] ||= record.persisted? ? "update" : "create"
78
+ action = url_for(:action => options[:action], :id => record)
79
+
80
+ submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
81
+
82
+ contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
83
+ contents.safe_concat hidden_field(record_name, :id) if record.persisted?
84
+ contents.safe_concat all_input_tags(record, record_name, options)
85
+ yield contents if block_given?
86
+ contents.safe_concat submit_tag(submit_value)
87
+ contents.safe_concat('</form>')
88
+ end
89
+
90
+ # Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
91
+ # This error message is wrapped in a <tt>DIV</tt> tag by default or with <tt>:html_tag</tt> if specified,
92
+ # which can be extended to include a <tt>:prepend_text</tt> and/or <tt>:append_text</tt> (to properly explain
93
+ # the error), and a <tt>:css_class</tt> to style it accordingly. +object+ should either be the name of an
94
+ # instance variable or the actual object. The method can be passed in either as a string or a symbol.
95
+ # As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
96
+ #
97
+ # <%= error_message_on "post", "title" %>
98
+ # # => <div class="formError">can't be empty</div>
99
+ #
100
+ # <%= error_message_on @post, :title %>
101
+ # # => <div class="formError">can't be empty</div>
102
+ #
103
+ # <%= error_message_on "post", "title",
104
+ # :prepend_text => "Title simply ",
105
+ # :append_text => " (or it won't work).",
106
+ # :html_tag => "span",
107
+ # :css_class => "inputError" %>
108
+ # # => <span class="inputError">Title simply can't be empty (or it won't work).</span>
109
+ def error_message_on(object, method, *args)
110
+ options = args.extract_options!
111
+ unless args.empty?
112
+ ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
113
+ 'prepend_text, append_text, html_tag, and css_class arguments', caller)
114
+
115
+ options[:prepend_text] = args[0] || ''
116
+ options[:append_text] = args[1] || ''
117
+ options[:html_tag] = args[2] || 'div'
118
+ options[:css_class] = args[3] || 'formError'
119
+ end
120
+ options.reverse_merge!(:prepend_text => '', :append_text => '', :html_tag => 'div', :css_class => 'formError')
121
+
122
+ object = convert_to_model(object)
123
+
124
+ if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
125
+ (errors = obj.errors[method]).presence
126
+ content_tag(options[:html_tag],
127
+ (options[:prepend_text].html_safe << errors.first).safe_concat(options[:append_text]),
128
+ :class => options[:css_class]
129
+ )
130
+ else
131
+ ''
132
+ end
133
+ end
134
+
135
+ # Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
136
+ # given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
137
+ # provided.
138
+ #
139
+ # This <tt>DIV</tt> can be tailored by the following options:
140
+ #
141
+ # * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
142
+ # * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
143
+ # * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
144
+ # * <tt>:object</tt> - The object (or array of objects) for which to display errors,
145
+ # if you need to escape the instance variable convention.
146
+ # * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
147
+ # If <tt>:object_name</tt> is not set, the name of the first object will be used.
148
+ # * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
149
+ # or an empty string to avoid the header message altogether. (Default: "X errors
150
+ # prohibited this object from being saved").
151
+ # * <tt>:message</tt> - The explanation message after the header message and before
152
+ # the error list. Pass +nil+ or an empty string to avoid the explanation message
153
+ # altogether. (Default: "There were problems with the following fields:").
154
+ #
155
+ # To specify the display for one object, you simply provide its name as a parameter.
156
+ # For example, for the <tt>@user</tt> model:
157
+ #
158
+ # error_messages_for 'user'
159
+ #
160
+ # You can also supply an object:
161
+ #
162
+ # error_messages_for @user
163
+ #
164
+ # This will use the last part of the model name in the presentation. For instance, if
165
+ # this is a MyKlass::User object, this will use "user" as the name in the String. This
166
+ # is taken from MyKlass::User.model_name.human, which can be overridden.
167
+ #
168
+ # To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
169
+ # will be the name used in the header message:
170
+ #
171
+ # error_messages_for 'user_common', 'user', :object_name => 'user'
172
+ #
173
+ # You can also use a number of objects, which will have the same naming semantics
174
+ # as a single object.
175
+ #
176
+ # error_messages_for @user, @post
177
+ #
178
+ # If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> parameter which gives the actual
179
+ # object (or array of objects to use):
180
+ #
181
+ # error_messages_for 'user', :object => @question.user
182
+ #
183
+ # NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
184
+ # you need is significantly different from the default presentation, it makes plenty of sense to access the <tt>object.errors</tt>
185
+ # instance yourself and set it up. View the source of this method to see how easy it is.
186
+ def error_messages_for(*params)
187
+ options = params.extract_options!.symbolize_keys
188
+
189
+ objects = Array.wrap(options.delete(:object) || params).map do |object|
190
+ object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
191
+ object = convert_to_model(object)
192
+
193
+ if object.class.respond_to?(:model_name)
194
+ options[:object_name] ||= object.class.model_name.human.downcase
195
+ end
196
+
197
+ object
198
+ end
199
+
200
+ objects.compact!
201
+ count = objects.inject(0) {|sum, object| sum + object.errors.count }
202
+
203
+ unless count.zero?
204
+ html = {}
205
+ [:id, :class].each do |key|
206
+ if options.include?(key)
207
+ value = options[key]
208
+ html[key] = value unless value.blank?
209
+ else
210
+ html[key] = 'errorExplanation'
211
+ end
212
+ end
213
+ options[:object_name] ||= params.first
214
+
215
+ I18n.with_options :locale => options[:locale], :scope => [:errors, :template] do |locale|
216
+ header_message = if options.include?(:header_message)
217
+ options[:header_message]
218
+ else
219
+ locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
220
+ end
221
+
222
+ message = options.include?(:message) ? options[:message] : locale.t(:body)
223
+
224
+ error_messages = objects.sum do |object|
225
+ object.errors.full_messages.map do |msg|
226
+ content_tag(:li, msg)
227
+ end
228
+ end.join.html_safe
229
+
230
+ contents = ''
231
+ contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
232
+ contents << content_tag(:p, message) unless message.blank?
233
+ contents << content_tag(:ul, error_messages)
234
+
235
+ content_tag(:div, contents.html_safe, html)
236
+ end
237
+ else
238
+ ''
239
+ end
240
+ end
241
+
242
+ private
243
+
244
+ def all_input_tags(record, record_name, options)
245
+ input_block = options[:input_block] || default_input_block
246
+ record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
247
+ end
248
+
249
+ def default_input_block
250
+ Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) }
251
+ end
252
+
253
+ module InstanceTagMethods
254
+ def to_tag(options = {})
255
+ case column_type
256
+ when :string
257
+ field_type = @method_name.include?("password") ? "password" : "text"
258
+ to_input_field_tag(field_type, options)
259
+ when :text
260
+ to_text_area_tag(options)
261
+ when :integer, :float, :decimal
262
+ to_input_field_tag("text", options)
263
+ when :date
264
+ to_date_select_tag(options)
265
+ when :datetime, :timestamp
266
+ to_datetime_select_tag(options)
267
+ when :time
268
+ to_time_select_tag(options)
269
+ when :boolean
270
+ to_boolean_select_tag(options)
271
+ end
272
+ end
273
+
274
+ def column_type
275
+ object.send(:column_for_attribute, @method_name).type
276
+ end
277
+ end
278
+
279
+ module FormBuilderMethods
280
+ def error_message_on(method, *args)
281
+ @template.error_message_on(@object || @object_name, method, *args)
282
+ end
283
+
284
+ def error_messages(options = {})
285
+ @template.error_messages_for(@object_name, objectify_options(options))
286
+ end
287
+ end
288
+ end
289
+
290
+ class InstanceTag
291
+ include DynamicForm::InstanceTagMethods
292
+ end
293
+
294
+ class FormBuilder
295
+ include DynamicForm::FormBuilderMethods
296
+ end
297
+ end
298
+ end
299
+
300
+ I18n.load_path << File.expand_path("../../locale/en.yml", __FILE__)
@@ -0,0 +1,8 @@
1
+ en:
2
+ errors:
3
+ template:
4
+ header:
5
+ one: "1 error prohibited this %{model} from being saved"
6
+ other: "%{count} errors prohibited this %{model} from being saved"
7
+ # The variable :count is also available
8
+ body: "There were problems with the following fields:"
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class DynamicFormI18nTest < Test::Unit::TestCase
4
+ include ActionView::Context
5
+ include ActionView::Helpers::DynamicForm
6
+
7
+ attr_reader :request
8
+
9
+ def setup
10
+ @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
11
+ @object.stubs :to_model => @object
12
+ @object.stubs :class => stub(:model_name => stub(:human => ""))
13
+
14
+ @object_name = 'book_seller'
15
+ @object_name_without_underscore = 'book seller'
16
+
17
+ stubs(:content_tag).returns 'content_tag'
18
+
19
+ I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
20
+ I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).returns 'There were problems with the following fields:'
21
+ end
22
+
23
+ def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
24
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').never
25
+ error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
26
+ end
27
+
28
+ def test_error_messages_for_given_no_header_option_it_translates_header_message
29
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').returns 'header message'
30
+ error_messages_for(:object => @object, :locale => 'en')
31
+ end
32
+
33
+ def test_error_messages_for_given_a_message_option_it_does_not_translate_message
34
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).never
35
+ error_messages_for(:object => @object, :message => 'message', :locale => 'en')
36
+ end
37
+
38
+ def test_error_messages_for_given_no_message_option_it_translates_message
39
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).returns 'There were problems with the following fields:'
40
+ error_messages_for(:object => @object, :locale => 'en')
41
+ end
42
+ end