josevalim-inherited_resources 0.1

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 (57) hide show
  1. data/CHANGELOG +4 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +362 -0
  4. data/Rakefile +19 -0
  5. data/init.rb +1 -0
  6. data/lib/inherited_resources.rb +4 -0
  7. data/lib/inherited_resources/base.rb +272 -0
  8. data/lib/inherited_resources/base_helpers.rb +199 -0
  9. data/lib/inherited_resources/belongs_to.rb +227 -0
  10. data/lib/inherited_resources/belongs_to_helpers.rb +89 -0
  11. data/lib/inherited_resources/class_methods.rb +155 -0
  12. data/lib/inherited_resources/polymorphic_helpers.rb +19 -0
  13. data/lib/inherited_resources/respond_to.rb +324 -0
  14. data/lib/inherited_resources/singleton_helpers.rb +53 -0
  15. data/lib/inherited_resources/url_helpers.rb +147 -0
  16. data/test/aliases_test.rb +71 -0
  17. data/test/base_helpers_test.rb +130 -0
  18. data/test/base_test.rb +219 -0
  19. data/test/belongs_to_base_test.rb +268 -0
  20. data/test/belongs_to_test.rb +109 -0
  21. data/test/class_methods_test.rb +73 -0
  22. data/test/fixtures/en.yml +9 -0
  23. data/test/nested_belongs_to_test.rb +138 -0
  24. data/test/polymorphic_base_test.rb +282 -0
  25. data/test/respond_to_test.rb +282 -0
  26. data/test/singleton_base_test.rb +226 -0
  27. data/test/test_helper.rb +37 -0
  28. data/test/url_helpers_test.rb +284 -0
  29. data/test/views/cities/edit.html.erb +1 -0
  30. data/test/views/cities/index.html.erb +1 -0
  31. data/test/views/cities/new.html.erb +1 -0
  32. data/test/views/cities/show.html.erb +1 -0
  33. data/test/views/comments/edit.html.erb +1 -0
  34. data/test/views/comments/index.html.erb +1 -0
  35. data/test/views/comments/new.html.erb +1 -0
  36. data/test/views/comments/show.html.erb +1 -0
  37. data/test/views/employees/edit.html.erb +1 -0
  38. data/test/views/employees/index.html.erb +1 -0
  39. data/test/views/employees/new.html.erb +1 -0
  40. data/test/views/employees/show.html.erb +1 -0
  41. data/test/views/managers/edit.html.erb +1 -0
  42. data/test/views/managers/new.html.erb +1 -0
  43. data/test/views/managers/show.html.erb +1 -0
  44. data/test/views/pets/edit.html.erb +1 -0
  45. data/test/views/professors/edit.html.erb +1 -0
  46. data/test/views/professors/index.html.erb +1 -0
  47. data/test/views/professors/new.html.erb +1 -0
  48. data/test/views/professors/show.html.erb +1 -0
  49. data/test/views/projects/index.html.erb +1 -0
  50. data/test/views/projects/respond_to_with_resource.html.erb +1 -0
  51. data/test/views/students/edit.html.erb +1 -0
  52. data/test/views/students/new.html.erb +1 -0
  53. data/test/views/users/edit.html.erb +1 -0
  54. data/test/views/users/index.html.erb +1 -0
  55. data/test/views/users/new.html.erb +1 -0
  56. data/test/views/users/show.html.erb +1 -0
  57. metadata +108 -0
@@ -0,0 +1,268 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # This test file is instead to test the how controller flow and actions
4
+ # using a belongs_to association. This is done using mocks a la rspec.
5
+ #
6
+ class Post
7
+ end
8
+
9
+ class Comment
10
+ def self.human_name; 'Comment'; end
11
+ end
12
+
13
+ class CommentsController < InheritedResources::Base
14
+ belongs_to :post
15
+ end
16
+
17
+ # Create a TestHelper module with some helpers
18
+ module CommentTestHelper
19
+ def setup
20
+ @controller = CommentsController.new
21
+ @controller.request = @request = ActionController::TestRequest.new
22
+ @controller.response = @response = ActionController::TestResponse.new
23
+ end
24
+
25
+ protected
26
+ def mock_post(stubs={})
27
+ @mock_post ||= mock(stubs)
28
+ end
29
+
30
+ def mock_comment(stubs={})
31
+ @mock_comment ||= mock(stubs)
32
+ end
33
+ end
34
+
35
+ class IndexActionBelongsToTest < Test::Unit::TestCase
36
+ include CommentTestHelper
37
+
38
+ def test_expose_all_comments_as_instance_variable
39
+ Post.expects(:find).with('37').returns(mock_post)
40
+ mock_post.expects(:comments).returns(Comment)
41
+ Comment.expects(:find).with(:all).returns([mock_comment])
42
+ get :index, :post_id => '37'
43
+ assert_equal mock_post, assigns(:post)
44
+ assert_equal [mock_comment], assigns(:comments)
45
+ end
46
+
47
+ def test_controller_should_render_index
48
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
49
+ Comment.stubs(:find).returns([mock_comment])
50
+ get :index
51
+ assert_response :success
52
+ assert_equal 'Index HTML', @response.body.strip
53
+ end
54
+
55
+ def test_render_all_comments_as_xml_when_mime_type_is_xml
56
+ @request.accept = 'application/xml'
57
+ Post.expects(:find).with('37').returns(mock_post)
58
+ mock_post.expects(:comments).returns(Comment)
59
+ Comment.expects(:find).with(:all).returns(mock_comment)
60
+ mock_comment.expects(:to_xml).returns('Generated XML')
61
+ get :index, :post_id => '37'
62
+ assert_response :success
63
+ assert_equal 'Generated XML', @response.body
64
+ end
65
+ end
66
+
67
+ class ShowActionBelongsToTest < Test::Unit::TestCase
68
+ include CommentTestHelper
69
+
70
+ def test_expose_the_resquested_comment
71
+ Post.expects(:find).with('37').returns(mock_post)
72
+ mock_post.expects(:comments).returns(Comment)
73
+ Comment.expects(:find).with('42').returns(mock_comment)
74
+ get :show, :id => '42', :post_id => '37'
75
+ assert_equal mock_post, assigns(:post)
76
+ assert_equal mock_comment, assigns(:comment)
77
+ end
78
+
79
+ def test_controller_should_render_show
80
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
81
+ Comment.stubs(:find).returns(mock_comment)
82
+ get :show
83
+ assert_response :success
84
+ assert_equal 'Show HTML', @response.body.strip
85
+ end
86
+
87
+ def test_render_exposed_comment_as_xml_when_mime_type_is_xml
88
+ @request.accept = 'application/xml'
89
+ Post.expects(:find).with('37').returns(mock_post)
90
+ mock_post.expects(:comments).returns(Comment)
91
+ Comment.expects(:find).with('42').returns(mock_comment)
92
+ mock_comment.expects(:to_xml).returns("Generated XML")
93
+ get :show, :id => '42', :post_id => '37'
94
+ assert_response :success
95
+ assert_equal 'Generated XML', @response.body
96
+ end
97
+ end
98
+
99
+ class NewActionBelongsToTest < Test::Unit::TestCase
100
+ include CommentTestHelper
101
+
102
+ def test_expose_a_new_comment
103
+ Post.expects(:find).with('37').returns(mock_post)
104
+ mock_post.expects(:comments).returns(Comment)
105
+ Comment.expects(:build).returns(mock_comment)
106
+ get :new, :post_id => '37'
107
+ assert_equal mock_post, assigns(:post)
108
+ assert_equal mock_comment, assigns(:comment)
109
+ end
110
+
111
+ def test_controller_should_render_new
112
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
113
+ Comment.stubs(:build).returns(mock_comment)
114
+ get :new
115
+ assert_response :success
116
+ assert_equal 'New HTML', @response.body.strip
117
+ end
118
+
119
+ def test_render_exposed_a_new_comment_as_xml_when_mime_type_is_xml
120
+ @request.accept = 'application/xml'
121
+ Post.expects(:find).with('37').returns(mock_post)
122
+ mock_post.expects(:comments).returns(Comment)
123
+ Comment.expects(:build).returns(mock_comment)
124
+ mock_comment.expects(:to_xml).returns("Generated XML")
125
+ get :new, :post_id => '37'
126
+ assert_equal 'Generated XML', @response.body
127
+ assert_response :success
128
+ end
129
+ end
130
+
131
+ class EditActionBelongsToTest < Test::Unit::TestCase
132
+ include CommentTestHelper
133
+
134
+ def test_expose_the_resquested_comment
135
+ Post.expects(:find).with('37').returns(mock_post)
136
+ mock_post.expects(:comments).returns(Comment)
137
+ Comment.expects(:find).with('42').returns(mock_comment)
138
+ get :edit, :id => '42', :post_id => '37'
139
+ assert_equal mock_post, assigns(:post)
140
+ assert_equal mock_comment, assigns(:comment)
141
+ assert_response :success
142
+ end
143
+
144
+ def test_controller_should_render_edit
145
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
146
+ Comment.stubs(:find).returns(mock_comment)
147
+ get :edit
148
+ assert_response :success
149
+ assert_equal 'Edit HTML', @response.body.strip
150
+ end
151
+ end
152
+
153
+ class CreateActionBelongsToTest < Test::Unit::TestCase
154
+ include CommentTestHelper
155
+
156
+ def test_expose_a_newly_create_comment_when_saved_with_success
157
+ Post.expects(:find).with('37').returns(mock_post)
158
+ mock_post.expects(:comments).returns(Comment)
159
+ Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(:save => true))
160
+ post :create, :post_id => '37', :comment => {:these => 'params'}
161
+ assert_equal mock_post, assigns(:post)
162
+ assert_equal mock_comment, assigns(:comment)
163
+ end
164
+
165
+ def test_redirect_to_the_created_comment
166
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
167
+ Comment.stubs(:build).returns(mock_comment(:save => true))
168
+ @controller.expects(:resource_url).returns('http://test.host/').times(2)
169
+ post :create
170
+ assert_redirected_to 'http://test.host/'
171
+ end
172
+
173
+ def test_show_flash_message_when_success
174
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
175
+ Comment.stubs(:build).returns(mock_comment(:save => true))
176
+ post :create
177
+ assert_equal flash[:notice], 'Comment was successfully created.'
178
+ end
179
+
180
+ def test_render_new_template_when_comment_cannot_be_saved
181
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
182
+ Comment.stubs(:build).returns(mock_comment(:save => false, :errors => []))
183
+ post :create
184
+ assert_response :success
185
+ assert_template 'new'
186
+ end
187
+
188
+ def test_dont_show_flash_message_when_comment_cannot_be_saved
189
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
190
+ Comment.stubs(:build).returns(mock_comment(:save => false, :errors => []))
191
+ post :create
192
+ assert flash.empty?
193
+ end
194
+ end
195
+
196
+ class UpdateActionBelongsToTest < Test::Unit::TestCase
197
+ include CommentTestHelper
198
+
199
+ def test_update_the_requested_object
200
+ Post.expects(:find).with('37').returns(mock_post)
201
+ mock_post.expects(:comments).returns(Comment)
202
+ Comment.expects(:find).with('42').returns(mock_comment)
203
+ mock_comment.expects(:update_attributes).with({'these' => 'params'}).returns(true)
204
+ put :update, :id => '42', :post_id => '37', :comment => {:these => 'params'}
205
+ assert_equal mock_post, assigns(:post)
206
+ assert_equal mock_comment, assigns(:comment)
207
+ end
208
+
209
+ def test_redirect_to_the_created_comment
210
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
211
+ Comment.stubs(:find).returns(mock_comment(:update_attributes => true))
212
+ @controller.expects(:resource_url).returns('http://test.host/')
213
+ put :update
214
+ assert_redirected_to 'http://test.host/'
215
+ end
216
+
217
+ def test_show_flash_message_when_success
218
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
219
+ Comment.stubs(:find).returns(mock_comment(:update_attributes => true))
220
+ put :update
221
+ assert_equal flash[:notice], 'Comment was successfully updated.'
222
+ end
223
+
224
+ def test_render_edit_template_when_comment_cannot_be_saved
225
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
226
+ Comment.stubs(:find).returns(mock_comment(:update_attributes => false, :errors => []))
227
+ put :update
228
+ assert_response :success
229
+ assert_template 'edit'
230
+ end
231
+
232
+ def test_dont_show_flash_message_when_comment_cannot_be_saved
233
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
234
+ Comment.stubs(:find).returns(mock_comment(:update_attributes => false, :errors => []))
235
+ put :update
236
+ assert flash.empty?
237
+ end
238
+ end
239
+
240
+ class DestroyActionBelongsToTest < Test::Unit::TestCase
241
+ include CommentTestHelper
242
+
243
+ def test_the_resquested_comment_is_destroyed
244
+ Post.expects(:find).with('37').returns(mock_post)
245
+ mock_post.expects(:comments).returns(Comment)
246
+ Comment.expects(:find).with('42').returns(mock_comment)
247
+ mock_comment.expects(:destroy)
248
+ delete :destroy, :id => '42', :post_id => '37'
249
+ assert_equal mock_post, assigns(:post)
250
+ assert_equal mock_comment, assigns(:comment)
251
+ end
252
+
253
+ def test_show_flash_message
254
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
255
+ Comment.stubs(:find).returns(mock_comment(:destroy => true))
256
+ delete :destroy
257
+ assert_equal flash[:notice], 'Comment was successfully destroyed.'
258
+ end
259
+
260
+ def test_redirects_to_comments_list
261
+ Post.stubs(:find).returns(mock_post(:comments => Comment))
262
+ Comment.stubs(:find).returns(mock_comment(:destroy => true))
263
+ @controller.expects(:collection_url).returns('http://test.host/')
264
+ delete :destroy
265
+ assert_redirected_to 'http://test.host/'
266
+ end
267
+ end
268
+
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class GreatSchool
4
+ end
5
+
6
+ class Professor
7
+ def self.human_name; 'Professor'; end
8
+ end
9
+
10
+ BELONGS_TO_OPTIONS = {
11
+ :parent_class => GreatSchool,
12
+ :instance_name => :great_school,
13
+ :finder => :find_by_title!,
14
+ :param => :school_title
15
+ }
16
+
17
+ class ProfessorsController < InheritedResources::Base
18
+ belongs_to :school, BELONGS_TO_OPTIONS
19
+ end
20
+
21
+ # Create a TestHelper module with some helpers
22
+ class BelongsToTest < Test::Unit::TestCase
23
+ def setup
24
+ @controller = ProfessorsController.new
25
+ @controller.request = @request = ActionController::TestRequest.new
26
+ @controller.response = @response = ActionController::TestResponse.new
27
+ end
28
+
29
+ def test_expose_the_resquested_school_with_chosen_instance_variable
30
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
31
+ Professor.stubs(:find).returns([mock_professor])
32
+ get :index, :school_title => 'nice'
33
+ assert_equal mock_school, assigns(:great_school)
34
+ end
35
+
36
+ def test_expose_the_resquested_school_with_chosen_instance_variable
37
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
38
+ Professor.stubs(:find).returns(mock_professor)
39
+ get :show, :school_title => 'nice'
40
+ assert_equal mock_school, assigns(:great_school)
41
+ end
42
+
43
+ def test_expose_the_resquested_school_with_chosen_instance_variable
44
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
45
+ Professor.stubs(:build).returns(mock_professor)
46
+ get :new, :school_title => 'nice'
47
+ assert_equal mock_school, assigns(:great_school)
48
+ end
49
+
50
+ def test_expose_the_resquested_school_with_chosen_instance_variable
51
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
52
+ Professor.stubs(:find).returns(mock_professor)
53
+ get :edit, :school_title => 'nice'
54
+ assert_equal mock_school, assigns(:great_school)
55
+ end
56
+
57
+ def test_expose_the_resquested_school_with_chosen_instance_variable
58
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
59
+ Professor.stubs(:build).returns(mock_professor(:save => true))
60
+ post :create, :school_title => 'nice'
61
+ assert_equal mock_school, assigns(:great_school)
62
+ end
63
+
64
+ def test_expose_the_resquested_school_with_chosen_instance_variable
65
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
66
+ Professor.stubs(:find).returns(mock_professor(:update_attributes => true))
67
+ put :update, :school_title => 'nice'
68
+ assert_equal mock_school, assigns(:great_school)
69
+ end
70
+
71
+ def test_expose_the_resquested_school_with_chosen_instance_variable
72
+ GreatSchool.expects(:find_by_title!).with('nice').returns(mock_school(:professors => Professor))
73
+ Professor.stubs(:find).returns(mock_professor(:destroy => true))
74
+ delete :destroy, :school_title => 'nice'
75
+ assert_equal mock_school, assigns(:great_school)
76
+ end
77
+
78
+ def test_belongs_to_raise_errors_with_invalid_arguments
79
+ assert_raise ArgumentError do
80
+ ProfessorsController.send(:belongs_to)
81
+ end
82
+
83
+ assert_raise ArgumentError do
84
+ ProfessorsController.send(:belongs_to, :arguments, :with_options, :parent_class => Professor)
85
+ end
86
+
87
+ assert_raise ArgumentError do
88
+ ProfessorsController.send(:belongs_to, :nice, :invalid_key => '')
89
+ end
90
+ end
91
+
92
+ def test_url_helpers_are_recreated_when_defaults_change
93
+ InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
94
+ ProfessorsController.send(:defaults, BELONGS_TO_OPTIONS)
95
+ ensure
96
+ # Reestore default settings
97
+ ProfessorsController.send(:parents_symbols=, [:school])
98
+ end
99
+
100
+ protected
101
+ def mock_school(stubs={})
102
+ @mock_school ||= mock(stubs)
103
+ end
104
+
105
+ def mock_professor(stubs={})
106
+ @mock_professor ||= mock(stubs)
107
+ end
108
+ end
109
+
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Book; end
4
+ class Folder; end
5
+
6
+ class BooksController < InheritedResources::Base
7
+ actions :index, :show
8
+ end
9
+
10
+ class ReadersController < InheritedResources::Base
11
+ actions :all, :except => [ :edit, :update ]
12
+ end
13
+
14
+ class FoldersController < InheritedResources::Base
15
+ end
16
+
17
+ class ActionsTest < Test::Unit::TestCase
18
+ def test_actions_are_undefined_when_only_option_is_given
19
+ action_methods = BooksController.send(:action_methods)
20
+ assert_equal 2, action_methods.size
21
+
22
+ ['index', 'show'].each do |action|
23
+ assert action_methods.include? action
24
+ end
25
+ end
26
+
27
+ def test_actions_are_undefined_when_except_option_is_given
28
+ action_methods = ReadersController.send(:action_methods)
29
+ assert_equal 5, action_methods.size
30
+
31
+ ['index', 'new', 'show', 'create', 'destroy'].each do |action|
32
+ assert action_methods.include? action
33
+ end
34
+ end
35
+ end
36
+
37
+ class DefaultsTest < Test::Unit::TestCase
38
+
39
  def test_resource_class_is_set_to_nil_when_resource_model_cannot_be_found
40
+ assert_nil ReadersController.send(:resource_class)
41
+ end
42
+
43
+ def test_defaults_are_set
44
+ assert Folder, FoldersController.send(:resource_class)
45
+ assert :folder, FoldersController.send(:resources_configuration)[:self][:instance_name]
46
+ assert :folders, FoldersController.send(:resources_configuration)[:self][:collection_name]
47
+ end
48
+
49
+ def test_defaults_can_be_overwriten
50
+ BooksController.send(:defaults, :resource_class => String, :instance_name => 'string', :collection_name => 'strings')
51
+
52
+ assert String, BooksController.send(:resource_class)
53
+ assert :string, BooksController.send(:resources_configuration)[:self][:instance_name]
54
+ assert :strings, BooksController.send(:resources_configuration)[:self][:collection_name]
55
+
56
+ BooksController.send(:defaults, :class_name => 'Fixnum', :instance_name => :fixnum, :collection_name => :fixnums)
57
+
58
+ assert String, BooksController.send(:resource_class)
59
+ assert :string, BooksController.send(:resources_configuration)[:self][:instance_name]
60
+ assert :strings, BooksController.send(:resources_configuration)[:self][:collection_name]
61
+ end
62
+
63
+ def test_defaults_raises_invalid_key
64
+ assert_raise ArgumentError do
65
+ BooksController.send(:defaults, :boom => String)
66
+ end
67
+ end
68
+
69
+ def test_url_helpers_are_recreated_when_defaults_change
70
+ InheritedResources::UrlHelpers.expects(:create_resources_url_helpers!).returns(true).once
71
+ BooksController.send(:defaults, :instance_name => 'string', :collection_name => 'strings')
72
+ end
73
+
74
+ end