rc_rails 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/.gitignore +7 -0
  2. data/CHANGELOG +355 -0
  3. data/Gemfile +5 -0
  4. data/Gemfile.lock.development +117 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +71 -0
  7. data/Rakefile +33 -0
  8. data/Todo.txt +1 -0
  9. data/lib/rc_rails.rb +9 -0
  10. data/lib/resources_controller/actions.rb +147 -0
  11. data/lib/resources_controller/active_record/saved.rb +15 -0
  12. data/lib/resources_controller/helper.rb +123 -0
  13. data/lib/resources_controller/include_actions.rb +37 -0
  14. data/lib/resources_controller/named_route_helper.rb +154 -0
  15. data/lib/resources_controller/railtie.rb +14 -0
  16. data/lib/resources_controller/request_path_introspection.rb +83 -0
  17. data/lib/resources_controller/resource_methods.rb +32 -0
  18. data/lib/resources_controller/singleton_actions.rb +21 -0
  19. data/lib/resources_controller/specification.rb +119 -0
  20. data/lib/resources_controller/version.rb +3 -0
  21. data/lib/resources_controller.rb +849 -0
  22. data/resources_controller.gemspec +29 -0
  23. data/spec/app/database.yml +5 -0
  24. data/spec/app/views/accounts/show.html.erb +0 -0
  25. data/spec/app/views/addresses/edit.html.erb +0 -0
  26. data/spec/app/views/addresses/index.html.erb +0 -0
  27. data/spec/app/views/addresses/new.html.erb +0 -0
  28. data/spec/app/views/addresses/show.html.erb +0 -0
  29. data/spec/app/views/admin/forums/create.html.erb +0 -0
  30. data/spec/app/views/admin/forums/destroy.html.erb +0 -0
  31. data/spec/app/views/admin/forums/edit.html.erb +0 -0
  32. data/spec/app/views/admin/forums/index.html.erb +0 -0
  33. data/spec/app/views/admin/forums/new.html.erb +0 -0
  34. data/spec/app/views/admin/forums/show.html.erb +0 -0
  35. data/spec/app/views/admin/forums/update.html.erb +0 -0
  36. data/spec/app/views/comments/edit.html.erb +0 -0
  37. data/spec/app/views/comments/index.html.erb +0 -0
  38. data/spec/app/views/comments/new.html.erb +0 -0
  39. data/spec/app/views/comments/show.html.erb +0 -0
  40. data/spec/app/views/forum_posts/edit.html.erb +0 -0
  41. data/spec/app/views/forum_posts/index.html.erb +0 -0
  42. data/spec/app/views/forum_posts/new.html.erb +0 -0
  43. data/spec/app/views/forum_posts/show.html.erb +0 -0
  44. data/spec/app/views/forums/create.html.erb +0 -0
  45. data/spec/app/views/forums/destroy.html.erb +0 -0
  46. data/spec/app/views/forums/edit.html.erb +0 -0
  47. data/spec/app/views/forums/index.html.erb +0 -0
  48. data/spec/app/views/forums/new.html.erb +0 -0
  49. data/spec/app/views/forums/show.html.erb +0 -0
  50. data/spec/app/views/forums/update.html.erb +0 -0
  51. data/spec/app/views/infos/edit.html.erb +0 -0
  52. data/spec/app/views/infos/show.html.erb +0 -0
  53. data/spec/app/views/interests/index.html.erb +0 -0
  54. data/spec/app/views/interests/show.html.erb +0 -0
  55. data/spec/app/views/owners/edit.html.erb +0 -0
  56. data/spec/app/views/owners/new.html.erb +0 -0
  57. data/spec/app/views/owners/show.html.erb +0 -0
  58. data/spec/app/views/tags/index.html.erb +0 -0
  59. data/spec/app/views/tags/new.html.erb +0 -0
  60. data/spec/app/views/tags/show.html.erb +0 -0
  61. data/spec/app/views/users/edit.html.erb +0 -0
  62. data/spec/app/views/users/index.html.erb +0 -0
  63. data/spec/app/views/users/show.html.erb +0 -0
  64. data/spec/app.rb +315 -0
  65. data/spec/controllers/accounts_controller_spec.rb +77 -0
  66. data/spec/controllers/addresses_controller_spec.rb +346 -0
  67. data/spec/controllers/admin_forums_controller_spec.rb +638 -0
  68. data/spec/controllers/comments_controller_spec.rb +380 -0
  69. data/spec/controllers/comments_controller_with_models_spec.rb +202 -0
  70. data/spec/controllers/forum_posts_controller_spec.rb +426 -0
  71. data/spec/controllers/forums_controller_spec.rb +694 -0
  72. data/spec/controllers/infos_controller_spec.rb +71 -0
  73. data/spec/controllers/interests_controller_via_forum_spec.rb +80 -0
  74. data/spec/controllers/interests_controller_via_user_spec.rb +114 -0
  75. data/spec/controllers/owners_controller_spec.rb +277 -0
  76. data/spec/controllers/resource_saved_spec.rb +47 -0
  77. data/spec/controllers/resource_service_in_forums_controller_spec.rb +37 -0
  78. data/spec/controllers/resource_service_in_infos_controller_spec.rb +36 -0
  79. data/spec/controllers/resource_service_in_interests_controller_via_forum_spec.rb +51 -0
  80. data/spec/controllers/tags_controller_spec.rb +83 -0
  81. data/spec/controllers/tags_controller_via_account_info_spec.rb +131 -0
  82. data/spec/controllers/tags_controller_via_forum_post_comment_spec.rb +144 -0
  83. data/spec/controllers/tags_controller_via_forum_post_spec.rb +133 -0
  84. data/spec/controllers/tags_controller_via_forum_spec.rb +173 -0
  85. data/spec/controllers/tags_controller_via_user_address_spec.rb +130 -0
  86. data/spec/controllers/users_controller_spec.rb +248 -0
  87. data/spec/lib/action_view_helper_spec.rb +143 -0
  88. data/spec/lib/bug_0001_spec.rb +22 -0
  89. data/spec/lib/include_actions_spec.rb +35 -0
  90. data/spec/lib/load_enclosing_resources_spec.rb +245 -0
  91. data/spec/lib/request_path_introspection_spec.rb +130 -0
  92. data/spec/lib/resource_methods_spec.rb +204 -0
  93. data/spec/lib/resources_controller_spec.rb +57 -0
  94. data/spec/models/comment_saved_spec.rb +24 -0
  95. data/spec/rspec_generator_task.rb +105 -0
  96. data/spec/spec_helper.rb +17 -0
  97. data/spec/verify_rcov.rb +52 -0
  98. metadata +193 -0
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ module Bug0001Spec
4
+ class Thing < ActiveRecord::Base
5
+ end
6
+
7
+ class MyController < ActionController::Base
8
+ def respond_to_with_cruft?(method)
9
+ respond_to_without_cruft?(method)
10
+ end
11
+ alias_method_chain :respond_to?, :cruft
12
+
13
+ resources_controller_for :things, :class => Thing
14
+ end
15
+
16
+ describe "Calling respond_to? when it has an old signature buried in there [#1]" do
17
+ it "should work just fine" do
18
+ c = MyController.new
19
+ c.respond_to?(:foo).should == false
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ module IncludeActionsSpec
4
+ module Actions
5
+ def foo; end
6
+ def bar; end
7
+ def faz; end
8
+ end
9
+
10
+ class ActionsController < ActionController::Base
11
+ include_actions Actions
12
+ end
13
+
14
+ class OnlyFooController < ActionController::Base
15
+ include_actions Actions, :only => :foo
16
+ end
17
+
18
+ class ExceptFooBarController < ActionController::Base
19
+ include_actions Actions, :except => [:foo, :bar]
20
+ end
21
+
22
+ describe "Include actions use case" do
23
+ it "ActionController should have actions from actions module" do
24
+ ActionsController.action_methods.should == ['foo', 'bar', 'faz'].to_set
25
+ end
26
+
27
+ it "OnlyFooController should have only :foo from actions module" do
28
+ OnlyFooController.action_methods.should == ['foo'].to_set
29
+ end
30
+
31
+ it "ExceptFooBarController should not have :foo, :bar from actions module" do
32
+ ExceptFooBarController.action_methods.should == ['faz'].to_set
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,245 @@
1
+ require 'spec_helper'
2
+
3
+ module LoadEnclosingResourcesSpecHelper
4
+ def setup_tags_controller(options = {})
5
+ @klass = Class.new(ActionController::Base)
6
+ @klass.stub!(:anonymous?).and_return(false)
7
+ @klass.resources_controller_for :tags, options
8
+ @klass.stub!(:name).and_return 'ATestController'
9
+ setup_common
10
+ end
11
+
12
+ def setup_common
13
+ @controller = @klass.new
14
+ @controller.stub!(:request_path).and_return('')
15
+ # stub :load_enclosing_resource_from_specification, increase enclosing_resources by one, and return a mock resource
16
+ @controller.stub!(:load_enclosing_resource_from_specification).and_return do |name, _|
17
+ mock("resource: #{name}").tap do |resource|
18
+ @controller.enclosing_resources << resource
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "#load_enclosing_resources for resources_controller_for :tags (when nesting_segments is [{:segment => 'users', :singleton => false}])" do
25
+ include LoadEnclosingResourcesSpecHelper
26
+
27
+ before do
28
+ setup_tags_controller
29
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'users', :singleton => false}]
30
+ end
31
+
32
+ it "should call load_wildcard once" do
33
+ @controller.should_receive(:load_wildcard).once
34
+ @controller.send(:load_enclosing_resources)
35
+ end
36
+
37
+ it "should call Specification.new('user', :singleton => false, :as => nil)" do
38
+ ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil)
39
+ @controller.send(:load_enclosing_resources)
40
+ end
41
+ end
42
+
43
+ describe "#load_enclosing_resources for resources_controller_for :tags, with :account mapping (when nesting_segments is [{:segment => 'account', :singleton => true}])" do
44
+ include LoadEnclosingResourcesSpecHelper
45
+
46
+ before do
47
+ setup_tags_controller
48
+ @klass.map_resource :account, :singleton => true, :class => User
49
+ @account_spec = @controller.resource_specification_map['account']
50
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'account', :singleton => true}]
51
+ end
52
+
53
+ it "should call load_wildcard once" do
54
+ @controller.should_receive(:load_wildcard).once
55
+ @controller.send(:load_enclosing_resources)
56
+ end
57
+
58
+ it "should call load_enclosing_resource_from_specification with account specification" do
59
+ @controller.should_receive(:load_enclosing_resource_from_specification).with(@account_spec)
60
+ @controller.send(:load_enclosing_resources)
61
+ end
62
+
63
+ it "should not call Specification.new" do
64
+ ResourcesController::Specification.should_not_receive(:new)
65
+ @controller.send(:load_enclosing_resources)
66
+ end
67
+ end
68
+
69
+ describe "#load_enclosing_resources for resources_controller_for :tags (when nesting_segments is [{:segment =>'users', :singleton => false}, {:segment =>'forums', :singleton => false}])" do
70
+ include LoadEnclosingResourcesSpecHelper
71
+
72
+ before do
73
+ setup_tags_controller
74
+ @controller.stub!(:nesting_segments).and_return [{:segment =>'users', :singleton => false}, {:segment => 'forums', :singleton => false}]
75
+ end
76
+
77
+ it "should call load_wildcard twice" do
78
+ @controller.should_receive(:load_wildcard).twice
79
+ @controller.send(:load_enclosing_resources)
80
+ end
81
+
82
+ it "should call Specification.new with ('user', :singleton => false, :as => nil), then ('forum', :singleton => false, :as => nil)" do
83
+ ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).ordered
84
+ ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).ordered
85
+ @controller.send(:load_enclosing_resources)
86
+ end
87
+ end
88
+
89
+ describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', :comment] (when nesting_segments is [{:segment => 'comments', :singleton => false}])" do
90
+ include LoadEnclosingResourcesSpecHelper
91
+
92
+ before do
93
+ setup_tags_controller :in => ['*', :comment]
94
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'comments', :singleton => false}]
95
+ end
96
+
97
+ it "should not call load_wildcard" do
98
+ @controller.should_not_receive(:load_wildcard)
99
+ @controller.send(:load_enclosing_resources)
100
+ end
101
+
102
+ it "should not call Specification.new" do
103
+ ResourcesController::Specification.should_not_receive(:new)
104
+ @controller.send(:load_enclosing_resources)
105
+ end
106
+ end
107
+
108
+ describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', :comment] (when nesting_segments is [{:segment => 'users', :singleton => false}, {:segment => 'forums', :singleton => false}, {:segment =>'comments', :singleton => false}])" do
109
+ include LoadEnclosingResourcesSpecHelper
110
+
111
+ before do
112
+ setup_tags_controller :in => ['*', :comment]
113
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'users', :singleton => false}, {:segment => 'forums', :singleton => false}, {:segment =>'comments', :singleton => false}]
114
+ end
115
+
116
+ it "should call load_wildcard twice" do
117
+ @controller.should_receive(:load_wildcard).twice
118
+ @controller.send(:load_enclosing_resources)
119
+ end
120
+
121
+ it "should call Specification.new with ('user', :singleton => false, :as => nil), then ('forum', :singleton => false, :as => nil)" do
122
+ ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).ordered
123
+ ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).ordered
124
+ @controller.send(:load_enclosing_resources)
125
+ end
126
+ end
127
+
128
+ describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', '?commentable', :comment] (when nesting_segments is [{:segment => 'users', :singleton => false}, {:segment => 'comments', :singleton => false}])" do
129
+ include LoadEnclosingResourcesSpecHelper
130
+
131
+ before do
132
+ setup_tags_controller :in => ['*', '?commentable', :comment]
133
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'users', :singleton => false}, {:segment => 'comments', :singleton => false}]
134
+ end
135
+
136
+ it "should call load_wildcard once with 'commentable'" do
137
+ @controller.should_receive(:load_wildcard).with('commentable').once
138
+ @controller.send(:load_enclosing_resources)
139
+ end
140
+
141
+ it "should call Specification.new with ('user', :singleton => false, :as => 'commentable')" do
142
+ ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => 'commentable').ordered
143
+ @controller.send(:load_enclosing_resources)
144
+ end
145
+ end
146
+
147
+ describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', '?commentable', :comment] (when nesting_segments is [{:segment => 'users', :singleton => false}, {:segment => 'forums', :singleton => false}, {:segment => 'comments', :singleton => false}])" do
148
+ include LoadEnclosingResourcesSpecHelper
149
+
150
+ before do
151
+ setup_tags_controller :in => ['*', '?commentable', :comment]
152
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'users', :singleton => false}, {:segment => 'forums', :singleton => false}, {:segment => 'comments', :singleton => false}]
153
+ end
154
+
155
+ it "should call load_wildcard twice" do
156
+ @controller.should_receive(:load_wildcard).with().once.ordered
157
+ @controller.should_receive(:load_wildcard).with('commentable').once.ordered
158
+ @controller.send(:load_enclosing_resources)
159
+ end
160
+
161
+ it "should call Specification.new with ('user', :singleton => false, :as => nil), ('forum', :singleton => false, :as => 'commentable')" do
162
+ ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).once.ordered
163
+ ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => 'commentable').once.ordered
164
+ @controller.send(:load_enclosing_resources)
165
+ end
166
+ end
167
+
168
+ describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', '?commentable', :comment] (when nesting_segments is [{:segment => 'users', :singleton => false}, {:segment => 'forums', :singleton => false}, {:segment => 'posts', :singleton => false}, {:segment => 'comments', :singleton => false}])" do
169
+ include LoadEnclosingResourcesSpecHelper
170
+
171
+ before do
172
+ setup_tags_controller :in => ['*', '?commentable', :comment]
173
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'users', :singleton => false}, {:segment => 'forums', :singleton => false}, {:segment => 'posts', :singleton => false}, {:segment => 'comments', :singleton => false}]
174
+ end
175
+
176
+ it "should call load_wildcard twice, then once with 'commentable'" do
177
+ @controller.should_receive(:load_wildcard).with().twice.ordered
178
+ @controller.should_receive(:load_wildcard).with('commentable').once.ordered
179
+ @controller.send(:load_enclosing_resources)
180
+ end
181
+
182
+ it "should call Specification.new with ('user', :singleton => false, :as => nil), ('forum', :singleton => false, :as => nil), then ('post', :singleton => false, :as => 'commentable')" do
183
+ ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).once.ordered
184
+ ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).once.ordered
185
+ ResourcesController::Specification.should_receive(:new).with('post', :singleton => false, :as => 'commentable').once.ordered
186
+ @controller.send(:load_enclosing_resources)
187
+ end
188
+ end
189
+
190
+ describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['user', '*', '?taggable'] (when nesting_segments is [{:segment => 'users', :singleton => false}, {:segment => 'comments', :singleton => false}])" do
191
+ include LoadEnclosingResourcesSpecHelper
192
+
193
+ before do
194
+ setup_tags_controller :in => ['user', '*', '?taggable']
195
+ @user_spec = @controller.send(:specifications)[1]
196
+ @controller.stub!(:nesting_segments).and_return [{:segment => 'users', :singleton => false}, {:segment => 'comments', :singleton => false}]
197
+ end
198
+
199
+ it "should call load_enclosing_resource_from_specification with user spec, then load_wildcard once with 'taggable'" do
200
+ @controller.should_receive(:load_enclosing_resource_from_specification).with(@user_spec).once.ordered do
201
+ mock('user').tap {|r| @controller.enclosing_resources << r }
202
+ end
203
+ @controller.should_receive(:load_wildcard).with('taggable').once.ordered
204
+ @controller.send(:load_enclosing_resources)
205
+ end
206
+
207
+ it "should call Specification.new with ('comment', :singleton => false, :as => 'taggable')" do
208
+ ResourcesController::Specification.should_receive(:new).with('comment', :singleton => false, :as => 'taggable')
209
+ @controller.send(:load_enclosing_resources)
210
+ end
211
+ end
212
+
213
+ # specing some branching BC code
214
+ # find_filter dissapeared from edge, but we want to support it for 2.0-stable
215
+ describe "ResourcesController.load_enclosing_resources_filter_exists?" do
216
+ before do
217
+ @klass = Class.new(ActionController::Base)
218
+ end
219
+
220
+ describe "when :find_filter defined" do
221
+ before do
222
+ class<<@klass
223
+ def find_filter; end
224
+ end
225
+ end
226
+
227
+ it "should call :find_filter with :load_enclosing_resources" do
228
+ @klass.should_receive(:find_filter).with(:load_enclosing_resources)
229
+ @klass.send(:load_enclosing_resources_filter_exists?)
230
+ end
231
+ end
232
+
233
+ describe "when :find_filter not defined" do
234
+ before do
235
+ class<<@klass
236
+ undef_method(:find_filter) rescue nil
237
+ end
238
+ end
239
+
240
+ it "should call :_process_action_callbacks" do
241
+ @klass.should_receive(:_process_action_callbacks).and_return([])
242
+ @klass.send(:load_enclosing_resources_filter_exists?)
243
+ end
244
+ end
245
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ module RequestPathIntrospectionSpec
4
+ class Forum < ActiveRecord::Base; end
5
+
6
+ class User < ActiveRecord::Base; end
7
+
8
+ describe "RequestPathIntrospection" do
9
+ before do
10
+ @klass = Class.new(ActionController::Base)
11
+ @controller = @klass.new
12
+ @controller.stub!(:controller_name).and_return('forums')
13
+ @controller.stub!(:controller_path).and_return('forums')
14
+ @controller.stub!(:params).and_return({})
15
+ @controller.stub!(:request).and_return(mock('request', :path => '/forums'))
16
+ end
17
+
18
+ describe "#request_path" do
19
+ it "should default to request.path" do
20
+ @controller.send(:request_path).should == '/forums'
21
+ end
22
+
23
+ it " should be params[:resource_path], when set" do
24
+ @controller.params[:resource_path] = '/foo'
25
+ @controller.send(:request_path).should == '/foo'
26
+ end
27
+ end
28
+
29
+ describe "#nesting_request_path" do
30
+ it "should remove the controller_name segment" do
31
+ @controller.stub!(:request_path).and_return('/users/1/forums/2')
32
+ @controller.send(:nesting_request_path).should == '/users/1'
33
+ end
34
+
35
+ it "when resource_specification present, whould remove taht segment" do
36
+ @controller.stub!(:resource_specification).and_return(ResourcesController::Specification.new(:forum, :class => RequestPathIntrospectionSpec::Forum, :segment => 'foromas'))
37
+ @controller.stub!(:request_path).and_return('/users/1/foromas/2')
38
+ @controller.send(:nesting_request_path).should == '/users/1'
39
+ end
40
+
41
+ it "should remove only the controller_name segment, when nesting is same name" do
42
+ @controller.stub!(:request_path).and_return('/forums/1/forums/2')
43
+ @controller.send(:nesting_request_path).should == '/forums/1'
44
+ end
45
+
46
+ it "should remove the controller_name segment, even when id matches controller name" do
47
+ @controller.stub!(:request_path).and_return('/forums/1/forums/forums.atom')
48
+ @controller.send(:nesting_request_path).should == '/forums/1'
49
+ end
50
+
51
+ it "should remove only the controller_name segment even when nesting is same name" do
52
+ @controller.stub!(:resource_specification).and_return(ResourcesController::Specification.new(:forum, :class => RequestPathIntrospectionSpec::Forum, :singleton => true))
53
+ @controller.stub!(:request_path).and_return('/users/1/forum/forum.atom')
54
+ @controller.send(:nesting_request_path).should == '/users/1/forum'
55
+ end
56
+
57
+ it "should remove any controller namespace" do
58
+ @controller.stub!(:controller_path).and_return('some/name/space/forums')
59
+ @controller.stub!(:request_path).and_return('/some/name/space/users/1/secret/forums')
60
+ @controller.send(:nesting_request_path).should == '/users/1/secret'
61
+ end
62
+ end
63
+
64
+ it "#namespace_segments should return [] segments if NOT present in request_path" do
65
+ @controller.stub!(:controller_path).and_return('some/name/space/forums')
66
+ @controller.stub!(:request_path).and_return('/SAM/name/space/users/1/secret/forums')
67
+ @controller.send(:namespace_segments).should == []
68
+ end
69
+
70
+ it "#namespace_segments should return namespace segments if present in request_path" do
71
+ @controller.stub!(:controller_path).and_return('some/name/space/forums')
72
+ @controller.stub!(:request_path).and_return('/some/name/space/users/1/secret/forums')
73
+ @controller.send(:namespace_segments).should == ['some', 'name', 'space']
74
+ end
75
+
76
+ describe "#nesting_segments" do
77
+ describe "when params include :user_id" do
78
+ before do
79
+ @controller.params[:user_id] = '1'
80
+ end
81
+
82
+ it "and request path is '/users/1/forums', should return [{:segment => 'users', :singleton => false}]" do
83
+ @controller.request.stub!(:path).and_return('/users/1/forums')
84
+ @controller.send(:nesting_segments).should == [{:segment => 'users', :singleton => false}]
85
+ end
86
+
87
+ it "and request path is '/account/users/1/forums', should return [{:segment => 'account', :singleton => true}, {:segment => 'users', :singleton => false}]" do
88
+ @controller.request.stub!(:path).and_return('/account/users/1/forums')
89
+ @controller.send(:nesting_segments).should == [{:segment => 'account', :singleton => true}, {:segment => 'users', :singleton => false}]
90
+ end
91
+
92
+ describe "when controller has nesting for :user => 'muchachos'" do
93
+ before do
94
+ @klass.resources_controller_for :forums, :class => RequestPathIntrospectionSpec::Forum
95
+ @klass.nested_in :user, :segment => "muchachos", :class => RequestPathIntrospectionSpec::User
96
+ end
97
+
98
+ it "and request path is '/muchachos/1/forums', should return [{:segment => 'muchachos', :singleton => false}]" do
99
+ @controller.request.stub!(:path).and_return('/muchachos/1/forums')
100
+ @controller.send(:nesting_segments).should == [{:segment => 'muchachos', :singleton => false}]
101
+ end
102
+ end
103
+
104
+ describe "when enclosing resource has mapping for :user => 'muchachos'" do
105
+ before do
106
+ @klass.map_enclosing_resource :user, :segment => "muchachos", :class => RequestPathIntrospectionSpec::User
107
+ end
108
+
109
+ it "and request path is '/muchachos/1/forums', should return [{:segment => 'muchachos', :singleton => false}]" do
110
+ @controller.request.stub!(:path).and_return('/muchachos/1/forums')
111
+ @controller.send(:nesting_segments).should == [{:segment => 'muchachos', :singleton => false}]
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ describe "#segment_for_key" do
118
+ describe "when controller has map {:user, :singelton => true}" do
119
+ before do
120
+ @klass.resources_controller_for :forums, :class => RequestPathIntrospectionSpec::Forum
121
+ @klass.map_enclosing_resource :user, :singleton => true, :class => RequestPathIntrospectionSpec::User
122
+ end
123
+
124
+ it "('user_id') should return 'users'" do
125
+ @controller.send(:segment_for_key, 'user_id').should == 'users'
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,204 @@
1
+ require 'spec_helper'
2
+
3
+ module ResourceMethodsSpec
4
+ class MyController < ActionController::Base
5
+ resources_controller_for :users
6
+ end
7
+
8
+ if Rails.respond_to?(:version) && Rails.version >= "2.3"
9
+ describe "#new_resource" do
10
+ it "should accept block syntax" do
11
+ c = MyController.new
12
+ c.resource_service = User
13
+ c.stub!(:params).and_return({})
14
+ r = c.send(:new_resource) do |u|
15
+ u.login = "Fred"
16
+ end
17
+ r.should be_kind_of User
18
+ r.login.should == "Fred"
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "An rc for collection :users" do
24
+ before do
25
+ @controller = MyController.new
26
+ @controller.stub!(:params).and_return({})
27
+ end
28
+
29
+ describe "when no enclosing resource" do
30
+ it "#find_resource(<id>) should call User.find(<id>)" do
31
+ User.should_receive(:find).with("42")
32
+ @controller.send(:find_resource, "42")
33
+ end
34
+
35
+ it "#find_resources should call User.find(:all)" do
36
+ User.should_receive(:all)
37
+ @controller.send(:find_resources)
38
+ end
39
+
40
+ it "#new_resource({}) should call User.new({})" do
41
+ User.should_receive(:new).with({})
42
+ @controller.send(:new_resource, {})
43
+ end
44
+
45
+ it "#destroy_resource(<id>) should call User.find(<id>).destroy" do
46
+ User.should_receive(:find).with("42").and_return(user = mock)
47
+ user.should_receive(:destroy).and_return(user)
48
+ @controller.send(:destroy_resource, "42").should == user
49
+ end
50
+ end
51
+
52
+ describe "when an enclosing resource is added (a forum)" do
53
+ before do
54
+ @forum = Forum.create!
55
+ @controller.send :add_enclosing_resource, @forum
56
+ end
57
+
58
+ it "#find_resource(<id>) should call forum.users.find(<id>)" do
59
+ @forum.users.should_receive(:find).with("42")
60
+ @controller.send(:find_resource, "42")
61
+ end
62
+
63
+ it "#find_resources should call forum.users.find(:all)" do
64
+ @forum.users.should_receive(:all)
65
+ @controller.send(:find_resources)
66
+ end
67
+
68
+ it "#new_resource({}) should call forum.users.build({})" do
69
+ @forum.users.should_receive(:build).with({})
70
+ @controller.send :new_resource, {}
71
+ end
72
+
73
+ it "#destroy_resource(<id>) should call forum.users.find(<id>) and forum.users.destroy(<id>)" do
74
+ @forum.users.should_receive(:find).with("42").and_return(user = mock)
75
+ @forum.users.should_receive(:destroy).with("42").and_return(useless_array = mock)
76
+ @controller.send(:destroy_resource, "42").should == user
77
+ end
78
+ end
79
+ end
80
+
81
+ class MySingletonController < ActionController::Base
82
+ resources_controller_for :info, :singleton => true
83
+ end
84
+
85
+ describe "An rc for singleton :info" do
86
+ before do
87
+ @controller = MySingletonController.new
88
+ @controller.stub!(:params).and_return({})
89
+ end
90
+
91
+ describe "with an enclosing resource (a user)" do
92
+ before do
93
+ @user = User.create!
94
+ @controller.send :add_enclosing_resource, @user
95
+ end
96
+
97
+ it "#find_resource should call user.info" do
98
+ @user.should_receive(:info)
99
+ @controller.send(:find_resource)
100
+ end
101
+
102
+ it "#new_resource({}) should call user.build_info({})" do
103
+ @user.should_receive(:build_info).with({})
104
+ @controller.send :new_resource, {}
105
+ end
106
+
107
+ it "#destroy_resource should call user.info.destroy" do
108
+ @user.should_receive(:info).and_return(info = mock)
109
+ info.should_receive(:destroy)
110
+ @controller.send(:destroy_resource)
111
+ end
112
+ end
113
+ end
114
+
115
+ module MyResourceMethods
116
+ protected
117
+ def new_resource(attrs = (params[resource_name] || {}), &block)
118
+ "my new_resource"
119
+ end
120
+
121
+ def find_resource(id = params[:id])
122
+ "my find_resource"
123
+
124
+ end
125
+
126
+ def find_resources
127
+ "my find_resources"
128
+ end
129
+
130
+ def destroy_resource(id = params[:id])
131
+ "my destroy_resource"
132
+ end
133
+ end
134
+
135
+ shared_examples_for "A controller with its own resource methods" do
136
+ it "#new_resource should call MyResourceMethods#new_resource" do
137
+ @controller.send(:new_resource, {}).should == 'my new_resource'
138
+ end
139
+
140
+ it "#find_resource should call MyResourceMethods#find_resource" do
141
+ @controller.send(:find_resource, 1).should == 'my find_resource'
142
+ end
143
+
144
+ it "#find_resources should call MyResourceMethods#new_resource" do
145
+ @controller.send(:find_resources).should == 'my find_resources'
146
+ end
147
+
148
+ it "#destroy_resource should call MyResourceMethods#destroy_resource" do
149
+ @controller.send(:destroy_resource, 1).should == 'my destroy_resource'
150
+ end
151
+ end
152
+
153
+ class MyControllerWithMyResourceMethodsMixedIn < ActionController::Base
154
+ resources_controller_for :users
155
+ include MyResourceMethods
156
+ end
157
+
158
+ describe "A controller with resource methods mixed in after resources_controller_for" do
159
+ before do
160
+ @controller = MyControllerWithMyResourceMethodsMixedIn.new
161
+ @controller.resource_service = User
162
+ @controller.stub!(:params).and_return({})
163
+ end
164
+
165
+ it_should_behave_like "A controller with its own resource methods"
166
+ end
167
+
168
+ class MyAbstractControllerWithOwnResourceMethods < ActionController::Base
169
+ include MyResourceMethods
170
+ end
171
+
172
+ class InhertedMyAbstractControllerWithOwnResourceMethods < MyAbstractControllerWithOwnResourceMethods
173
+ resources_controller_for :users, :resource_methods => false
174
+ end
175
+
176
+ describe "A controller inheriting resource methods which declares :resource_methods => false" do
177
+ before do
178
+ @controller = InhertedMyAbstractControllerWithOwnResourceMethods.new
179
+ @controller.resource_service = User
180
+ @controller.stub!(:params).and_return({})
181
+ end
182
+
183
+ it_should_behave_like "A controller with its own resource methods"
184
+ end
185
+
186
+ class MyAbstractControllerWithResourceMethodsOverridden < ActionController::Base
187
+ include ResourcesController::ResourceMethods
188
+ include MyResourceMethods
189
+ end
190
+
191
+ class InhertedMyAbstractControllerWithResourceMethodsOverridden < MyAbstractControllerWithResourceMethodsOverridden
192
+ resources_controller_for :users
193
+ end
194
+
195
+ describe "A controller inheriting from a controller which mixes in ResourceMethods, and overrides them" do
196
+ before do
197
+ @controller = InhertedMyAbstractControllerWithResourceMethodsOverridden.new
198
+ @controller.resource_service = User
199
+ @controller.stub!(:params).and_return({})
200
+ end
201
+
202
+ it_should_behave_like "A controller with its own resource methods"
203
+ end
204
+ end