draper 1.2.0 → 1.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +216 -56
- data/lib/draper/automatic_delegation.rb +3 -3
- data/lib/draper/collection_decorator.rb +14 -10
- data/lib/draper/decoratable.rb +1 -1
- data/lib/draper/decoratable/equality.rb +1 -1
- data/lib/draper/decorated_association.rb +1 -1
- data/lib/draper/decorator.rb +46 -40
- data/lib/draper/delegation.rb +2 -2
- data/lib/draper/factory.rb +18 -14
- data/lib/draper/finders.rb +5 -5
- data/lib/draper/railtie.rb +1 -1
- data/lib/draper/version.rb +1 -1
- data/lib/generators/decorator/decorator_generator.rb +2 -2
- data/lib/generators/decorator/templates/decorator.rb +1 -1
- data/spec/draper/collection_decorator_spec.rb +41 -22
- data/spec/draper/decoratable_spec.rb +4 -4
- data/spec/draper/decorated_association_spec.rb +5 -5
- data/spec/draper/decorates_assigned_spec.rb +3 -3
- data/spec/draper/decorator_spec.rb +130 -91
- data/spec/draper/factory_spec.rb +30 -28
- data/spec/draper/finders_spec.rb +4 -4
- data/spec/dummy/app/decorators/post_decorator.rb +2 -2
- data/spec/dummy/fast_spec/post_decorator_spec.rb +2 -2
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +7 -7
- data/spec/dummy/test/minitest_helper.rb +1 -3
- data/spec/generators/decorator/decorator_generator_spec.rb +1 -0
- data/spec/support/shared_examples/decoratable_equality.rb +8 -8
- metadata +2 -2
data/lib/draper/railtie.rb
CHANGED
data/lib/draper/version.rb
CHANGED
@@ -27,8 +27,8 @@ module Rails
|
|
27
27
|
|
28
28
|
# Rails 3.0.X compatibility, stolen from https://github.com/jnunemaker/mongomapper/pull/385/files#L1R32
|
29
29
|
unless methods.include?(:module_namespacing)
|
30
|
-
def module_namespacing
|
31
|
-
yield if
|
30
|
+
def module_namespacing
|
31
|
+
yield if block_given?
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -84,8 +84,8 @@ module Draper
|
|
84
84
|
collection = [Product.new, Product.new]
|
85
85
|
decorator = CollectionDecorator.new(collection)
|
86
86
|
|
87
|
-
decorator.zip collection do |item,
|
88
|
-
expect(item.
|
87
|
+
decorator.zip collection do |item, object|
|
88
|
+
expect(item.object).to be object
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
@@ -109,8 +109,8 @@ module Draper
|
|
109
109
|
describe ".delegate" do
|
110
110
|
protect_class ProductsDecorator
|
111
111
|
|
112
|
-
it "defaults the :to option to :
|
113
|
-
Object.should_receive(:delegate).with(:foo, :bar, to: :
|
112
|
+
it "defaults the :to option to :object" do
|
113
|
+
Object.should_receive(:delegate).with(:foo, :bar, to: :object)
|
114
114
|
ProductsDecorator.delegate :foo, :bar
|
115
115
|
end
|
116
116
|
|
@@ -131,12 +131,15 @@ module Draper
|
|
131
131
|
end
|
132
132
|
|
133
133
|
context "without a block" do
|
134
|
-
it "decorates
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
134
|
+
it "decorates object.find" do
|
135
|
+
object = []
|
136
|
+
found = stub(decorate: :decorated)
|
137
|
+
decorator = CollectionDecorator.new(object)
|
138
|
+
|
139
|
+
object.should_receive(:find).and_return(found)
|
140
|
+
ActiveSupport::Deprecation.silence do
|
141
|
+
expect(decorator.find(1)).to be :decorated
|
142
|
+
end
|
140
143
|
end
|
141
144
|
end
|
142
145
|
end
|
@@ -159,17 +162,17 @@ module Draper
|
|
159
162
|
end
|
160
163
|
|
161
164
|
describe "#==" do
|
162
|
-
context "when comparing to a collection decorator with the same
|
165
|
+
context "when comparing to a collection decorator with the same object" do
|
163
166
|
it "returns true" do
|
164
|
-
|
165
|
-
decorator = CollectionDecorator.new(
|
166
|
-
other = ProductsDecorator.new(
|
167
|
+
object = [Product.new, Product.new]
|
168
|
+
decorator = CollectionDecorator.new(object)
|
169
|
+
other = ProductsDecorator.new(object)
|
167
170
|
|
168
171
|
expect(decorator == other).to be_true
|
169
172
|
end
|
170
173
|
end
|
171
174
|
|
172
|
-
context "when comparing to a collection decorator with a different
|
175
|
+
context "when comparing to a collection decorator with a different object" do
|
173
176
|
it "returns false" do
|
174
177
|
decorator = CollectionDecorator.new([Product.new, Product.new])
|
175
178
|
other = ProductsDecorator.new([Product.new, Product.new])
|
@@ -180,9 +183,9 @@ module Draper
|
|
180
183
|
|
181
184
|
context "when comparing to a collection of the same items" do
|
182
185
|
it "returns true" do
|
183
|
-
|
184
|
-
decorator = CollectionDecorator.new(
|
185
|
-
other =
|
186
|
+
object = [Product.new, Product.new]
|
187
|
+
decorator = CollectionDecorator.new(object)
|
188
|
+
other = object.dup
|
186
189
|
|
187
190
|
expect(decorator == other).to be_true
|
188
191
|
end
|
@@ -198,10 +201,10 @@ module Draper
|
|
198
201
|
end
|
199
202
|
|
200
203
|
context "when the decorated collection has been modified" do
|
201
|
-
it "is no longer equal to the
|
202
|
-
|
203
|
-
decorator = CollectionDecorator.new(
|
204
|
-
other =
|
204
|
+
it "is no longer equal to the object" do
|
205
|
+
object = [Product.new, Product.new]
|
206
|
+
decorator = CollectionDecorator.new(object)
|
207
|
+
other = object.dup
|
205
208
|
|
206
209
|
decorator << Product.new.decorate
|
207
210
|
expect(decorator == other).to be_false
|
@@ -275,5 +278,21 @@ module Draper
|
|
275
278
|
end
|
276
279
|
end
|
277
280
|
|
281
|
+
describe "#replace" do
|
282
|
+
it "replaces the decorated collection" do
|
283
|
+
decorator = CollectionDecorator.new([Product.new])
|
284
|
+
replacement = [:foo, :bar]
|
285
|
+
|
286
|
+
decorator.replace replacement
|
287
|
+
expect(decorator).to match_array replacement
|
288
|
+
end
|
289
|
+
|
290
|
+
it "returns itself" do
|
291
|
+
decorator = CollectionDecorator.new([Product.new])
|
292
|
+
|
293
|
+
expect(decorator.replace([:foo, :bar])).to be decorator
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
278
297
|
end
|
279
298
|
end
|
@@ -10,7 +10,7 @@ module Draper
|
|
10
10
|
decorator = product.decorate
|
11
11
|
|
12
12
|
expect(decorator).to be_a ProductDecorator
|
13
|
-
expect(decorator.
|
13
|
+
expect(decorator.object).to be product
|
14
14
|
end
|
15
15
|
|
16
16
|
it "accepts context" do
|
@@ -89,19 +89,19 @@ module Draper
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it "is true for a decorated instance" do
|
92
|
-
decorator = double(
|
92
|
+
decorator = double(object: Product.new)
|
93
93
|
|
94
94
|
expect(Product === decorator).to be_true
|
95
95
|
end
|
96
96
|
|
97
97
|
it "is true for a decorated derived instance" do
|
98
|
-
decorator = double(
|
98
|
+
decorator = double(object: Class.new(Product).new)
|
99
99
|
|
100
100
|
expect(Product === decorator).to be_true
|
101
101
|
end
|
102
102
|
|
103
103
|
it "is false for a decorated unrelated instance" do
|
104
|
-
decorator = double(
|
104
|
+
decorator = double(object: Model.new)
|
105
105
|
|
106
106
|
expect(Product === decorator).to be_false
|
107
107
|
end
|
@@ -43,8 +43,8 @@ module Draper
|
|
43
43
|
Factory.stub new: factory
|
44
44
|
associated = double
|
45
45
|
owner_context = {foo: "bar"}
|
46
|
-
|
47
|
-
owner = double(
|
46
|
+
object = double(association: associated)
|
47
|
+
owner = double(object: object, context: owner_context)
|
48
48
|
decorated_association = DecoratedAssociation.new(owner, :association, {})
|
49
49
|
decorated = double
|
50
50
|
|
@@ -55,7 +55,7 @@ module Draper
|
|
55
55
|
it "memoizes" do
|
56
56
|
factory = double
|
57
57
|
Factory.stub new: factory
|
58
|
-
owner = double(
|
58
|
+
owner = double(object: double(association: double), context: {})
|
59
59
|
decorated_association = DecoratedAssociation.new(owner, :association, {})
|
60
60
|
decorated = double
|
61
61
|
|
@@ -69,8 +69,8 @@ module Draper
|
|
69
69
|
factory = double
|
70
70
|
Factory.stub new: factory
|
71
71
|
scoped = double
|
72
|
-
|
73
|
-
owner = double(
|
72
|
+
object = double(association: double(applied_scope: scoped))
|
73
|
+
owner = double(object: object, context: {})
|
74
74
|
decorated_association = DecoratedAssociation.new(owner, :association, scope: :applied_scope)
|
75
75
|
decorated = double
|
76
76
|
|
@@ -41,15 +41,15 @@ module Draper
|
|
41
41
|
|
42
42
|
describe "the generated method" do
|
43
43
|
it "decorates the instance variable" do
|
44
|
-
|
44
|
+
object = double
|
45
45
|
factory = double
|
46
46
|
Factory.stub new: factory
|
47
47
|
|
48
48
|
controller_class.decorates_assigned :article
|
49
49
|
controller = controller_class.new
|
50
|
-
controller.instance_variable_set "@article",
|
50
|
+
controller.instance_variable_set "@article", object
|
51
51
|
|
52
|
-
factory.should_receive(:decorate).with(
|
52
|
+
factory.should_receive(:decorate).with(object, context_args: controller).and_return(:decorated)
|
53
53
|
expect(controller.article).to be :decorated
|
54
54
|
end
|
55
55
|
|
@@ -17,11 +17,11 @@ module Draper
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
it "sets the
|
21
|
-
|
22
|
-
decorator = Decorator.new(
|
20
|
+
it "sets the object" do
|
21
|
+
object = Model.new
|
22
|
+
decorator = Decorator.new(object)
|
23
23
|
|
24
|
-
expect(decorator.
|
24
|
+
expect(decorator.object).to be object
|
25
25
|
end
|
26
26
|
|
27
27
|
it "stores context" do
|
@@ -32,12 +32,12 @@ module Draper
|
|
32
32
|
end
|
33
33
|
|
34
34
|
context "when decorating an instance of itself" do
|
35
|
-
it "applies to the
|
36
|
-
|
37
|
-
decorated = Decorator.new(
|
35
|
+
it "applies to the object instead" do
|
36
|
+
object = Model.new
|
37
|
+
decorated = Decorator.new(object)
|
38
38
|
redecorated = Decorator.new(decorated)
|
39
39
|
|
40
|
-
expect(redecorated.
|
40
|
+
expect(redecorated.object).to be object
|
41
41
|
end
|
42
42
|
|
43
43
|
context "with context" do
|
@@ -65,7 +65,7 @@ module Draper
|
|
65
65
|
decorated = OtherDecorator.new(Model.new)
|
66
66
|
redecorated = Decorator.new(decorated)
|
67
67
|
|
68
|
-
expect(redecorated.
|
68
|
+
expect(redecorated.object).to be decorated
|
69
69
|
end
|
70
70
|
|
71
71
|
context "when it has been applied previously" do
|
@@ -85,7 +85,7 @@ module Draper
|
|
85
85
|
Object.any_instance.stub(:warn)
|
86
86
|
redecorated = Decorator.decorate(decorated)
|
87
87
|
|
88
|
-
expect(redecorated.
|
88
|
+
expect(redecorated.object).to be decorated
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
@@ -116,10 +116,10 @@ module Draper
|
|
116
116
|
|
117
117
|
context "without a custom collection decorator" do
|
118
118
|
it "creates a CollectionDecorator using itself for each item" do
|
119
|
-
|
119
|
+
object = [Model.new]
|
120
120
|
|
121
|
-
CollectionDecorator.should_receive(:new).with(
|
122
|
-
Decorator.decorate_collection(
|
121
|
+
CollectionDecorator.should_receive(:new).with(object, with: Decorator)
|
122
|
+
Decorator.decorate_collection(object)
|
123
123
|
end
|
124
124
|
|
125
125
|
it "passes options to the collection decorator" do
|
@@ -132,10 +132,10 @@ module Draper
|
|
132
132
|
|
133
133
|
context "with a custom collection decorator" do
|
134
134
|
it "creates a custom collection decorator using itself for each item" do
|
135
|
-
|
135
|
+
object = [Model.new]
|
136
136
|
|
137
|
-
ProductsDecorator.should_receive(:new).with(
|
138
|
-
ProductDecorator.decorate_collection(
|
137
|
+
ProductsDecorator.should_receive(:new).with(object, with: ProductDecorator)
|
138
|
+
ProductDecorator.decorate_collection(object)
|
139
139
|
end
|
140
140
|
|
141
141
|
it "passes options to the collection decorator" do
|
@@ -157,74 +157,84 @@ module Draper
|
|
157
157
|
describe ".decorates" do
|
158
158
|
protect_class Decorator
|
159
159
|
|
160
|
-
it "sets .
|
160
|
+
it "sets .object_class with a symbol" do
|
161
161
|
Decorator.decorates :product
|
162
162
|
|
163
|
-
expect(Decorator.
|
163
|
+
expect(Decorator.object_class).to be Product
|
164
164
|
end
|
165
165
|
|
166
|
-
it "sets .
|
166
|
+
it "sets .object_class with a string" do
|
167
167
|
Decorator.decorates "product"
|
168
168
|
|
169
|
-
expect(Decorator.
|
169
|
+
expect(Decorator.object_class).to be Product
|
170
170
|
end
|
171
171
|
|
172
|
-
it "sets .
|
172
|
+
it "sets .object_class with a class" do
|
173
173
|
Decorator.decorates Product
|
174
174
|
|
175
|
-
expect(Decorator.
|
175
|
+
expect(Decorator.object_class).to be Product
|
176
176
|
end
|
177
177
|
end
|
178
178
|
|
179
|
-
describe ".
|
179
|
+
describe ".object_class" do
|
180
180
|
protect_class ProductDecorator
|
181
181
|
protect_class Namespaced::ProductDecorator
|
182
182
|
|
183
183
|
context "when not set by .decorates" do
|
184
184
|
it "raises an UninferrableSourceError for a so-named 'Decorator'" do
|
185
|
-
expect{Decorator.
|
185
|
+
expect{Decorator.object_class}.to raise_error UninferrableSourceError
|
186
186
|
end
|
187
187
|
|
188
188
|
it "raises an UninferrableSourceError for anonymous decorators" do
|
189
|
-
expect{Class.new(Decorator).
|
189
|
+
expect{Class.new(Decorator).object_class}.to raise_error UninferrableSourceError
|
190
190
|
end
|
191
191
|
|
192
192
|
it "raises an UninferrableSourceError for a decorator without a model" do
|
193
|
-
expect{OtherDecorator.
|
193
|
+
expect{OtherDecorator.object_class}.to raise_error UninferrableSourceError
|
194
194
|
end
|
195
195
|
|
196
196
|
it "raises an UninferrableSourceError for other naming conventions" do
|
197
|
-
expect{ProductPresenter.
|
197
|
+
expect{ProductPresenter.object_class}.to raise_error UninferrableSourceError
|
198
198
|
end
|
199
199
|
|
200
200
|
it "infers the source for '<Model>Decorator'" do
|
201
|
-
expect(ProductDecorator.
|
201
|
+
expect(ProductDecorator.object_class).to be Product
|
202
202
|
end
|
203
203
|
|
204
204
|
it "infers namespaced sources" do
|
205
|
-
expect(Namespaced::ProductDecorator.
|
205
|
+
expect(Namespaced::ProductDecorator.object_class).to be Namespaced::Product
|
206
206
|
end
|
207
207
|
|
208
208
|
context "when an unrelated NameError is thrown" do
|
209
209
|
it "re-raises that error" do
|
210
210
|
String.any_instance.stub(:constantize).and_return{SomethingThatDoesntExist}
|
211
|
-
expect{ProductDecorator.
|
211
|
+
expect{ProductDecorator.object_class}.to raise_error NameError, /SomethingThatDoesntExist/
|
212
212
|
end
|
213
213
|
end
|
214
214
|
end
|
215
|
+
|
216
|
+
it "is aliased to .source_class" do
|
217
|
+
expect(ProductDecorator.source_class).to be Product
|
218
|
+
end
|
215
219
|
end
|
216
220
|
|
217
|
-
describe ".
|
218
|
-
it "returns truthy when .
|
219
|
-
Decorator.stub(:
|
221
|
+
describe ".object_class?" do
|
222
|
+
it "returns truthy when .object_class is set" do
|
223
|
+
Decorator.stub(:object_class).and_return(Model)
|
220
224
|
|
221
|
-
expect(Decorator.
|
225
|
+
expect(Decorator.object_class?).to be_true
|
226
|
+
end
|
227
|
+
|
228
|
+
it "returns false when .object_class is not inferrable" do
|
229
|
+
Decorator.stub(:object_class).and_raise(UninferrableSourceError.new(Decorator))
|
230
|
+
|
231
|
+
expect(Decorator.object_class?).to be_false
|
222
232
|
end
|
223
233
|
|
224
|
-
it "
|
225
|
-
Decorator.stub(:
|
234
|
+
it "is aliased to .source_class?" do
|
235
|
+
Decorator.stub(:object_class).and_return(Model)
|
226
236
|
|
227
|
-
expect(Decorator.source_class?).to
|
237
|
+
expect(Decorator.source_class?).to be_true
|
228
238
|
end
|
229
239
|
end
|
230
240
|
|
@@ -319,14 +329,35 @@ module Draper
|
|
319
329
|
end
|
320
330
|
end
|
321
331
|
|
322
|
-
describe "#
|
332
|
+
describe "#object" do
|
323
333
|
it "returns the wrapped object" do
|
324
|
-
|
325
|
-
decorator = Decorator.new(
|
334
|
+
object = Model.new
|
335
|
+
decorator = Decorator.new(object)
|
336
|
+
|
337
|
+
expect(decorator.object).to be object
|
338
|
+
expect(decorator.model).to be object
|
339
|
+
expect(decorator.to_source).to be object
|
340
|
+
end
|
341
|
+
|
342
|
+
it "is aliased to #model" do
|
343
|
+
object = Model.new
|
344
|
+
decorator = Decorator.new(object)
|
326
345
|
|
327
|
-
expect(decorator.
|
328
|
-
|
329
|
-
|
346
|
+
expect(decorator.model).to be object
|
347
|
+
end
|
348
|
+
|
349
|
+
it "is aliased to #source" do
|
350
|
+
object = Model.new
|
351
|
+
decorator = Decorator.new(object)
|
352
|
+
|
353
|
+
expect(decorator.source).to be object
|
354
|
+
end
|
355
|
+
|
356
|
+
it "is aliased to #to_source" do
|
357
|
+
object = Model.new
|
358
|
+
decorator = Decorator.new(object)
|
359
|
+
|
360
|
+
expect(decorator.to_source).to be object
|
330
361
|
end
|
331
362
|
end
|
332
363
|
|
@@ -339,7 +370,7 @@ module Draper
|
|
339
370
|
end
|
340
371
|
|
341
372
|
describe "#to_param" do
|
342
|
-
it "delegates to the
|
373
|
+
it "delegates to the object" do
|
343
374
|
decorator = Decorator.new(double(to_param: :delegated))
|
344
375
|
|
345
376
|
expect(decorator.to_param).to be :delegated
|
@@ -347,15 +378,23 @@ module Draper
|
|
347
378
|
end
|
348
379
|
|
349
380
|
describe "#present?" do
|
350
|
-
it "delegates to the
|
381
|
+
it "delegates to the object" do
|
351
382
|
decorator = Decorator.new(double(present?: :delegated))
|
352
383
|
|
353
384
|
expect(decorator.present?).to be :delegated
|
354
385
|
end
|
355
386
|
end
|
356
387
|
|
388
|
+
describe "#blank?" do
|
389
|
+
it "delegates to the object" do
|
390
|
+
decorator = Decorator.new(double(blank?: :delegated))
|
391
|
+
|
392
|
+
expect(decorator.blank?).to be :delegated
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
357
396
|
describe "#to_partial_path" do
|
358
|
-
it "delegates to the
|
397
|
+
it "delegates to the object" do
|
359
398
|
decorator = Decorator.new(double(to_partial_path: :delegated))
|
360
399
|
|
361
400
|
expect(decorator.to_partial_path).to be :delegated
|
@@ -363,7 +402,7 @@ module Draper
|
|
363
402
|
end
|
364
403
|
|
365
404
|
describe "#attributes" do
|
366
|
-
it "returns only the
|
405
|
+
it "returns only the object's attributes that are implemented by the decorator" do
|
367
406
|
decorator = Decorator.new(double(attributes: {foo: "bar", baz: "qux"}))
|
368
407
|
decorator.stub(:foo)
|
369
408
|
|
@@ -373,42 +412,42 @@ module Draper
|
|
373
412
|
|
374
413
|
describe ".model_name" do
|
375
414
|
it "delegates to the source class" do
|
376
|
-
Decorator.stub
|
415
|
+
Decorator.stub object_class: double(model_name: :delegated)
|
377
416
|
|
378
417
|
expect(Decorator.model_name).to be :delegated
|
379
418
|
end
|
380
419
|
end
|
381
420
|
|
382
421
|
describe "#==" do
|
383
|
-
it "works for a
|
384
|
-
|
385
|
-
decorator = Decorator.new(
|
422
|
+
it "works for a object that does not include Decoratable" do
|
423
|
+
object = Object.new
|
424
|
+
decorator = Decorator.new(object)
|
386
425
|
|
387
|
-
expect(decorator).to eq Decorator.new(
|
426
|
+
expect(decorator).to eq Decorator.new(object)
|
388
427
|
end
|
389
428
|
|
390
|
-
it "works for a multiply-decorated
|
391
|
-
|
392
|
-
decorator = Decorator.new(
|
429
|
+
it "works for a multiply-decorated object that does not include Decoratable" do
|
430
|
+
object = Object.new
|
431
|
+
decorator = Decorator.new(object)
|
393
432
|
|
394
|
-
expect(decorator).to eq ProductDecorator.new(Decorator.new(
|
433
|
+
expect(decorator).to eq ProductDecorator.new(Decorator.new(object))
|
395
434
|
end
|
396
435
|
|
397
|
-
it "is true when
|
398
|
-
|
399
|
-
decorator = Decorator.new(
|
400
|
-
other = double(
|
436
|
+
it "is true when object #== is true" do
|
437
|
+
object = Model.new
|
438
|
+
decorator = Decorator.new(object)
|
439
|
+
other = double(object: Model.new)
|
401
440
|
|
402
|
-
|
441
|
+
object.should_receive(:==).with(other).and_return(true)
|
403
442
|
expect(decorator == other).to be_true
|
404
443
|
end
|
405
444
|
|
406
|
-
it "is false when
|
407
|
-
|
408
|
-
decorator = Decorator.new(
|
409
|
-
other = double(
|
445
|
+
it "is false when object #== is false" do
|
446
|
+
object = Model.new
|
447
|
+
decorator = Decorator.new(object)
|
448
|
+
other = double(object: Model.new)
|
410
449
|
|
411
|
-
|
450
|
+
object.should_receive(:==).with(other).and_return(false)
|
412
451
|
expect(decorator == other).to be_false
|
413
452
|
end
|
414
453
|
|
@@ -433,8 +472,8 @@ module Draper
|
|
433
472
|
describe ".delegate" do
|
434
473
|
protect_class Decorator
|
435
474
|
|
436
|
-
it "defaults the :to option to :
|
437
|
-
Object.should_receive(:delegate).with(:foo, :bar, to: :
|
475
|
+
it "defaults the :to option to :object" do
|
476
|
+
Object.should_receive(:delegate).with(:foo, :bar, to: :object)
|
438
477
|
Decorator.delegate :foo, :bar
|
439
478
|
end
|
440
479
|
|
@@ -450,7 +489,7 @@ module Draper
|
|
450
489
|
before { Decorator.delegate_all }
|
451
490
|
|
452
491
|
describe "#method_missing" do
|
453
|
-
it "delegates missing methods that exist on the
|
492
|
+
it "delegates missing methods that exist on the object" do
|
454
493
|
decorator = Decorator.new(double(hello_world: :delegated))
|
455
494
|
|
456
495
|
expect(decorator.hello_world).to be :delegated
|
@@ -465,9 +504,9 @@ module Draper
|
|
465
504
|
end
|
466
505
|
|
467
506
|
it "passes blocks to delegated methods" do
|
468
|
-
|
469
|
-
|
470
|
-
decorator = Decorator.new(
|
507
|
+
object = Model.new
|
508
|
+
object.stub(:hello_world).and_return{|*args, &block| block.call}
|
509
|
+
decorator = Decorator.new(object)
|
471
510
|
|
472
511
|
expect(decorator.hello_world{:yielded}).to be :yielded
|
473
512
|
end
|
@@ -479,21 +518,21 @@ module Draper
|
|
479
518
|
end
|
480
519
|
|
481
520
|
it "delegates already-delegated methods" do
|
482
|
-
|
483
|
-
|
484
|
-
decorator = Decorator.new(
|
521
|
+
object = Class.new{ delegate :bar, to: :foo }.new
|
522
|
+
object.stub foo: double(bar: :delegated)
|
523
|
+
decorator = Decorator.new(object)
|
485
524
|
|
486
525
|
expect(decorator.bar).to be :delegated
|
487
526
|
end
|
488
527
|
|
489
528
|
it "does not delegate private methods" do
|
490
|
-
|
491
|
-
decorator = Decorator.new(
|
529
|
+
object = Class.new{ private; def hello_world; end }.new
|
530
|
+
decorator = Decorator.new(object)
|
492
531
|
|
493
532
|
expect{decorator.hello_world}.to raise_error NoMethodError
|
494
533
|
end
|
495
534
|
|
496
|
-
it "does not delegate methods that do not exist on the
|
535
|
+
it "does not delegate methods that do not exist on the object" do
|
497
536
|
decorator = Decorator.new(Model.new)
|
498
537
|
|
499
538
|
expect(decorator.methods).not_to include :hello_world
|
@@ -511,15 +550,15 @@ module Draper
|
|
511
550
|
|
512
551
|
context "with a source class" do
|
513
552
|
it "delegates methods that exist on the source class" do
|
514
|
-
|
515
|
-
|
516
|
-
Decorator.stub
|
553
|
+
object_class = Class.new
|
554
|
+
object_class.stub hello_world: :delegated
|
555
|
+
Decorator.stub object_class: object_class
|
517
556
|
|
518
557
|
expect(Decorator.hello_world).to be :delegated
|
519
558
|
end
|
520
559
|
|
521
560
|
it "does not delegate methods that do not exist on the source class" do
|
522
|
-
Decorator.stub
|
561
|
+
Decorator.stub object_class: Class.new
|
523
562
|
|
524
563
|
expect{Decorator.hello_world}.to raise_error NoMethodError
|
525
564
|
end
|
@@ -534,7 +573,7 @@ module Draper
|
|
534
573
|
expect(decorator).to respond_to :hello_world
|
535
574
|
end
|
536
575
|
|
537
|
-
it "returns true for the
|
576
|
+
it "returns true for the object's methods" do
|
538
577
|
decorator = Decorator.new(double(hello_world: :delegated))
|
539
578
|
|
540
579
|
expect(decorator).to respond_to :hello_world
|
@@ -548,9 +587,9 @@ module Draper
|
|
548
587
|
expect(decorator.respond_to?(:hello_world, true)).to be_true
|
549
588
|
end
|
550
589
|
|
551
|
-
it "returns false for the
|
552
|
-
|
553
|
-
decorator = Decorator.new(
|
590
|
+
it "returns false for the object's private methods" do
|
591
|
+
object = Class.new{private; def hello_world; end}.new
|
592
|
+
decorator = Decorator.new(object)
|
554
593
|
|
555
594
|
expect(decorator.respond_to?(:hello_world, true)).to be_false
|
556
595
|
end
|
@@ -573,13 +612,13 @@ module Draper
|
|
573
612
|
context "with a source class" do
|
574
613
|
it "returns true for its own class methods" do
|
575
614
|
Decorator.class_eval{def self.hello_world; end}
|
576
|
-
Decorator.stub
|
615
|
+
Decorator.stub object_class: Class.new
|
577
616
|
|
578
617
|
expect(Decorator).to respond_to :hello_world
|
579
618
|
end
|
580
619
|
|
581
620
|
it "returns true for the source's class methods" do
|
582
|
-
Decorator.stub
|
621
|
+
Decorator.stub object_class: double(hello_world: :delegated)
|
583
622
|
|
584
623
|
expect(Decorator).to respond_to :hello_world
|
585
624
|
end
|
@@ -588,8 +627,8 @@ module Draper
|
|
588
627
|
|
589
628
|
describe "#respond_to_missing?" do
|
590
629
|
it "allows #method to be called on delegated methods" do
|
591
|
-
|
592
|
-
decorator = Decorator.new(
|
630
|
+
object = Class.new{def hello_world; end}.new
|
631
|
+
decorator = Decorator.new(object)
|
593
632
|
|
594
633
|
expect { decorator.method(:hello_world) }.not_to raise_error NameError
|
595
634
|
expect(decorator.method(:hello_world)).not_to be_nil
|
@@ -598,7 +637,7 @@ module Draper
|
|
598
637
|
|
599
638
|
describe ".respond_to_missing?" do
|
600
639
|
it "allows .method to be called on delegated class methods" do
|
601
|
-
Decorator.stub
|
640
|
+
Decorator.stub object_class: double(hello_world: :delegated)
|
602
641
|
|
603
642
|
expect { Decorator.method(:hello_world) }.not_to raise_error NameError
|
604
643
|
expect(Decorator.method(:hello_world)).not_to be_nil
|