responder_controller 0.2.0 → 0.3.0
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.
- data/CHANGELOG +6 -0
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/responder_controller.rb +4 -297
- data/lib/responder_controller/actions.rb +57 -0
- data/lib/responder_controller/class_methods.rb +126 -0
- data/lib/responder_controller/instance_methods.rb +118 -0
- data/responder_controller.gemspec +14 -5
- data/spec/responder_controller/actions_spec.rb +140 -0
- data/spec/responder_controller/class_methods_spec.rb +266 -0
- data/spec/responder_controller/instance_methods_spec.rb +239 -0
- data/spec/responder_controller_spec.rb +2 -586
- metadata +13 -4
@@ -0,0 +1,239 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Post
|
4
|
+
class <<self
|
5
|
+
def scopes
|
6
|
+
{
|
7
|
+
:recent => "a scope",
|
8
|
+
:unpublished => "another",
|
9
|
+
:authored_by => "and another",
|
10
|
+
:commented_on_by => "even one more",
|
11
|
+
:published_after => "man they just keep coming"
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ResponderController::InstanceMethods do
|
18
|
+
|
19
|
+
class ApplicationController
|
20
|
+
include ResponderController
|
21
|
+
|
22
|
+
def params
|
23
|
+
@params ||= { :user_id => 'me', :id => 7 }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class PostsController < ApplicationController
|
28
|
+
end
|
29
|
+
|
30
|
+
class Admin::SettingsController < ApplicationController
|
31
|
+
end
|
32
|
+
|
33
|
+
before :each do
|
34
|
+
@query = mock("the scoped query")
|
35
|
+
@query.stub!(:unpublished).and_return(@query)
|
36
|
+
@query.stub!(:recent).and_return(@query)
|
37
|
+
@query.stub!(:owned_by).with('me').and_return(@query)
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#model_class_name' do
|
41
|
+
it "is .model_class_name" do
|
42
|
+
PostsController.new.model_class_name.should == PostsController.model_class_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#model_class' do
|
47
|
+
it 'is .model_class' do
|
48
|
+
PostsController.new.model_class.should == PostsController.model_class
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#scopes' do
|
53
|
+
it 'is .scopes' do
|
54
|
+
PostsController.new.scopes.should == PostsController.scopes
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#scope' do
|
59
|
+
it "passes its argument out by default" do
|
60
|
+
PostsController.new.scope(@query).should == @query
|
61
|
+
end
|
62
|
+
|
63
|
+
it "with explicit scopes asks model_class for the declared scopes in order" do
|
64
|
+
PostsController.scope :unpublished
|
65
|
+
PostsController.scope :recent
|
66
|
+
|
67
|
+
@query.should_receive(:unpublished).and_return(@query)
|
68
|
+
@query.should_receive(:recent).and_return(@query)
|
69
|
+
PostsController.new.scope(@query).should == @query
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'with a block scope instance_execs the block while passing in the current query' do
|
73
|
+
PostsController.scope do |posts|
|
74
|
+
posts.owned_by(params[:user_id])
|
75
|
+
end
|
76
|
+
|
77
|
+
@query.should_receive(:owned_by).with('me').and_return(@query)
|
78
|
+
|
79
|
+
controller = PostsController.new
|
80
|
+
controller.scope(@query).should == @query
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'explodes with an unknown scope' do
|
84
|
+
PostsController.scope :furst_p0sts
|
85
|
+
|
86
|
+
lambda do
|
87
|
+
PostsController.new.scope
|
88
|
+
end.should raise_error ArgumentError
|
89
|
+
|
90
|
+
PostsController.scopes.pop
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'with request parameters naming scopes' do
|
94
|
+
before :each do
|
95
|
+
@controller = PostsController.new
|
96
|
+
@controller.params['commented_on_by'] = 'you'
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'applies the requested scopes in order' do
|
100
|
+
@query.should_receive(:commented_on_by).with('you').and_return(@query)
|
101
|
+
@controller.scope @query
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'is applied after class-level scopes' do
|
105
|
+
class_level_query = mock("class-level scoped query")
|
106
|
+
@query.should_receive(:owned_by).and_return(class_level_query) # last class-level scope
|
107
|
+
|
108
|
+
class_level_query.should_receive(:commented_on_by).with('you').and_return(class_level_query)
|
109
|
+
@controller.scope(@query).should == class_level_query
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'throws BadScope for scopes that raise an exception' do
|
113
|
+
@query.should_receive(:commented_on_by).and_raise(ArgumentError.new)
|
114
|
+
lambda do
|
115
|
+
@controller.scope @query
|
116
|
+
end.should raise_error(ResponderController::BadScope)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#find_models' do
|
122
|
+
it 'is #scope #model_class.scoped' do
|
123
|
+
controller = PostsController.new
|
124
|
+
|
125
|
+
Post.should_receive(:scoped).and_return(@query)
|
126
|
+
controller.should_receive(:scope).with(@query).and_return(@query)
|
127
|
+
|
128
|
+
controller.find_models.should == @query
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '#find_model' do
|
133
|
+
it 'is #find_models.find(params[:id])' do
|
134
|
+
controller = PostsController.new
|
135
|
+
controller.should_receive(:find_models).and_return(@query)
|
136
|
+
@query.should_receive(:find).with(controller.params[:id]).and_return(post = mock("the post"))
|
137
|
+
|
138
|
+
controller.find_model.should == post
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe '#model_slug' do
|
143
|
+
it 'is the model class name' do
|
144
|
+
PostsController.new.model_slug.should == :post
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'drops the leading module names, if any' do
|
148
|
+
Admin::SettingsController.new.model_slug.should == :setting
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe '#models_slug' do
|
153
|
+
it 'is ths symbolized plural of #model_slug' do
|
154
|
+
PostsController.new.models_slug.should == :posts
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#model_ivar' do
|
159
|
+
it 'is the #model_slug with a leading @' do
|
160
|
+
PostsController.new.model_ivar.should == '@post'
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe '#models_ivar' do
|
165
|
+
it 'is the plural #model_ivar' do
|
166
|
+
(controller = PostsController.new).models_ivar.should == controller.model_ivar.pluralize
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "#models" do
|
171
|
+
it "gets #models_ivar" do
|
172
|
+
(controller = PostsController.new).instance_variable_set("@posts", :some_posts)
|
173
|
+
controller.models.should == :some_posts
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
describe "#model" do
|
178
|
+
it "gets #model_ivar" do
|
179
|
+
(controller = PostsController.new).instance_variable_set("@post", :a_post)
|
180
|
+
controller.model.should == :a_post
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "#models=" do
|
185
|
+
it "assigns to #models_ivar" do
|
186
|
+
assigned = mock("some models")
|
187
|
+
(controller = PostsController.new).models = assigned
|
188
|
+
controller.instance_variable_get("@posts").should == assigned
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe "#model=" do
|
193
|
+
it "assigns to #model_ivar" do
|
194
|
+
assigned = mock("a model")
|
195
|
+
(controller = PostsController.new).model = assigned
|
196
|
+
controller.instance_variable_get("@post").should == assigned
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe '#responds_within' do
|
201
|
+
it 'is .responds_within' do
|
202
|
+
PostsController.new.responds_within.should == PostsController.responds_within
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe '#responder_context' do
|
207
|
+
it "is the argument prepended with responds_within" do
|
208
|
+
Admin::SettingsController.new.responder_context(:argument).should == [:admin, :argument]
|
209
|
+
end
|
210
|
+
|
211
|
+
it "passes the argument to responds_within and prepends the result if it is a lambda" do
|
212
|
+
Admin::SettingsController.responds_within do |model|
|
213
|
+
model.should == :argument
|
214
|
+
[:nested, :namespace]
|
215
|
+
end
|
216
|
+
|
217
|
+
Admin::SettingsController.new.responder_context(:argument).should == [:nested, :namespace, :argument]
|
218
|
+
end
|
219
|
+
|
220
|
+
it "wraps the lambda result in an array if needed" do
|
221
|
+
Admin::SettingsController.responds_within { |model| :namespace }
|
222
|
+
Admin::SettingsController.new.responder_context(:argument).should == [:namespace, :argument]
|
223
|
+
end
|
224
|
+
|
225
|
+
after :each do
|
226
|
+
Admin::SettingsController.instance_variable_set "@responds_within", nil
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe '#respond_with_contextual' do
|
231
|
+
it 'passed #responder_context to #respond_with' do
|
232
|
+
controller = PostsController.new
|
233
|
+
controller.should_receive(:responder_context).with(:argument).and_return([:contextualized_argument])
|
234
|
+
controller.should_receive(:respond_with).with(:contextualized_argument)
|
235
|
+
|
236
|
+
controller.respond_with_contextual :argument
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
@@ -1,588 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
class <<self
|
5
|
-
def scopes
|
6
|
-
{
|
7
|
-
:recent => "a scope",
|
8
|
-
:unpublished => "another",
|
9
|
-
:authored_by => "and another",
|
10
|
-
:commented_on_by => "even one more",
|
11
|
-
:published_after => "man they just keep coming"
|
12
|
-
}
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
module Accounts
|
18
|
-
class User
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
module Admin
|
23
|
-
class Setting
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "ResponderController" do
|
28
|
-
|
29
|
-
class ApplicationController
|
30
|
-
include ResponderController
|
31
|
-
|
32
|
-
def params
|
33
|
-
@params ||= { :user_id => 'me', :id => 7 }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
class PostsController < ApplicationController
|
38
|
-
end
|
39
|
-
|
40
|
-
class Admin::SettingsController < ApplicationController
|
41
|
-
end
|
42
|
-
|
43
|
-
before :each do
|
44
|
-
@query = mock("the scoped query")
|
45
|
-
@query.stub!(:unpublished).and_return(@query)
|
46
|
-
@query.stub!(:recent).and_return(@query)
|
47
|
-
@query.stub!(:owned_by).with('me').and_return(@query)
|
48
|
-
end
|
49
|
-
|
50
|
-
describe '.model_class_name', 'by default' do
|
51
|
-
it 'is taken from the controller class name' do
|
52
|
-
PostsController.model_class_name.should == 'post'
|
53
|
-
end
|
54
|
-
|
55
|
-
it "includes the controller's modules divided by whacks" do
|
56
|
-
Admin::SettingsController.model_class_name.should == 'admin/setting'
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe '.serves_model' do
|
61
|
-
it 'sets the model class name to the passed value' do
|
62
|
-
PostsController.serves_model 'some_other_model'
|
63
|
-
PostsController.model_class_name.should == 'some_other_model'
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'accepts symbols as well as strings' do
|
67
|
-
PostsController.serves_model :some_other_model
|
68
|
-
PostsController.model_class_name.should == 'some_other_model'
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'raises ArgumentError for other values' do
|
72
|
-
lambda do
|
73
|
-
PostsController.serves_model [:not_a, :string_or_symbol]
|
74
|
-
end.should raise_error ArgumentError
|
75
|
-
end
|
76
|
-
|
77
|
-
after :each do
|
78
|
-
PostsController.serves_model :post
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe '#model_class_name' do
|
83
|
-
it "is .model_class_name" do
|
84
|
-
PostsController.new.model_class_name.should == PostsController.model_class_name
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
describe '.model_class' do
|
89
|
-
it 'is the constant named by model_class_name' do
|
90
|
-
PostsController.model_class.should == Post
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'handles modules in the name' do
|
94
|
-
Admin::SettingsController.model_class.should == Admin::Setting
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
describe '#scope', 'by default' do
|
99
|
-
it "passes its argument out" do
|
100
|
-
PostsController.new.scope(@query).should == @query
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
describe '#model_class' do
|
105
|
-
it 'is .model_class' do
|
106
|
-
PostsController.new.model_class.should == PostsController.model_class
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
describe '.scope' do
|
111
|
-
it 'takes a string naming a scope on model_class' do
|
112
|
-
PostsController.scope 'unpublished'
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'can take a symbol' do
|
116
|
-
PostsController.scope :recent
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
describe '.scopes' do
|
121
|
-
it 'is an array of scopes in order, as symbols' do
|
122
|
-
PostsController.scopes.should == [:unpublished, :recent]
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
describe '#scopes' do
|
127
|
-
it 'is .scopes' do
|
128
|
-
PostsController.new.scopes.should == PostsController.scopes
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe '#scope', 'with explicit scopes' do
|
133
|
-
it "asks model_class for the declared scopes in order" do
|
134
|
-
@query.should_receive(:unpublished).and_return(@query)
|
135
|
-
@query.should_receive(:recent).and_return(@query)
|
136
|
-
PostsController.new.scope(@query).should == @query
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
describe '.scope', 'with a block' do
|
141
|
-
it 'omits the name and can reference params' do
|
142
|
-
PostsController.scope do |posts|
|
143
|
-
posts.owned_by(params[:user_id])
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'puts a lambda on .scopes' do
|
148
|
-
PostsController.scopes.last.should be_a Proc
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
describe '.scope', 'with something that is not a string, symbol or block' do
|
153
|
-
it 'explodes' do
|
154
|
-
lambda do
|
155
|
-
PostsController.scope [:not_a, :string_symbol_or_block]
|
156
|
-
end.should raise_error ArgumentError
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
describe '#scope', 'with a block scope' do
|
161
|
-
it 'instance_execs the block while passing in the current query' do
|
162
|
-
@query.should_receive(:owned_by).with('me').and_return(@query)
|
163
|
-
|
164
|
-
controller = PostsController.new
|
165
|
-
controller.scope(@query).should == @query
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
describe '#scope', 'with an unknown scope' do
|
170
|
-
it 'explodes' do
|
171
|
-
PostsController.scope :furst_p0sts
|
172
|
-
|
173
|
-
lambda do
|
174
|
-
PostsController.new.scope
|
175
|
-
end.should raise_error ArgumentError
|
176
|
-
|
177
|
-
PostsController.scopes.pop
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
describe '#scope', 'with request parameters naming scopes' do
|
182
|
-
before :each do
|
183
|
-
@controller = PostsController.new
|
184
|
-
@controller.params['commented_on_by'] = 'you'
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'applies the requested scopes in order' do
|
188
|
-
@query.should_receive(:commented_on_by).with('you').and_return(@query)
|
189
|
-
@controller.scope @query
|
190
|
-
end
|
191
|
-
|
192
|
-
it 'is applied after class-level scopes' do
|
193
|
-
class_level_query = mock("class-level scoped query")
|
194
|
-
@query.should_receive(:owned_by).and_return(class_level_query) # last class-level scope
|
195
|
-
|
196
|
-
class_level_query.should_receive(:commented_on_by).with('you').and_return(class_level_query)
|
197
|
-
@controller.scope(@query).should == class_level_query
|
198
|
-
end
|
199
|
-
|
200
|
-
it 'throws BadScope for scopes that raise an exception' do
|
201
|
-
@query.should_receive(:commented_on_by).and_raise(ArgumentError.new)
|
202
|
-
lambda do
|
203
|
-
@controller.scope @query
|
204
|
-
end.should raise_error(ResponderController::BadScope)
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
describe '.serves_scopes' do
|
209
|
-
before :each do
|
210
|
-
@controller = PostsController.new
|
211
|
-
@controller.params['commented_on_by'] = 'you'
|
212
|
-
end
|
213
|
-
|
214
|
-
it 'can specify a white list of active record scopes to serve' do
|
215
|
-
PostsController.serves_scopes :only => [:recent, :authored_by, :published_after]
|
216
|
-
lambda do
|
217
|
-
@controller.scope @query
|
218
|
-
end.should raise_error(ResponderController::ForbiddenScope)
|
219
|
-
end
|
220
|
-
|
221
|
-
it 'can specify just one scope to white list' do
|
222
|
-
PostsController.serves_scopes :only => :recent
|
223
|
-
lambda do
|
224
|
-
@controller.scope @query
|
225
|
-
end.should raise_error(ResponderController::ForbiddenScope)
|
226
|
-
end
|
227
|
-
|
228
|
-
it 'can specify a black list of active record scopes to deny' do
|
229
|
-
PostsController.serves_scopes :except => [:commented_on_by, :unpublished]
|
230
|
-
lambda do
|
231
|
-
@controller.scope @query
|
232
|
-
end.should raise_error(ResponderController::ForbiddenScope)
|
233
|
-
end
|
234
|
-
|
235
|
-
it 'can specify just one scope to black list' do
|
236
|
-
PostsController.serves_scopes :except => :commented_on_by
|
237
|
-
lambda do
|
238
|
-
@controller.scope @query
|
239
|
-
end.should raise_error(ResponderController::ForbiddenScope)
|
240
|
-
end
|
241
|
-
|
242
|
-
it 'whines if passed anything other than a hash' do
|
243
|
-
lambda do
|
244
|
-
PostsController.serves_scopes 'cupcakes!'
|
245
|
-
end.should raise_error TypeError
|
246
|
-
end
|
247
|
-
|
248
|
-
it 'whines about keys other than :only and :except' do
|
249
|
-
lambda do
|
250
|
-
PostsController.serves_scopes 'only' => :recent
|
251
|
-
end.should raise_error ArgumentError
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'whines when both :only and :except are passed' do
|
255
|
-
lambda do
|
256
|
-
PostsController.serves_scopes :only => :recent, :except => :commented_on_by
|
257
|
-
end.should raise_error ArgumentError
|
258
|
-
end
|
259
|
-
|
260
|
-
it 'whines if both :only and :except are passed between different calls' do
|
261
|
-
PostsController.serves_scopes :only => :recent
|
262
|
-
lambda do
|
263
|
-
PostsController.serves_scopes :except => :commented_on_by
|
264
|
-
end.should raise_error ArgumentError
|
265
|
-
end
|
266
|
-
|
267
|
-
it 'accumulates scopes passed over multiple calls' do
|
268
|
-
PostsController.serves_scopes :only => :recent
|
269
|
-
PostsController.serves_scopes :only => :authored_by
|
270
|
-
PostsController.serves_scopes[:only].should == [:recent, :authored_by]
|
271
|
-
end
|
272
|
-
|
273
|
-
after :each do
|
274
|
-
PostsController.serves_scopes.clear # clean up
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
describe '#find_models' do
|
279
|
-
it 'is #scope #model_class.scoped' do
|
280
|
-
controller = PostsController.new
|
281
|
-
|
282
|
-
Post.should_receive(:scoped).and_return(@query)
|
283
|
-
controller.should_receive(:scope).with(@query).and_return(@query)
|
284
|
-
|
285
|
-
controller.find_models.should == @query
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
describe '#find_model' do
|
290
|
-
it 'is #find_models.find(params[:id])' do
|
291
|
-
controller = PostsController.new
|
292
|
-
controller.should_receive(:find_models).and_return(@query)
|
293
|
-
@query.should_receive(:find).with(controller.params[:id]).and_return(post = mock("the post"))
|
294
|
-
|
295
|
-
controller.find_model.should == post
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
describe '#model_slug' do
|
300
|
-
it 'is the model class name' do
|
301
|
-
PostsController.new.model_slug.should == :post
|
302
|
-
end
|
303
|
-
|
304
|
-
it 'drops the leading module names, if any' do
|
305
|
-
Admin::SettingsController.new.model_slug.should == :setting
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
describe '#models_slug' do
|
310
|
-
it 'is ths symbolized plural of #model_slug' do
|
311
|
-
PostsController.new.models_slug.should == :posts
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
describe '#model_ivar' do
|
316
|
-
it 'is the #model_slug with a leading @' do
|
317
|
-
PostsController.new.model_ivar.should == '@post'
|
318
|
-
end
|
319
|
-
end
|
320
|
-
|
321
|
-
describe '#models_ivar' do
|
322
|
-
it 'is the plural #model_ivar' do
|
323
|
-
(controller = PostsController.new).models_ivar.should == controller.model_ivar.pluralize
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
describe "#models" do
|
328
|
-
it "gets #models_ivar" do
|
329
|
-
(controller = PostsController.new).instance_variable_set("@posts", :some_posts)
|
330
|
-
controller.models.should == :some_posts
|
331
|
-
end
|
332
|
-
end
|
333
|
-
|
334
|
-
describe "#model" do
|
335
|
-
it "gets #model_ivar" do
|
336
|
-
(controller = PostsController.new).instance_variable_set("@post", :a_post)
|
337
|
-
controller.model.should == :a_post
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
describe "#models=" do
|
342
|
-
it "assigns to #models_ivar" do
|
343
|
-
assigned = mock("some models")
|
344
|
-
(controller = PostsController.new).models = assigned
|
345
|
-
controller.instance_variable_get("@posts").should == assigned
|
346
|
-
end
|
347
|
-
end
|
348
|
-
|
349
|
-
describe "#model=" do
|
350
|
-
it "assigns to #model_ivar" do
|
351
|
-
assigned = mock("a model")
|
352
|
-
(controller = PostsController.new).model = assigned
|
353
|
-
controller.instance_variable_get("@post").should == assigned
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
describe '.responds_within' do
|
358
|
-
it "contains the model's enclosing module names as symbols by default" do
|
359
|
-
PostsController.responds_within.should == []
|
360
|
-
Admin::SettingsController.responds_within.should == [:admin]
|
361
|
-
end
|
362
|
-
|
363
|
-
it "takes, saves and returns a varargs" do
|
364
|
-
PostsController.responds_within(:foo, :bar, :baz).should == [:foo, :bar, :baz]
|
365
|
-
PostsController.responds_within.should == [:foo, :bar, :baz]
|
366
|
-
end
|
367
|
-
|
368
|
-
it "accumulates between calls" do
|
369
|
-
PostsController.responds_within(:foo).should == [:foo]
|
370
|
-
PostsController.responds_within(:bar, :baz).should == [:foo, :bar, :baz]
|
371
|
-
PostsController.responds_within.should == [:foo, :bar, :baz]
|
372
|
-
end
|
373
|
-
|
374
|
-
it "can take a block instead" do
|
375
|
-
block = lambda {}
|
376
|
-
PostsController.responds_within(&block).should == [block]
|
377
|
-
PostsController.responds_within.should == [block]
|
378
|
-
end
|
379
|
-
|
380
|
-
it "whines if both positional arguments and a block are passed" do
|
381
|
-
lambda do
|
382
|
-
PostsController.responds_within(:foo, :bar, :baz) {}
|
383
|
-
end.should raise_error ArgumentError
|
384
|
-
end
|
385
|
-
|
386
|
-
after :each do
|
387
|
-
PostsController.instance_variable_set "@responds_within", nil # clear out the garbage
|
388
|
-
end
|
389
|
-
end
|
390
|
-
|
391
|
-
describe '.children_of' do
|
392
|
-
it 'takes a underscored model class name' do
|
393
|
-
PostsController.children_of 'accounts/user'
|
394
|
-
end
|
395
|
-
|
396
|
-
it "can take symbols" do
|
397
|
-
PostsController.children_of 'accounts/user'.to_sym
|
398
|
-
end
|
399
|
-
|
400
|
-
it "creates a scope filtering by the parent model's foreign key as passed in params" do
|
401
|
-
PostsController.children_of 'accounts/user'
|
402
|
-
controller = PostsController.new
|
403
|
-
controller.params[:user_id] = :the_user_id
|
404
|
-
|
405
|
-
user_query = mock("user-restricted query")
|
406
|
-
@query.should_receive(:where).with(:user_id => :the_user_id).and_return(user_query)
|
407
|
-
controller.scope(@query).should == user_query
|
408
|
-
end
|
409
|
-
|
410
|
-
it "adds a responds_within context, of the parent modules followed by the parent itself" do
|
411
|
-
PostsController.children_of 'accounts/user'
|
412
|
-
controller = PostsController.new
|
413
|
-
controller.params[:user_id] = :the_user_id
|
414
|
-
|
415
|
-
Accounts::User.should_receive(:find).with(:the_user_id).and_return(user = mock("the user"))
|
416
|
-
|
417
|
-
controller.responder_context(:argument).should == [:accounts, user, :argument]
|
418
|
-
end
|
419
|
-
|
420
|
-
after :each do
|
421
|
-
PostsController.instance_variable_set "@responds_within", nil # clear out the garbage
|
422
|
-
PostsController.scopes.clear
|
423
|
-
end
|
424
|
-
end
|
425
|
-
|
426
|
-
describe '#responds_within' do
|
427
|
-
it 'is .responds_within' do
|
428
|
-
PostsController.new.responds_within.should == PostsController.responds_within
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
|
-
describe '#responder_context' do
|
433
|
-
it "is the argument prepended with responds_within" do
|
434
|
-
Admin::SettingsController.new.responder_context(:argument).should == [:admin, :argument]
|
435
|
-
end
|
436
|
-
|
437
|
-
it "passes the argument to responds_within and prepends the result if it is a lambda" do
|
438
|
-
Admin::SettingsController.responds_within do |model|
|
439
|
-
model.should == :argument
|
440
|
-
[:nested, :namespace]
|
441
|
-
end
|
442
|
-
|
443
|
-
Admin::SettingsController.new.responder_context(:argument).should == [:nested, :namespace, :argument]
|
444
|
-
end
|
445
|
-
|
446
|
-
it "wraps the lambda result in an array if needed" do
|
447
|
-
Admin::SettingsController.responds_within { |model| :namespace }
|
448
|
-
Admin::SettingsController.new.responder_context(:argument).should == [:namespace, :argument]
|
449
|
-
end
|
450
|
-
|
451
|
-
after :each do
|
452
|
-
Admin::SettingsController.instance_variable_set "@responds_within", nil
|
453
|
-
end
|
454
|
-
end
|
455
|
-
|
456
|
-
describe '#respond_with_contextual' do
|
457
|
-
it 'passed #responder_context to #respond_with' do
|
458
|
-
controller = PostsController.new
|
459
|
-
controller.should_receive(:responder_context).with(:argument).and_return([:contextualized_argument])
|
460
|
-
controller.should_receive(:respond_with).with(:contextualized_argument)
|
461
|
-
|
462
|
-
controller.respond_with_contextual :argument
|
463
|
-
end
|
464
|
-
end
|
465
|
-
|
466
|
-
describe 'actions' do
|
467
|
-
before :each do
|
468
|
-
@controller = PostsController.new
|
469
|
-
@controller.stub!(:find_models).and_return(@posts = mock("some posts"))
|
470
|
-
@controller.stub!(:find_model).and_return(@post = mock("a post"))
|
471
|
-
@controller.stub!(:respond_with)
|
472
|
-
|
473
|
-
@posts.stub!(:build).and_return(@post)
|
474
|
-
end
|
475
|
-
|
476
|
-
describe '#index' do
|
477
|
-
it 'assigns #find_models to #models=' do
|
478
|
-
@controller.should_receive(:find_models).and_return(@posts)
|
479
|
-
@controller.index
|
480
|
-
@controller.instance_variable_get('@posts').should == @posts
|
481
|
-
end
|
482
|
-
|
483
|
-
it '#respond_with_contextual @models' do
|
484
|
-
@controller.should_receive(:respond_with_contextual).with(@posts)
|
485
|
-
@controller.index
|
486
|
-
end
|
487
|
-
end
|
488
|
-
|
489
|
-
[:show, :edit].each do |verb|
|
490
|
-
describe "##{verb}" do
|
491
|
-
it 'assigns #find_model to #model=' do
|
492
|
-
@controller.should_receive(:find_model).and_return(@post)
|
493
|
-
@controller.send verb
|
494
|
-
@controller.instance_variable_get('@post').should == @post
|
495
|
-
end
|
496
|
-
|
497
|
-
it '#respond_with_contextual @model' do
|
498
|
-
@controller.should_receive(:respond_with_contextual).with(@post)
|
499
|
-
@controller.send verb
|
500
|
-
end
|
501
|
-
end
|
502
|
-
end
|
503
|
-
|
504
|
-
describe '#new' do
|
505
|
-
it 'assigns #find_models.new to #model=' do
|
506
|
-
@posts.should_receive(:build).and_return(@post)
|
507
|
-
@controller.new
|
508
|
-
@controller.instance_variable_get('@post').should == @post
|
509
|
-
end
|
510
|
-
|
511
|
-
it '#respond_with_contextual @model' do
|
512
|
-
@controller.should_receive(:respond_with_contextual).with(@post)
|
513
|
-
@controller.new
|
514
|
-
end
|
515
|
-
end
|
516
|
-
|
517
|
-
describe '#create' do
|
518
|
-
before :each do
|
519
|
-
@post.stub!(:save)
|
520
|
-
end
|
521
|
-
|
522
|
-
it 'passes params[model_slug] to #find_models.new' do
|
523
|
-
@controller.params[:post] = :params_to_new
|
524
|
-
@posts.should_receive(:build).with(:params_to_new)
|
525
|
-
@controller.create
|
526
|
-
end
|
527
|
-
|
528
|
-
it 'assigns #find_models.new to #model=' do
|
529
|
-
@controller.create
|
530
|
-
@controller.instance_variable_get('@post').should == @post
|
531
|
-
end
|
532
|
-
|
533
|
-
it 'saves the model' do
|
534
|
-
@post.should_receive(:save)
|
535
|
-
@controller.create
|
536
|
-
end
|
537
|
-
|
538
|
-
it '#respond_with_contextual @model' do
|
539
|
-
@controller.should_receive(:respond_with_contextual).with(@post)
|
540
|
-
@controller.create
|
541
|
-
end
|
542
|
-
end
|
543
|
-
|
544
|
-
describe '#update' do
|
545
|
-
before :each do
|
546
|
-
@post.stub!(:update_attributes)
|
547
|
-
end
|
548
|
-
|
549
|
-
it 'assigns #find_model to #model=' do
|
550
|
-
@controller.should_receive(:find_model).and_return(@post)
|
551
|
-
@controller.update
|
552
|
-
@controller.instance_variable_get('@post').should == @post
|
553
|
-
end
|
554
|
-
|
555
|
-
it "updates the model's attributes" do
|
556
|
-
@controller.params[:post] = :params_to_update
|
557
|
-
@post.should_receive(:update_attributes).with(:params_to_update)
|
558
|
-
@controller.update
|
559
|
-
end
|
560
|
-
|
561
|
-
it '#respond_with_contextual @model' do
|
562
|
-
@controller.should_receive(:respond_with_contextual).with(@post)
|
563
|
-
@controller.update
|
564
|
-
end
|
565
|
-
end
|
566
|
-
|
567
|
-
describe '#destroy' do
|
568
|
-
before :each do
|
569
|
-
@post.stub!(:destroy)
|
570
|
-
end
|
571
|
-
|
572
|
-
it 'finds the model' do
|
573
|
-
@controller.should_receive(:find_model).and_return(@post)
|
574
|
-
@controller.destroy
|
575
|
-
end
|
576
|
-
|
577
|
-
it 'destroys the model' do
|
578
|
-
@post.should_receive(:destroy)
|
579
|
-
@controller.destroy
|
580
|
-
end
|
581
|
-
|
582
|
-
it '#respond_with_contextual #models_slug' do
|
583
|
-
@controller.should_receive(:respond_with_contextual).with(:posts)
|
584
|
-
@controller.destroy
|
585
|
-
end
|
586
|
-
end
|
587
|
-
end
|
3
|
+
describe ResponderController do
|
588
4
|
end
|