resource_controller 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. data/LICENSE +22 -0
  2. data/README +282 -0
  3. data/README.rdoc +282 -0
  4. data/Rakefile +35 -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/init.rb +1 -0
  35. data/lib/resource_controller.rb +15 -0
  36. data/lib/resource_controller/accessors.rb +77 -0
  37. data/lib/resource_controller/action_options.rb +40 -0
  38. data/lib/resource_controller/actions.rb +75 -0
  39. data/lib/resource_controller/base.rb +15 -0
  40. data/lib/resource_controller/class_methods.rb +22 -0
  41. data/lib/resource_controller/controller.rb +63 -0
  42. data/lib/resource_controller/failable_action_options.rb +25 -0
  43. data/lib/resource_controller/helpers.rb +28 -0
  44. data/lib/resource_controller/helpers/current_objects.rb +69 -0
  45. data/lib/resource_controller/helpers/internal.rb +76 -0
  46. data/lib/resource_controller/helpers/nested.rb +45 -0
  47. data/lib/resource_controller/helpers/urls.rb +124 -0
  48. data/lib/resource_controller/response_collector.rb +27 -0
  49. data/lib/resource_controller/version.rb +9 -0
  50. data/lib/tasks/gem.rake +67 -0
  51. data/lib/urligence.rb +50 -0
  52. data/rails/init.rb +6 -0
  53. data/test/Rakefile +10 -0
  54. data/test/app/controllers/application.rb +7 -0
  55. data/test/app/controllers/cms/options_controller.rb +3 -0
  56. data/test/app/controllers/cms/products_controller.rb +3 -0
  57. data/test/app/controllers/comments_controller.rb +3 -0
  58. data/test/app/controllers/people_controller.rb +9 -0
  59. data/test/app/controllers/photos_controller.rb +11 -0
  60. data/test/app/controllers/posts_controller.rb +10 -0
  61. data/test/app/controllers/projects_controller.rb +3 -0
  62. data/test/app/controllers/somethings_controller.rb +3 -0
  63. data/test/app/controllers/tags_controller.rb +13 -0
  64. data/test/app/controllers/users_controller.rb +12 -0
  65. data/test/app/helpers/application_helper.rb +3 -0
  66. data/test/app/helpers/cms/products_helper.rb +2 -0
  67. data/test/app/helpers/comments_helper.rb +2 -0
  68. data/test/app/helpers/options_helper.rb +2 -0
  69. data/test/app/helpers/people_helper.rb +2 -0
  70. data/test/app/helpers/photos_helper.rb +2 -0
  71. data/test/app/helpers/posts_helper.rb +2 -0
  72. data/test/app/helpers/projects_helper.rb +2 -0
  73. data/test/app/helpers/somethings_helper.rb +2 -0
  74. data/test/app/helpers/tags_helper.rb +2 -0
  75. data/test/app/helpers/users_helper.rb +2 -0
  76. data/test/app/models/account.rb +3 -0
  77. data/test/app/models/comment.rb +3 -0
  78. data/test/app/models/option.rb +3 -0
  79. data/test/app/models/photo.rb +4 -0
  80. data/test/app/models/post.rb +3 -0
  81. data/test/app/models/product.rb +3 -0
  82. data/test/app/models/project.rb +2 -0
  83. data/test/app/models/something.rb +2 -0
  84. data/test/app/models/tag.rb +3 -0
  85. data/test/app/views/cms/options/edit.rhtml +17 -0
  86. data/test/app/views/cms/options/index.rhtml +20 -0
  87. data/test/app/views/cms/options/new.rhtml +16 -0
  88. data/test/app/views/cms/options/show.rhtml +8 -0
  89. data/test/app/views/cms/products/edit.rhtml +17 -0
  90. data/test/app/views/cms/products/index.rhtml +20 -0
  91. data/test/app/views/cms/products/new.rhtml +16 -0
  92. data/test/app/views/cms/products/show.rhtml +8 -0
  93. data/test/app/views/comments/edit.rhtml +27 -0
  94. data/test/app/views/comments/index.rhtml +24 -0
  95. data/test/app/views/comments/new.rhtml +26 -0
  96. data/test/app/views/comments/show.rhtml +18 -0
  97. data/test/app/views/layouts/application.rhtml +17 -0
  98. data/test/app/views/layouts/comments.rhtml +17 -0
  99. data/test/app/views/layouts/options.rhtml +17 -0
  100. data/test/app/views/layouts/people.rhtml +17 -0
  101. data/test/app/views/layouts/photos.rhtml +17 -0
  102. data/test/app/views/layouts/projects.rhtml +17 -0
  103. data/test/app/views/layouts/somethings.rhtml +17 -0
  104. data/test/app/views/layouts/tags.rhtml +17 -0
  105. data/test/app/views/people/edit.rhtml +17 -0
  106. data/test/app/views/people/index.rhtml +20 -0
  107. data/test/app/views/people/new.rhtml +16 -0
  108. data/test/app/views/people/show.rhtml +8 -0
  109. data/test/app/views/photos/edit.rhtml +17 -0
  110. data/test/app/views/photos/index.rhtml +20 -0
  111. data/test/app/views/photos/new.rhtml +16 -0
  112. data/test/app/views/photos/show.rhtml +8 -0
  113. data/test/app/views/posts/edit.rhtml +22 -0
  114. data/test/app/views/posts/index.rhtml +22 -0
  115. data/test/app/views/posts/new.rhtml +21 -0
  116. data/test/app/views/posts/show.rhtml +13 -0
  117. data/test/app/views/projects/edit.rhtml +17 -0
  118. data/test/app/views/projects/index.rhtml +20 -0
  119. data/test/app/views/projects/new.rhtml +16 -0
  120. data/test/app/views/projects/show.rhtml +8 -0
  121. data/test/app/views/somethings/edit.rhtml +17 -0
  122. data/test/app/views/somethings/index.rhtml +20 -0
  123. data/test/app/views/somethings/new.rhtml +16 -0
  124. data/test/app/views/somethings/show.rhtml +8 -0
  125. data/test/app/views/tags/edit.rhtml +17 -0
  126. data/test/app/views/tags/index.rhtml +20 -0
  127. data/test/app/views/tags/index.rjs +0 -0
  128. data/test/app/views/tags/new.rhtml +16 -0
  129. data/test/app/views/tags/show.rhtml +8 -0
  130. data/test/app/views/users/edit.rhtml +17 -0
  131. data/test/app/views/users/index.rhtml +20 -0
  132. data/test/app/views/users/new.rhtml +16 -0
  133. data/test/app/views/users/show.rhtml +8 -0
  134. data/test/config/boot.rb +109 -0
  135. data/test/config/database.yml +16 -0
  136. data/test/config/environment.rb +64 -0
  137. data/test/config/environments/development.rb +21 -0
  138. data/test/config/environments/test.rb +19 -0
  139. data/test/config/routes.rb +51 -0
  140. data/test/db/migrate/001_create_posts.rb +12 -0
  141. data/test/db/migrate/002_create_products.rb +11 -0
  142. data/test/db/migrate/003_create_comments.rb +13 -0
  143. data/test/db/migrate/004_create_options.rb +12 -0
  144. data/test/db/migrate/005_create_photos.rb +11 -0
  145. data/test/db/migrate/006_create_tags.rb +17 -0
  146. data/test/db/migrate/007_create_somethings.rb +11 -0
  147. data/test/db/migrate/008_create_accounts.rb +11 -0
  148. data/test/db/migrate/009_add_account_id_to_photos.rb +9 -0
  149. data/test/db/migrate/010_create_projects.rb +11 -0
  150. data/test/db/schema.rb +65 -0
  151. data/test/log/development.log +2918 -0
  152. data/test/log/test.log +135619 -0
  153. data/test/log/thin.log +12 -0
  154. data/test/script/console +3 -0
  155. data/test/script/destroy +3 -0
  156. data/test/script/generate +3 -0
  157. data/test/script/server +3 -0
  158. data/test/test/fixtures/accounts.yml +7 -0
  159. data/test/test/fixtures/comments.yml +11 -0
  160. data/test/test/fixtures/options.yml +9 -0
  161. data/test/test/fixtures/photos.yml +9 -0
  162. data/test/test/fixtures/photos_tags.yml +3 -0
  163. data/test/test/fixtures/posts.yml +9 -0
  164. data/test/test/fixtures/products.yml +7 -0
  165. data/test/test/fixtures/projects.yml +7 -0
  166. data/test/test/fixtures/somethings.yml +7 -0
  167. data/test/test/fixtures/tags.yml +7 -0
  168. data/test/test/functional/cms/options_controller_test.rb +23 -0
  169. data/test/test/functional/cms/products_controller_test.rb +23 -0
  170. data/test/test/functional/comments_controller_test.rb +26 -0
  171. data/test/test/functional/people_controller_test.rb +34 -0
  172. data/test/test/functional/photos_controller_test.rb +130 -0
  173. data/test/test/functional/posts_controller_test.rb +34 -0
  174. data/test/test/functional/projects_controller_test.rb +18 -0
  175. data/test/test/functional/somethings_controller_test.rb +28 -0
  176. data/test/test/functional/tags_controller_test.rb +64 -0
  177. data/test/test/functional/users_controller_test.rb +24 -0
  178. data/test/test/test_helper.rb +12 -0
  179. data/test/test/unit/accessors_test.rb +110 -0
  180. data/test/test/unit/account_test.rb +7 -0
  181. data/test/test/unit/action_options_test.rb +109 -0
  182. data/test/test/unit/base_test.rb +11 -0
  183. data/test/test/unit/comment_test.rb +10 -0
  184. data/test/test/unit/failable_action_options_test.rb +77 -0
  185. data/test/test/unit/helpers/current_objects_test.rb +127 -0
  186. data/test/test/unit/helpers/internal_test.rb +106 -0
  187. data/test/test/unit/helpers/nested_test.rb +82 -0
  188. data/test/test/unit/helpers/urls_test.rb +71 -0
  189. data/test/test/unit/helpers_test.rb +25 -0
  190. data/test/test/unit/option_test.rb +10 -0
  191. data/test/test/unit/photo_test.rb +10 -0
  192. data/test/test/unit/post_test.rb +10 -0
  193. data/test/test/unit/project_test.rb +10 -0
  194. data/test/test/unit/response_collector_test.rb +49 -0
  195. data/test/test/unit/something_test.rb +10 -0
  196. data/test/test/unit/tag_test.rb +10 -0
  197. data/test/test/unit/urligence_test.rb +203 -0
  198. data/test/vendor/plugins/shoulda/Rakefile +32 -0
  199. data/test/vendor/plugins/shoulda/bin/convert_to_should_syntax +40 -0
  200. data/test/vendor/plugins/shoulda/init.rb +3 -0
  201. data/test/vendor/plugins/shoulda/lib/shoulda.rb +43 -0
  202. data/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb +580 -0
  203. data/test/vendor/plugins/shoulda/lib/shoulda/color.rb +77 -0
  204. data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb +467 -0
  205. data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb +201 -0
  206. data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb +170 -0
  207. data/test/vendor/plugins/shoulda/lib/shoulda/gem/proc_extensions.rb +14 -0
  208. data/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb +239 -0
  209. data/test/vendor/plugins/shoulda/lib/shoulda/general.rb +118 -0
  210. data/test/vendor/plugins/shoulda/lib/shoulda/private_helpers.rb +22 -0
  211. metadata +312 -0
@@ -0,0 +1,77 @@
1
+ require 'test/unit/ui/console/testrunner'
2
+
3
+ # Completely stolen from redgreen gem
4
+ #
5
+ # Adds colored output to your tests. Specify <tt>color: true</tt> in
6
+ # your <tt>~/.shoulda.conf</tt> file to enable.
7
+ #
8
+ # *Bug*: for some reason, this adds another line of output to the end of
9
+ # every rake task, as though there was another (empty) set of tests.
10
+ # A fix would be most welcome.
11
+ #
12
+ module ThoughtBot::Shoulda::Color
13
+ COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 } # :nodoc:
14
+ def self.method_missing(color_name, *args) # :nodoc:
15
+ color(color_name) + args.first + color(:clear)
16
+ end
17
+ def self.color(color) # :nodoc:
18
+ "\e[#{COLORS[color.to_sym]}m"
19
+ end
20
+ end
21
+
22
+ module Test # :nodoc:
23
+ module Unit # :nodoc:
24
+ class TestResult # :nodoc:
25
+ alias :old_to_s :to_s
26
+ def to_s
27
+ if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
28
+ ThoughtBot::Shoulda::Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&)
29
+ end
30
+ end
31
+ end
32
+
33
+ class AutoRunner # :nodoc:
34
+ alias :old_initialize :initialize
35
+ def initialize(standalone)
36
+ old_initialize(standalone)
37
+ @runner = proc do |r|
38
+ Test::Unit::UI::Console::RedGreenTestRunner
39
+ end
40
+ end
41
+ end
42
+
43
+ class Failure # :nodoc:
44
+ alias :old_long_display :long_display
45
+ def long_display
46
+ # old_long_display.sub('Failure', ThoughtBot::Shoulda::Color.red('Failure'))
47
+ ThoughtBot::Shoulda::Color.red(old_long_display)
48
+ end
49
+ end
50
+
51
+ class Error # :nodoc:
52
+ alias :old_long_display :long_display
53
+ def long_display
54
+ # old_long_display.sub('Error', ThoughtBot::Shoulda::Color.yellow('Error'))
55
+ ThoughtBot::Shoulda::Color.yellow(old_long_display)
56
+ end
57
+ end
58
+
59
+ module UI # :nodoc:
60
+ module Console # :nodoc:
61
+ class RedGreenTestRunner < Test::Unit::UI::Console::TestRunner # :nodoc:
62
+ def output_single(something, level=NORMAL)
63
+ return unless (output?(level))
64
+ something = case something
65
+ when '.' then ThoughtBot::Shoulda::Color.green('.')
66
+ when 'F' then ThoughtBot::Shoulda::Color.red("F")
67
+ when 'E' then ThoughtBot::Shoulda::Color.yellow("E")
68
+ else something
69
+ end
70
+ @io.write(something)
71
+ @io.flush
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,467 @@
1
+ module ThoughtBot # :nodoc:
2
+ module Shoulda # :nodoc:
3
+ module Controller
4
+ def self.included(other) # :nodoc:
5
+ other.class_eval do
6
+ extend ThoughtBot::Shoulda::Controller::ClassMethods
7
+ include ThoughtBot::Shoulda::Controller::InstanceMethods
8
+ ThoughtBot::Shoulda::Controller::ClassMethods::VALID_FORMATS.each do |format|
9
+ include "ThoughtBot::Shoulda::Controller::#{format.to_s.upcase}".constantize
10
+ end
11
+ end
12
+ end
13
+
14
+ # = Macro test helpers for your controllers
15
+ #
16
+ # By using the macro helpers you can quickly and easily create concise and easy to read test suites.
17
+ #
18
+ # This code segment:
19
+ # context "on GET to :show for first record" do
20
+ # setup do
21
+ # get :show, :id => 1
22
+ # end
23
+ #
24
+ # should_assign_to :user
25
+ # should_respond_with :success
26
+ # should_render_template :show
27
+ # should_not_set_the_flash
28
+ #
29
+ # should "do something else really cool" do
30
+ # assert_equal 1, assigns(:user).id
31
+ # end
32
+ # end
33
+ #
34
+ # Would produce 5 tests for the +show+ action
35
+ #
36
+ # Furthermore, the should_be_restful helper will create an entire set of tests which will verify that your
37
+ # controller responds restfully to a variety of requested formats.
38
+ module ClassMethods
39
+ # Formats tested by #should_be_restful. Defaults to [:html, :xml]
40
+ VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
41
+ VALID_FORMATS.each {|f| require "shoulda/controller_tests/formats/#{f}.rb"}
42
+
43
+ # Actions tested by #should_be_restful
44
+ VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
45
+
46
+ # A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
47
+ #
48
+ # Example:
49
+ # class UsersControllerTest < Test::Unit::TestCase
50
+ # load_all_fixtures
51
+ #
52
+ # def setup
53
+ # ...normal setup code...
54
+ # @user = User.find(:first)
55
+ # end
56
+ #
57
+ # should_be_restful do |resource|
58
+ # resource.identifier = :id
59
+ # resource.klass = User
60
+ # resource.object = :user
61
+ # resource.parent = []
62
+ # resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
63
+ # resource.formats = [:html, :xml]
64
+ #
65
+ # resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
66
+ # resource.update.params = { :name => "sue" }
67
+ #
68
+ # resource.create.redirect = "user_url(@user)"
69
+ # resource.update.redirect = "user_url(@user)"
70
+ # resource.destroy.redirect = "users_url"
71
+ #
72
+ # resource.create.flash = /created/i
73
+ # resource.update.flash = /updated/i
74
+ # resource.destroy.flash = /removed/i
75
+ # end
76
+ # end
77
+ #
78
+ # Whenever possible, the resource attributes will be set to sensible defaults.
79
+ #
80
+ class ResourceOptions
81
+ # Configuration options for the create, update, destroy actions under should_be_restful
82
+ class ActionOptions
83
+ # String evaled to get the target of the redirection.
84
+ # All of the instance variables set by the controller will be available to the
85
+ # evaled code.
86
+ #
87
+ # Example:
88
+ # resource.create.redirect = "user_url(@user.company, @user)"
89
+ #
90
+ # Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
91
+ attr_accessor :redirect
92
+
93
+ # String or Regexp describing a value expected in the flash. Will match against any flash key.
94
+ #
95
+ # Defaults:
96
+ # destroy:: /removed/
97
+ # create:: /created/
98
+ # update:: /updated/
99
+ attr_accessor :flash
100
+
101
+ # Hash describing the params that should be sent in with this action.
102
+ attr_accessor :params
103
+ end
104
+
105
+ # Configuration options for the denied actions under should_be_restful
106
+ #
107
+ # Example:
108
+ # context "The public" do
109
+ # setup do
110
+ # @request.session[:logged_in] = false
111
+ # end
112
+ #
113
+ # should_be_restful do |resource|
114
+ # resource.parent = :user
115
+ #
116
+ # resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
117
+ # resource.denied.flash = /get outta here/i
118
+ # resource.denied.redirect = 'new_session_url'
119
+ # end
120
+ # end
121
+ #
122
+ class DeniedOptions
123
+ # String evaled to get the target of the redirection.
124
+ # All of the instance variables set by the controller will be available to the
125
+ # evaled code.
126
+ #
127
+ # Example:
128
+ # resource.create.redirect = "user_url(@user.company, @user)"
129
+ attr_accessor :redirect
130
+
131
+ # String or Regexp describing a value expected in the flash. Will match against any flash key.
132
+ #
133
+ # Example:
134
+ # resource.create.flash = /created/
135
+ attr_accessor :flash
136
+
137
+ # Actions that should be denied (only used by resource.denied). <i>Note that these actions will
138
+ # only be tested if they are also listed in +resource.actions+</i>
139
+ # The special value of :all will deny all of the REST actions.
140
+ attr_accessor :actions
141
+ end
142
+
143
+ # Name of key in params that references the primary key.
144
+ # Will almost always be :id (default), unless you are using a plugin or have patched rails.
145
+ attr_accessor :identifier
146
+
147
+ # Name of the ActiveRecord class this resource is responsible for. Automatically determined from
148
+ # test class if not explicitly set. UserTest => "User"
149
+ attr_accessor :klass
150
+
151
+ # Name of the instantiated ActiveRecord object that should be used by some of the tests.
152
+ # Defaults to the underscored name of the AR class. CompanyManager => :company_manager
153
+ attr_accessor :object
154
+
155
+ # Name of the parent AR objects. Can be set as parent= or parents=, and can take either
156
+ # the name of the parent resource (if there's only one), or an array of names (if there's
157
+ # more than one).
158
+ #
159
+ # Example:
160
+ # # in the routes...
161
+ # map.resources :companies do
162
+ # map.resources :people do
163
+ # map.resources :limbs
164
+ # end
165
+ # end
166
+ #
167
+ # # in the tests...
168
+ # class PeopleControllerTest < Test::Unit::TestCase
169
+ # should_be_restful do |resource|
170
+ # resource.parent = :companies
171
+ # end
172
+ # end
173
+ #
174
+ # class LimbsControllerTest < Test::Unit::TestCase
175
+ # should_be_restful do |resource|
176
+ # resource.parents = [:companies, :people]
177
+ # end
178
+ # end
179
+ attr_accessor :parent
180
+ alias parents parent
181
+ alias parents= parent=
182
+
183
+ # Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
184
+ # Tests for each actionw will only be generated if the action is listed here.
185
+ # The special value of :all will test all of the REST actions.
186
+ #
187
+ # Example (for a read-only controller):
188
+ # resource.actions = [:show, :index]
189
+ attr_accessor :actions
190
+
191
+ # Formats that should be tested. Must be a subset of VALID_FORMATS (default).
192
+ # Each action will be tested against the formats listed here. The special value
193
+ # of :all will test all of the supported formats.
194
+ #
195
+ # Example:
196
+ # resource.actions = [:html, :xml]
197
+ attr_accessor :formats
198
+
199
+ # ActionOptions object specifying options for the create action.
200
+ attr_accessor :create
201
+
202
+ # ActionOptions object specifying options for the update action.
203
+ attr_accessor :update
204
+
205
+ # ActionOptions object specifying options for the desrtoy action.
206
+ attr_accessor :destroy
207
+
208
+ # DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
209
+ attr_accessor :denied
210
+
211
+ def initialize # :nodoc:
212
+ @create = ActionOptions.new
213
+ @update = ActionOptions.new
214
+ @destroy = ActionOptions.new
215
+ @denied = DeniedOptions.new
216
+
217
+ @create.flash ||= /created/i
218
+ @update.flash ||= /updated/i
219
+ @destroy.flash ||= /removed/i
220
+ @denied.flash ||= /denied/i
221
+
222
+ @create.params ||= {}
223
+ @update.params ||= {}
224
+
225
+ @actions = VALID_ACTIONS
226
+ @formats = VALID_FORMATS
227
+ @denied.actions = []
228
+ end
229
+
230
+ def normalize!(target) # :nodoc:
231
+ @denied.actions = VALID_ACTIONS if @denied.actions == :all
232
+ @actions = VALID_ACTIONS if @actions == :all
233
+ @formats = VALID_FORMATS if @formats == :all
234
+
235
+ @denied.actions = @denied.actions.map(&:to_sym)
236
+ @actions = @actions.map(&:to_sym)
237
+ @formats = @formats.map(&:to_sym)
238
+
239
+ ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
240
+ ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
241
+ ensure_valid_members(@formats, VALID_FORMATS, 'formats')
242
+
243
+ @identifier ||= :id
244
+ @klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
245
+ @object ||= @klass.name.tableize.singularize
246
+ @parent ||= []
247
+ @parent = [@parent] unless @parent.is_a? Array
248
+
249
+ collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_')
250
+ collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
251
+ @destroy.redirect ||= "#{collection_helper}(#{collection_args})"
252
+
253
+ member_helper = [@parent, @object, 'url'].flatten.join('_')
254
+ member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
255
+ @create.redirect ||= "#{member_helper}(#{member_args})"
256
+ @update.redirect ||= "#{member_helper}(#{member_args})"
257
+ @denied.redirect ||= "new_session_url"
258
+ end
259
+
260
+ private
261
+
262
+ def ensure_valid_members(ary, valid_members, name) # :nodoc:
263
+ invalid = ary - valid_members
264
+ raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
265
+ end
266
+ end
267
+
268
+ # :section: should_be_restful
269
+ # Generates a full suite of tests for a restful controller.
270
+ #
271
+ # The following definition will generate tests for the +index+, +show+, +new+,
272
+ # +edit+, +create+, +update+ and +destroy+ actions, in both +html+ and +xml+ formats:
273
+ #
274
+ # should_be_restful do |resource|
275
+ # resource.parent = :user
276
+ #
277
+ # resource.create.params = { :title => "first post", :body => 'blah blah blah'}
278
+ # resource.update.params = { :title => "changed" }
279
+ # end
280
+ #
281
+ # This generates about 40 tests, all of the format:
282
+ # "on GET to :show should assign @user."
283
+ # "on GET to :show should not set the flash."
284
+ # "on GET to :show should render 'show' template."
285
+ # "on GET to :show should respond with success."
286
+ # "on GET to :show as xml should assign @user."
287
+ # "on GET to :show as xml should have ContentType set to 'application/xml'."
288
+ # "on GET to :show as xml should respond with success."
289
+ # "on GET to :show as xml should return <user/> as the root element."
290
+ # The +resource+ parameter passed into the block is a ResourceOptions object, and
291
+ # is used to configure the tests for the details of your resources.
292
+ #
293
+ def should_be_restful(&blk) # :yields: resource
294
+ resource = ResourceOptions.new
295
+ blk.call(resource)
296
+ resource.normalize!(self)
297
+
298
+ resource.formats.each do |format|
299
+ resource.actions.each do |action|
300
+ if self.respond_to? :"make_#{action}_#{format}_tests"
301
+ self.send(:"make_#{action}_#{format}_tests", resource)
302
+ else
303
+ should "test #{action} #{format}" do
304
+ flunk "Test for #{action} as #{format} not implemented"
305
+ end
306
+ end
307
+ end
308
+ end
309
+ end
310
+
311
+ # :section: Test macros
312
+
313
+ # Macro that creates a test asserting that the flash contains the given value.
314
+ # val can be a String, a Regex, or nil (indicating that the flash should not be set)
315
+ #
316
+ # Example:
317
+ #
318
+ # should_set_the_flash_to "Thank you for placing this order."
319
+ # should_set_the_flash_to /created/i
320
+ # should_set_the_flash_to nil
321
+ def should_set_the_flash_to(val)
322
+ if val
323
+ should "have #{val.inspect} in the flash" do
324
+ assert_contains flash.values, val, ", Flash: #{flash.inspect}"
325
+ end
326
+ else
327
+ should "not set the flash" do
328
+ assert_equal({}, flash, "Flash was set to:\n#{flash.inspect}")
329
+ end
330
+ end
331
+ end
332
+
333
+ # Macro that creates a test asserting that the flash is empty. Same as
334
+ # @should_set_the_flash_to nil@
335
+ def should_not_set_the_flash
336
+ should_set_the_flash_to nil
337
+ end
338
+
339
+ # Macro that creates a test asserting that the controller assigned to @name
340
+ #
341
+ # Example:
342
+ #
343
+ # should_assign_to :user
344
+ def should_assign_to(name)
345
+ should "assign @#{name}" do
346
+ assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
347
+ end
348
+ end
349
+
350
+ # Macro that creates a test asserting that the controller did not assign to @name
351
+ #
352
+ # Example:
353
+ #
354
+ # should_not_assign_to :user
355
+ def should_not_assign_to(name)
356
+ should "not assign to @#{name}" do
357
+ assert !assigns(name.to_sym), "@#{name} was visible"
358
+ end
359
+ end
360
+
361
+ # Macro that creates a test asserting that the controller responded with a 'response' status code.
362
+ # Example:
363
+ #
364
+ # should_respond_with :success
365
+ def should_respond_with(response)
366
+ should "respond with #{response}" do
367
+ assert_response response
368
+ end
369
+ end
370
+
371
+ # Macro that creates a test asserting that the controller rendered the given template.
372
+ # Example:
373
+ #
374
+ # should_render_template :new
375
+ def should_render_template(template)
376
+ should "render template #{template.inspect}" do
377
+ assert_template template.to_s
378
+ end
379
+ end
380
+
381
+ # Macro that creates a test asserting that the controller returned a redirect to the given path.
382
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
383
+ # set by the controller are available to the evaled string.
384
+ # Example:
385
+ #
386
+ # should_redirect_to '"/"'
387
+ # should_redirect_to "users_url(@user)"
388
+ def should_redirect_to(url)
389
+ should "redirect to #{url.inspect}" do
390
+ instantiate_variables_from_assigns do
391
+ assert_redirected_to eval(url, self.send(:binding), __FILE__, __LINE__)
392
+ end
393
+ end
394
+ end
395
+
396
+ # Macro that creates a test asserting that the rendered view contains a <form> element.
397
+ def should_render_a_form
398
+ should "display a form" do
399
+ assert_select "form", true, "The template doesn't contain a <form> element"
400
+ end
401
+ end
402
+ end
403
+
404
+ module InstanceMethods # :nodoc:
405
+
406
+ private # :enddoc:
407
+
408
+ SPECIAL_INSTANCE_VARIABLES = %w{
409
+ _cookies
410
+ _flash
411
+ _headers
412
+ _params
413
+ _request
414
+ _response
415
+ _session
416
+ action_name
417
+ before_filter_chain_aborted
418
+ cookies
419
+ flash
420
+ headers
421
+ ignore_missing_templates
422
+ logger
423
+ params
424
+ request
425
+ request_origin
426
+ response
427
+ session
428
+ template
429
+ template_class
430
+ template_root
431
+ url
432
+ variables_added
433
+ }.map(&:to_s)
434
+
435
+ def instantiate_variables_from_assigns(*names, &blk)
436
+ old = {}
437
+ names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?
438
+ names.each do |name|
439
+ old[name] = instance_variable_get("@#{name}")
440
+ instance_variable_set("@#{name}", assigns(name.to_sym))
441
+ end
442
+ blk.call
443
+ names.each do |name|
444
+ instance_variable_set("@#{name}", old[name])
445
+ end
446
+ end
447
+
448
+ def get_existing_record(res) # :nodoc:
449
+ returning(instance_variable_get("@#{res.object}")) do |record|
450
+ assert(record, "This test requires you to set @#{res.object} in your setup block")
451
+ end
452
+ end
453
+
454
+ def make_parent_params(resource, record = nil, parent_names = nil) # :nodoc:
455
+ parent_names ||= resource.parents.reverse
456
+ return {} if parent_names == [] # Base case
457
+ parent_name = parent_names.shift
458
+ parent = record ? record.send(parent_name) : parent_name.to_s.classify.constantize.find(:first)
459
+
460
+ { :"#{parent_name}_id" => parent.to_param }.merge(make_parent_params(resource, parent, parent_names))
461
+ end
462
+
463
+ end
464
+ end
465
+ end
466
+ end
467
+