public
Description: resources_controller rails plugin: rc makes RESTful controllers fun
Homepage: http://plugins.ardes.com/doc/resources_controller
Clone URL: git://github.com/ianwhite/resources_controller.git
Click here to lend your support to: resources_controller and make a donation at www.pledgie.com !
resources_controller / spec / lib / load_enclosing_resources_spec.rb
100644 265 lines (216 sloc) 11.879 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), '../app'))
 
module LoadEnclosingResourcesSpecHelper
  def setup_tags_controller(options = {})
    @klass = Class.new(ActionController::Base)
    @klass.resources_controller_for :tags, options
    setup_common
  end
 
  def setup_common
    @controller = @klass.new
    # stub :load_enclosing_resource_from_specification, increase enclosing_resources by one, and return a mock resource
    @controller.stub!(:load_enclosing_resource_from_specification).and_return do |name, _|
      returning mock("resource: #{name}") do |resource|
        @controller.enclosing_resources << resource
      end
    end
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags (when route_enclosing_names is [['users', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller
    @controller.stub!(:route_enclosing_names).and_return [['users', false]]
  end
    
  it "should call load_wildcard once" do
    @controller.should_receive(:load_wildcard).once
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new('user', :singleton => false, :as => nil)" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil)
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, with :account mapping (when route_enclosing_names is [['account', true]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller
    @klass.map_resource :account, :singleton => true, :class => User
    @account_spec = @controller.resource_specification_map['account']
    @controller.stub!(:route_enclosing_names).and_return [['account', true]]
  end
    
  it "should call load_wildcard once" do
    @controller.should_receive(:load_wildcard).once
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call load_enclosing_resource_from_specification with account specification" do
    @controller.should_receive(:load_enclosing_resource_from_specification).with(@account_spec)
    @controller.send(:load_enclosing_resources)
  end
  
  it "should not call Specification.new" do
    Ardes::ResourcesController::Specification.should_not_receive(:new)
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags (when route_enclosing_names is [['users', false], ['forums', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['forums', false]]
  end
    
  it "should call load_wildcard twice" do
    @controller.should_receive(:load_wildcard).twice
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('user', :singleton => false, :as => nil), then ('forum', :singleton => false, :as => nil)" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', :comment] (when route_enclosing_names is [['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['*', :comment]
    @controller.stub!(:route_enclosing_names).and_return [['comments', false]]
  end
  
  it "should not call load_wildcard" do
    @controller.should_not_receive(:load_wildcard)
    @controller.send(:load_enclosing_resources)
  end
  
  it "should not call Specification.new" do
    Ardes::ResourcesController::Specification.should_not_receive(:new)
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', :comment] (when route_enclosing_names is [['users', false], ['forums', false], ['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['*', :comment]
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['forums', false], ['comments', false]]
  end
  
  it "should call load_wildcard twice" do
    @controller.should_receive(:load_wildcard).twice
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('user', :singleton => false, :as => nil), then ('forum', :singleton => false, :as => nil)" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', :comment] (when route_enclosing_names is [['users', false], ['forums', false], ['special', true], ['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['*', :comment]
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['forums', false], ['special', true], ['comments', false]]
  end
  
  it "should call load_wildcard three times" do
    @controller.should_receive(:load_wildcard).exactly(3).times
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('user', :singleton => false, :as => nil), ('forum', :singleton => false, :as => nil), then ('special', :singleton => true, :as => nil)" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('special', :singleton => true, :as => nil).ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', '?commentable', :comment] (when route_enclosing_names is [['users', false], ['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['*', '?commentable', :comment]
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['comments', false]]
  end
  
  it "should call load_wildcard once with 'commentable'" do
    @controller.should_receive(:load_wildcard).with('commentable').once
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('user', :singleton => false, :as => 'commentable')" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => 'commentable').ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', '?commentable', :comment] (when route_enclosing_names is [['users', false], ['forums', false], ['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['*', '?commentable', :comment]
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['forums', false], ['comments', false]]
  end
  
  it "should call load_wildcard twice" do
    @controller.should_receive(:load_wildcard).with().once.ordered
    @controller.should_receive(:load_wildcard).with('commentable').once.ordered
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('user', :singleton => false, :as => nil), ('forum', :singleton => false, :as => 'commentable')" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).once.ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => 'commentable').once.ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['*', '?commentable', :comment] (when route_enclosing_names is [['users', false], ['forums', false], ['posts', false], ['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['*', '?commentable', :comment]
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['forums', false], ['posts', false], ['comments', false]]
  end
  
  it "should call load_wildcard twice, then once with 'commentable'" do
    @controller.should_receive(:load_wildcard).with().twice.ordered
    @controller.should_receive(:load_wildcard).with('commentable').once.ordered
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('user', :singleton => false, :as => nil), ('forum', :singleton => false, :as => nil), then ('post', :singleton => false, :as => 'commentable')" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('user', :singleton => false, :as => nil).once.ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('forum', :singleton => false, :as => nil).once.ordered
    Ardes::ResourcesController::Specification.should_receive(:new).with('post', :singleton => false, :as => 'commentable').once.ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
describe "#load_enclosing_resources for resources_controller_for :tags, :in => ['user', '*', '?taggable'] (when route_enclosing_names is [['users', false], ['comments', false]])" do
  include LoadEnclosingResourcesSpecHelper
 
  before do
    setup_tags_controller :in => ['user', '*', '?taggable']
    @user_spec = @controller.send(:specifications)[1]
    @controller.stub!(:route_enclosing_names).and_return [['users', false], ['comments', false]]
  end
  
  it "should call load_enclosing_resource_from_specification with user spec, then load_wildcard once with 'taggable'" do
    @controller.should_receive(:load_enclosing_resource_from_specification).with(@user_spec).once.ordered do
      returning(mock('user')){|r| @controller.enclosing_resources << r }
    end
    @controller.should_receive(:load_wildcard).with('taggable').once.ordered
    @controller.send(:load_enclosing_resources)
  end
  
  it "should call Specification.new with ('comment', :singleton => false, :as => 'taggable')" do
    Ardes::ResourcesController::Specification.should_receive(:new).with('comment', :singleton => false, :as => 'taggable').ordered
    @controller.send(:load_enclosing_resources)
  end
end
 
# specing some branching BC code
# find_filter dissapeared from edge, but we want to support it for 2.0-stable
describe "ResourcesController.load_enclosing_resources_filter_exists?" do
  before do
    @klass = Class.new(ActionController::Base)
  end
  
  describe "when :find_filter defined" do
    before do
      class<<@klass
        def find_filter; end
      end
    end
    
    it "should call :find_filter with :load_enclosing_resources" do
      @klass.should_receive(:find_filter).with(:load_enclosing_resources)
      @klass.send(:load_enclosing_resources_filter_exists?)
    end
  end
  
  describe "when :find_filter not defined" do
    before do
      class<<@klass
        undef_method(:find_filter) rescue nil
      end
    end
        
    it "should call :filter_chain" do
      @klass.should_receive(:filter_chain).and_return([])
      @klass.send(:load_enclosing_resources_filter_exists?)
    end
  end
end