draper_new 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +230 -0
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +16 -0
- data/Guardfile +26 -0
- data/LICENSE +7 -0
- data/README.md +587 -0
- data/Rakefile +69 -0
- data/draper_new.gemspec +31 -0
- data/gemfiles/4.0.gemfile +3 -0
- data/gemfiles/4.1.gemfile +3 -0
- data/gemfiles/4.2.6.gemfile +3 -0
- data/gemfiles/4.2.gemfile +3 -0
- data/lib/draper.rb +63 -0
- data/lib/draper/automatic_delegation.rb +56 -0
- data/lib/draper/collection_decorator.rb +100 -0
- data/lib/draper/decoratable.rb +96 -0
- data/lib/draper/decoratable/equality.rb +26 -0
- data/lib/draper/decorated_association.rb +35 -0
- data/lib/draper/decorates_assigned.rb +44 -0
- data/lib/draper/decorator.rb +293 -0
- data/lib/draper/delegation.rb +13 -0
- data/lib/draper/factory.rb +91 -0
- data/lib/draper/finders.rb +37 -0
- data/lib/draper/helper_proxy.rb +44 -0
- data/lib/draper/helper_support.rb +5 -0
- data/lib/draper/lazy_helpers.rb +15 -0
- data/lib/draper/railtie.rb +70 -0
- data/lib/draper/tasks/test.rake +22 -0
- data/lib/draper/test/devise_helper.rb +30 -0
- data/lib/draper/test/minitest_integration.rb +6 -0
- data/lib/draper/test/rspec_integration.rb +20 -0
- data/lib/draper/test_case.rb +42 -0
- data/lib/draper/undecorate.rb +9 -0
- data/lib/draper/version.rb +3 -0
- data/lib/draper/view_context.rb +104 -0
- data/lib/draper/view_context/build_strategy.rb +48 -0
- data/lib/draper/view_helpers.rb +37 -0
- data/lib/generators/controller_override.rb +17 -0
- data/lib/generators/mini_test/decorator_generator.rb +20 -0
- data/lib/generators/mini_test/templates/decorator_spec.rb +4 -0
- data/lib/generators/mini_test/templates/decorator_test.rb +4 -0
- data/lib/generators/rails/decorator_generator.rb +36 -0
- data/lib/generators/rails/templates/decorator.rb +19 -0
- data/lib/generators/rspec/decorator_generator.rb +9 -0
- data/lib/generators/rspec/templates/decorator_spec.rb +4 -0
- data/lib/generators/test_unit/decorator_generator.rb +9 -0
- data/lib/generators/test_unit/templates/decorator_test.rb +4 -0
- data/spec/draper/collection_decorator_spec.rb +307 -0
- data/spec/draper/decoratable/equality_spec.rb +10 -0
- data/spec/draper/decoratable_spec.rb +202 -0
- data/spec/draper/decorated_association_spec.rb +84 -0
- data/spec/draper/decorates_assigned_spec.rb +71 -0
- data/spec/draper/decorator_spec.rb +816 -0
- data/spec/draper/factory_spec.rb +251 -0
- data/spec/draper/finders_spec.rb +166 -0
- data/spec/draper/helper_proxy_spec.rb +61 -0
- data/spec/draper/lazy_helpers_spec.rb +21 -0
- data/spec/draper/undecorate_spec.rb +19 -0
- data/spec/draper/view_context/build_strategy_spec.rb +116 -0
- data/spec/draper/view_context_spec.rb +154 -0
- data/spec/draper/view_helpers_spec.rb +8 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/localized_urls.rb +5 -0
- data/spec/dummy/app/controllers/posts_controller.rb +20 -0
- data/spec/dummy/app/decorators/mongoid_post_decorator.rb +4 -0
- data/spec/dummy/app/decorators/post_decorator.rb +60 -0
- data/spec/dummy/app/helpers/application_helper.rb +5 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -0
- data/spec/dummy/app/mailers/post_mailer.rb +19 -0
- data/spec/dummy/app/models/admin.rb +5 -0
- data/spec/dummy/app/models/mongoid_post.rb +5 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +11 -0
- data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
- data/spec/dummy/app/views/posts/_post.html.erb +40 -0
- data/spec/dummy/app/views/posts/show.html.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +71 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +33 -0
- data/spec/dummy/config/environments/production.rb +57 -0
- data/spec/dummy/config/environments/test.rb +31 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +8 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/mongoid.yml +79 -0
- data/spec/dummy/config/routes.rb +9 -0
- data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
- data/spec/dummy/db/schema.rb +21 -0
- data/spec/dummy/db/seeds.rb +2 -0
- data/spec/dummy/fast_spec/post_decorator_spec.rb +37 -0
- data/spec/dummy/lib/tasks/test.rake +16 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +16 -0
- data/spec/dummy/spec/decorators/devise_spec.rb +64 -0
- data/spec/dummy/spec/decorators/helpers_spec.rb +21 -0
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +66 -0
- data/spec/dummy/spec/decorators/spec_type_spec.rb +7 -0
- data/spec/dummy/spec/decorators/view_context_spec.rb +22 -0
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +33 -0
- data/spec/dummy/spec/models/mongoid_post_spec.rb +8 -0
- data/spec/dummy/spec/models/post_spec.rb +6 -0
- data/spec/dummy/spec/shared_examples/decoratable.rb +24 -0
- data/spec/dummy/spec/spec_helper.rb +8 -0
- data/spec/dummy/test/decorators/minitest/devise_test.rb +64 -0
- data/spec/dummy/test/decorators/minitest/helpers_test.rb +21 -0
- data/spec/dummy/test/decorators/minitest/spec_type_test.rb +52 -0
- data/spec/dummy/test/decorators/minitest/view_context_test.rb +24 -0
- data/spec/dummy/test/decorators/test_unit/devise_test.rb +64 -0
- data/spec/dummy/test/decorators/test_unit/helpers_test.rb +21 -0
- data/spec/dummy/test/decorators/test_unit/view_context_test.rb +24 -0
- data/spec/dummy/test/minitest_helper.rb +2 -0
- data/spec/dummy/test/test_helper.rb +3 -0
- data/spec/generators/controller/controller_generator_spec.rb +22 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +92 -0
- data/spec/integration/integration_spec.rb +66 -0
- data/spec/performance/active_record.rb +4 -0
- data/spec/performance/benchmark.rb +55 -0
- data/spec/performance/decorators.rb +45 -0
- data/spec/performance/models.rb +20 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/support/dummy_app.rb +85 -0
- data/spec/support/matchers/have_text.rb +50 -0
- data/spec/support/shared_examples/decoratable_equality.rb +40 -0
- data/spec/support/shared_examples/view_helpers.rb +39 -0
- metadata +420 -0
@@ -0,0 +1,251 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Draper
|
4
|
+
describe Factory do
|
5
|
+
|
6
|
+
describe "#initialize" do
|
7
|
+
it "accepts valid options" do
|
8
|
+
valid_options = {with: Decorator, context: {foo: "bar"}}
|
9
|
+
expect{Factory.new(valid_options)}.not_to raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it "rejects invalid options" do
|
13
|
+
expect{Factory.new(foo: "bar")}.to raise_error ArgumentError, /Unknown key/
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#decorate" do
|
18
|
+
context "when object is nil" do
|
19
|
+
it "returns nil" do
|
20
|
+
factory = Factory.new
|
21
|
+
|
22
|
+
expect(factory.decorate(nil)).to be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "calls a worker" do
|
27
|
+
factory = Factory.new
|
28
|
+
worker = ->(*){ :decorated }
|
29
|
+
|
30
|
+
Factory::Worker.should_receive(:new).and_return(worker)
|
31
|
+
expect(factory.decorate(double)).to be :decorated
|
32
|
+
end
|
33
|
+
|
34
|
+
it "passes the object to the worker" do
|
35
|
+
factory = Factory.new
|
36
|
+
object = double
|
37
|
+
|
38
|
+
Factory::Worker.should_receive(:new).with(anything(), object).and_return(->(*){})
|
39
|
+
factory.decorate(object)
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when the :with option was given" do
|
43
|
+
it "passes the decorator class to the worker" do
|
44
|
+
decorator_class = double
|
45
|
+
factory = Factory.new(with: decorator_class)
|
46
|
+
|
47
|
+
Factory::Worker.should_receive(:new).with(decorator_class, anything()).and_return(->(*){})
|
48
|
+
factory.decorate(double)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when the :with option was omitted" do
|
53
|
+
it "passes nil to the worker" do
|
54
|
+
factory = Factory.new
|
55
|
+
|
56
|
+
Factory::Worker.should_receive(:new).with(nil, anything()).and_return(->(*){})
|
57
|
+
factory.decorate(double)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "passes options to the call" do
|
62
|
+
factory = Factory.new
|
63
|
+
worker = ->(*){}
|
64
|
+
Factory::Worker.stub new: worker
|
65
|
+
options = {foo: "bar"}
|
66
|
+
|
67
|
+
worker.should_receive(:call).with(options)
|
68
|
+
factory.decorate(double, options)
|
69
|
+
end
|
70
|
+
|
71
|
+
context "when the :context option was given" do
|
72
|
+
it "sets the passed context" do
|
73
|
+
factory = Factory.new(context: {foo: "bar"})
|
74
|
+
worker = ->(*){}
|
75
|
+
Factory::Worker.stub new: worker
|
76
|
+
|
77
|
+
worker.should_receive(:call).with(baz: "qux", context: {foo: "bar"})
|
78
|
+
factory.decorate(double, {baz: "qux"})
|
79
|
+
end
|
80
|
+
|
81
|
+
it "is overridden by explicitly-specified context" do
|
82
|
+
factory = Factory.new(context: {foo: "bar"})
|
83
|
+
worker = ->(*){}
|
84
|
+
Factory::Worker.stub new: worker
|
85
|
+
|
86
|
+
worker.should_receive(:call).with(context: {baz: "qux"})
|
87
|
+
factory.decorate(double, context: {baz: "qux"})
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
describe Factory::Worker do
|
95
|
+
|
96
|
+
describe "#call" do
|
97
|
+
it "calls the decorator method" do
|
98
|
+
object = double
|
99
|
+
options = {foo: "bar"}
|
100
|
+
worker = Factory::Worker.new(double, object)
|
101
|
+
decorator = ->(*){}
|
102
|
+
allow(worker).to receive(:decorator){ decorator }
|
103
|
+
|
104
|
+
decorator.should_receive(:call).with(object, options).and_return(:decorated)
|
105
|
+
expect(worker.call(options)).to be :decorated
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when the :context option is callable" do
|
109
|
+
it "calls it" do
|
110
|
+
worker = Factory::Worker.new(double, double)
|
111
|
+
decorator = ->(*){}
|
112
|
+
worker.stub decorator: decorator
|
113
|
+
context = {foo: "bar"}
|
114
|
+
|
115
|
+
decorator.should_receive(:call).with(anything(), context: context)
|
116
|
+
worker.call(context: ->{ context })
|
117
|
+
end
|
118
|
+
|
119
|
+
it "receives arguments from the :context_args option" do
|
120
|
+
worker = Factory::Worker.new(double, double)
|
121
|
+
worker.stub decorator: ->(*){}
|
122
|
+
context = ->{}
|
123
|
+
|
124
|
+
context.should_receive(:call).with(:foo, :bar)
|
125
|
+
worker.call(context: context, context_args: [:foo, :bar])
|
126
|
+
end
|
127
|
+
|
128
|
+
it "wraps non-arrays passed to :context_args" do
|
129
|
+
worker = Factory::Worker.new(double, double)
|
130
|
+
worker.stub decorator: ->(*){}
|
131
|
+
context = ->{}
|
132
|
+
hash = {foo: "bar"}
|
133
|
+
|
134
|
+
context.should_receive(:call).with(hash)
|
135
|
+
worker.call(context: context, context_args: hash)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "when the :context option is not callable" do
|
140
|
+
it "doesn't call it" do
|
141
|
+
worker = Factory::Worker.new(double, double)
|
142
|
+
decorator = ->(*){}
|
143
|
+
worker.stub decorator: decorator
|
144
|
+
context = {foo: "bar"}
|
145
|
+
|
146
|
+
decorator.should_receive(:call).with(anything(), context: context)
|
147
|
+
worker.call(context: context)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
it "does not pass the :context_args option to the decorator" do
|
152
|
+
worker = Factory::Worker.new(double, double)
|
153
|
+
decorator = ->(*){}
|
154
|
+
worker.stub decorator: decorator
|
155
|
+
|
156
|
+
decorator.should_receive(:call).with(anything(), foo: "bar")
|
157
|
+
worker.call(foo: "bar", context_args: [])
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "#decorator" do
|
162
|
+
context "for a singular object" do
|
163
|
+
context "when decorator_class is specified" do
|
164
|
+
it "returns the .decorate method from the decorator" do
|
165
|
+
decorator_class = Class.new(Decorator)
|
166
|
+
worker = Factory::Worker.new(decorator_class, double)
|
167
|
+
|
168
|
+
expect(worker.decorator).to eq decorator_class.method(:decorate)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context "when decorator_class is unspecified" do
|
173
|
+
context "and the object is decoratable" do
|
174
|
+
it "returns the object's #decorate method" do
|
175
|
+
object = double
|
176
|
+
options = {foo: "bar"}
|
177
|
+
worker = Factory::Worker.new(nil, object)
|
178
|
+
|
179
|
+
object.should_receive(:decorate).with(options).and_return(:decorated)
|
180
|
+
expect(worker.decorator.call(object, options)).to be :decorated
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
context "and the object is not decoratable" do
|
185
|
+
it "raises an error" do
|
186
|
+
object = double
|
187
|
+
worker = Factory::Worker.new(nil, object)
|
188
|
+
|
189
|
+
expect{worker.decorator}.to raise_error UninferrableDecoratorError
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context "when the object is a struct" do
|
195
|
+
it "returns a singular decorator" do
|
196
|
+
object = Struct.new(:stuff).new("things")
|
197
|
+
|
198
|
+
decorator_class = Class.new(Decorator)
|
199
|
+
worker = Factory::Worker.new(decorator_class, object)
|
200
|
+
|
201
|
+
expect(worker.decorator).to eq decorator_class.method(:decorate)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
context "for a collection object" do
|
207
|
+
context "when decorator_class is a CollectionDecorator" do
|
208
|
+
it "returns the .decorate method from the collection decorator" do
|
209
|
+
decorator_class = Class.new(CollectionDecorator)
|
210
|
+
worker = Factory::Worker.new(decorator_class, [])
|
211
|
+
|
212
|
+
expect(worker.decorator).to eq decorator_class.method(:decorate)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
context "when decorator_class is a Decorator" do
|
217
|
+
it "returns the .decorate_collection method from the decorator" do
|
218
|
+
decorator_class = Class.new(Decorator)
|
219
|
+
worker = Factory::Worker.new(decorator_class, [])
|
220
|
+
|
221
|
+
expect(worker.decorator).to eq decorator_class.method(:decorate_collection)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
context "when decorator_class is unspecified" do
|
226
|
+
context "and the object is decoratable" do
|
227
|
+
it "returns the .decorate_collection method from the object's decorator" do
|
228
|
+
object = []
|
229
|
+
decorator_class = Class.new(Decorator)
|
230
|
+
allow(object).to receive(:decorator_class){ decorator_class }
|
231
|
+
allow(object).to receive(:decorate){ nil }
|
232
|
+
worker = Factory::Worker.new(nil, object)
|
233
|
+
|
234
|
+
decorator_class.should_receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated)
|
235
|
+
expect(worker.decorator.call(object, foo: "bar")).to be :decorated
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
context "and the object is not decoratable" do
|
240
|
+
it "returns the .decorate method from CollectionDecorator" do
|
241
|
+
worker = Factory::Worker.new(nil, [])
|
242
|
+
|
243
|
+
expect(worker.decorator).to eq CollectionDecorator.method(:decorate)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Draper
|
4
|
+
describe Finders do
|
5
|
+
protect_class ProductDecorator
|
6
|
+
before { ProductDecorator.decorates_finders }
|
7
|
+
|
8
|
+
describe ".find" do
|
9
|
+
it "proxies to the model class" do
|
10
|
+
Product.should_receive(:find).with(1)
|
11
|
+
ProductDecorator.find(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "decorates the result" do
|
15
|
+
found = Product.new
|
16
|
+
Product.stub(:find).and_return(found)
|
17
|
+
decorator = ProductDecorator.find(1)
|
18
|
+
expect(decorator).to be_a ProductDecorator
|
19
|
+
expect(decorator.object).to be found
|
20
|
+
end
|
21
|
+
|
22
|
+
it "passes context to the decorator" do
|
23
|
+
Product.stub(:find)
|
24
|
+
context = {some: "context"}
|
25
|
+
decorator = ProductDecorator.find(1, context: context)
|
26
|
+
|
27
|
+
expect(decorator.context).to be context
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".find_by_(x)" do
|
32
|
+
it "proxies to the model class" do
|
33
|
+
Product.should_receive(:find_by_name).with("apples")
|
34
|
+
ProductDecorator.find_by_name("apples")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "decorates the result" do
|
38
|
+
found = Product.new
|
39
|
+
Product.stub(:find_by_name).and_return(found)
|
40
|
+
decorator = ProductDecorator.find_by_name("apples")
|
41
|
+
expect(decorator).to be_a ProductDecorator
|
42
|
+
expect(decorator.object).to be found
|
43
|
+
end
|
44
|
+
|
45
|
+
it "proxies complex ProductDecorators" do
|
46
|
+
Product.should_receive(:find_by_name_and_size).with("apples", "large")
|
47
|
+
ProductDecorator.find_by_name_and_size("apples", "large")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "proxies find_last_by_(x) ProductDecorators" do
|
51
|
+
Product.should_receive(:find_last_by_name_and_size).with("apples", "large")
|
52
|
+
ProductDecorator.find_last_by_name_and_size("apples", "large")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "proxies find_or_initialize_by_(x) ProductDecorators" do
|
56
|
+
Product.should_receive(:find_or_initialize_by_name_and_size).with("apples", "large")
|
57
|
+
ProductDecorator.find_or_initialize_by_name_and_size("apples", "large")
|
58
|
+
end
|
59
|
+
|
60
|
+
it "proxies find_or_create_by_(x) ProductDecorators" do
|
61
|
+
Product.should_receive(:find_or_create_by_name_and_size).with("apples", "large")
|
62
|
+
ProductDecorator.find_or_create_by_name_and_size("apples", "large")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "passes context to the decorator" do
|
66
|
+
Product.stub(:find_by_name_and_size)
|
67
|
+
context = {some: "context"}
|
68
|
+
decorator = ProductDecorator.find_by_name_and_size("apples", "large", context: context)
|
69
|
+
|
70
|
+
expect(decorator.context).to be context
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe ".find_all_by_" do
|
75
|
+
it "proxies to the model class" do
|
76
|
+
Product.should_receive(:find_all_by_name_and_size).with("apples", "large").and_return([])
|
77
|
+
ProductDecorator.find_all_by_name_and_size("apples", "large")
|
78
|
+
end
|
79
|
+
|
80
|
+
it "decorates the result" do
|
81
|
+
found = [Product.new, Product.new]
|
82
|
+
Product.stub(:find_all_by_name).and_return(found)
|
83
|
+
decorator = ProductDecorator.find_all_by_name("apples")
|
84
|
+
|
85
|
+
expect(decorator).to be_a Draper::CollectionDecorator
|
86
|
+
expect(decorator.decorator_class).to be ProductDecorator
|
87
|
+
expect(decorator).to eq found
|
88
|
+
end
|
89
|
+
|
90
|
+
it "passes context to the decorator" do
|
91
|
+
Product.stub(:find_all_by_name)
|
92
|
+
context = {some: "context"}
|
93
|
+
decorator = ProductDecorator.find_all_by_name("apples", context: context)
|
94
|
+
|
95
|
+
expect(decorator.context).to be context
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe ".all" do
|
100
|
+
it "returns a decorated collection" do
|
101
|
+
found = [Product.new, Product.new]
|
102
|
+
Product.stub all: found
|
103
|
+
decorator = ProductDecorator.all
|
104
|
+
|
105
|
+
expect(decorator).to be_a Draper::CollectionDecorator
|
106
|
+
expect(decorator.decorator_class).to be ProductDecorator
|
107
|
+
expect(decorator).to eq found
|
108
|
+
end
|
109
|
+
|
110
|
+
it "passes context to the decorator" do
|
111
|
+
Product.stub(:all)
|
112
|
+
context = {some: "context"}
|
113
|
+
decorator = ProductDecorator.all(context: context)
|
114
|
+
|
115
|
+
expect(decorator.context).to be context
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe ".first" do
|
120
|
+
it "proxies to the model class" do
|
121
|
+
Product.should_receive(:first)
|
122
|
+
ProductDecorator.first
|
123
|
+
end
|
124
|
+
|
125
|
+
it "decorates the result" do
|
126
|
+
first = Product.new
|
127
|
+
Product.stub(:first).and_return(first)
|
128
|
+
decorator = ProductDecorator.first
|
129
|
+
expect(decorator).to be_a ProductDecorator
|
130
|
+
expect(decorator.object).to be first
|
131
|
+
end
|
132
|
+
|
133
|
+
it "passes context to the decorator" do
|
134
|
+
Product.stub(:first)
|
135
|
+
context = {some: "context"}
|
136
|
+
decorator = ProductDecorator.first(context: context)
|
137
|
+
|
138
|
+
expect(decorator.context).to be context
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe ".last" do
|
143
|
+
it "proxies to the model class" do
|
144
|
+
Product.should_receive(:last)
|
145
|
+
ProductDecorator.last
|
146
|
+
end
|
147
|
+
|
148
|
+
it "decorates the result" do
|
149
|
+
last = Product.new
|
150
|
+
Product.stub(:last).and_return(last)
|
151
|
+
decorator = ProductDecorator.last
|
152
|
+
expect(decorator).to be_a ProductDecorator
|
153
|
+
expect(decorator.object).to be last
|
154
|
+
end
|
155
|
+
|
156
|
+
it "passes context to the decorator" do
|
157
|
+
Product.stub(:last)
|
158
|
+
context = {some: "context"}
|
159
|
+
decorator = ProductDecorator.last(context: context)
|
160
|
+
|
161
|
+
expect(decorator.context).to be context
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Draper
|
4
|
+
describe HelperProxy do
|
5
|
+
describe "#initialize" do
|
6
|
+
it "sets the view context" do
|
7
|
+
view_context = double
|
8
|
+
helper_proxy = HelperProxy.new(view_context)
|
9
|
+
|
10
|
+
expect(helper_proxy.send(:view_context)).to be view_context
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#method_missing" do
|
15
|
+
protect_class HelperProxy
|
16
|
+
|
17
|
+
it "proxies methods to the view context" do
|
18
|
+
view_context = double
|
19
|
+
helper_proxy = HelperProxy.new(view_context)
|
20
|
+
|
21
|
+
view_context.stub(:foo) { |arg| arg }
|
22
|
+
expect(helper_proxy.foo(:passed)).to be :passed
|
23
|
+
end
|
24
|
+
|
25
|
+
it "passes blocks" do
|
26
|
+
view_context = double
|
27
|
+
helper_proxy = HelperProxy.new(view_context)
|
28
|
+
|
29
|
+
view_context.stub(:foo) { |&block| block.call }
|
30
|
+
expect(helper_proxy.foo{:yielded}).to be :yielded
|
31
|
+
end
|
32
|
+
|
33
|
+
it "defines the method for better performance" do
|
34
|
+
helper_proxy = HelperProxy.new(double(foo: "bar"))
|
35
|
+
|
36
|
+
expect(HelperProxy.instance_methods).not_to include :foo
|
37
|
+
helper_proxy.foo
|
38
|
+
expect(HelperProxy.instance_methods).to include :foo
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#respond_to_missing?" do
|
43
|
+
it "allows #method to be called on the view context" do
|
44
|
+
helper_proxy = HelperProxy.new(double(foo: "bar"))
|
45
|
+
|
46
|
+
expect(helper_proxy.respond_to?(:foo)).to be_truthy
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "proxying methods which are overriding" do
|
51
|
+
it "proxies :capture" do
|
52
|
+
view_context = double
|
53
|
+
helper_proxy = HelperProxy.new(view_context)
|
54
|
+
|
55
|
+
allow(view_context).to receive(:capture) { |*args, &block| [*args, block.call] }
|
56
|
+
expect(helper_proxy.capture(:first_arg, :second_arg){:yielded}).to \
|
57
|
+
be_eql [:first_arg, :second_arg, :yielded]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|