draper 4.0.4 → 4.0.5
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 +6 -0
- data/README.md +3 -6
- data/lib/draper/collection_decorator.rb +0 -6
- data/lib/draper/compatibility/broadcastable.rb +24 -0
- data/lib/draper/compatibility/global_id.rb +9 -5
- data/lib/draper/decoratable.rb +5 -0
- data/lib/draper/decorator.rb +2 -18
- data/lib/draper/railtie.rb +6 -1
- data/lib/draper/version.rb +1 -1
- metadata +4 -232
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -24
- data/.github/workflows/ci.yml +0 -71
- data/.gitignore +0 -18
- data/Gemfile +0 -46
- data/bin/bundle +0 -114
- data/bin/rake +0 -29
- data/draper.gemspec +0 -34
- data/spec/draper/collection_decorator_spec.rb +0 -291
- data/spec/draper/configuration_spec.rb +0 -49
- data/spec/draper/decoratable/equality_spec.rb +0 -10
- data/spec/draper/decoratable_spec.rb +0 -235
- data/spec/draper/decorated_association_spec.rb +0 -82
- data/spec/draper/decorates_assigned_spec.rb +0 -71
- data/spec/draper/decorator_spec.rb +0 -872
- data/spec/draper/draper_spec.rb +0 -25
- data/spec/draper/factory_spec.rb +0 -247
- data/spec/draper/finders_spec.rb +0 -166
- data/spec/draper/helper_proxy_spec.rb +0 -61
- data/spec/draper/lazy_helpers_spec.rb +0 -21
- data/spec/draper/query_methods/load_strategy_spec.rb +0 -26
- data/spec/draper/query_methods_spec.rb +0 -80
- data/spec/draper/undecorate_chain_spec.rb +0 -20
- data/spec/draper/undecorate_spec.rb +0 -19
- data/spec/draper/view_context/build_strategy_spec.rb +0 -115
- data/spec/draper/view_context_spec.rb +0 -182
- data/spec/draper/view_helpers_spec.rb +0 -8
- data/spec/dummy/.rspec +0 -1
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/assets/config/manifest.js +0 -3
- data/spec/dummy/app/controllers/application_controller.rb +0 -2
- data/spec/dummy/app/controllers/base_controller.rb +0 -4
- data/spec/dummy/app/controllers/localized_urls.rb +0 -5
- data/spec/dummy/app/controllers/posts_controller.rb +0 -20
- data/spec/dummy/app/decorators/comment_decorator.rb +0 -13
- data/spec/dummy/app/decorators/mongoid_post_decorator.rb +0 -2
- data/spec/dummy/app/decorators/post_decorator.rb +0 -60
- data/spec/dummy/app/helpers/application_helper.rb +0 -5
- data/spec/dummy/app/jobs/publish_post_job.rb +0 -7
- data/spec/dummy/app/mailers/application_mailer.rb +0 -3
- data/spec/dummy/app/mailers/post_mailer.rb +0 -19
- data/spec/dummy/app/models/admin.rb +0 -3
- data/spec/dummy/app/models/application_record.rb +0 -3
- data/spec/dummy/app/models/comment.rb +0 -3
- data/spec/dummy/app/models/mongoid_post.rb +0 -3
- data/spec/dummy/app/models/post.rb +0 -7
- data/spec/dummy/app/models/user.rb +0 -3
- data/spec/dummy/app/views/layouts/application.html.erb +0 -11
- data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +0 -1
- data/spec/dummy/app/views/posts/_post.html.erb +0 -42
- data/spec/dummy/app/views/posts/show.html.erb +0 -1
- data/spec/dummy/bin/rails +0 -4
- data/spec/dummy/config/application.rb +0 -39
- data/spec/dummy/config/boot.rb +0 -5
- data/spec/dummy/config/database.yml +0 -25
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -78
- data/spec/dummy/config/environments/production.rb +0 -102
- data/spec/dummy/config/environments/test.rb +0 -69
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/draper.rb +0 -5
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy/config/initializers/inflections.rb +0 -15
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -8
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/mongoid.yml +0 -142
- data/spec/dummy/config/routes.rb +0 -9
- data/spec/dummy/config/storage.yml +0 -7
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/db/migrate/20121019115657_create_posts.rb +0 -8
- data/spec/dummy/db/migrate/20240907041839_create_comments.rb +0 -9
- data/spec/dummy/db/schema.rb +0 -28
- data/spec/dummy/db/seeds.rb +0 -2
- data/spec/dummy/fast_spec/post_decorator_spec.rb +0 -37
- data/spec/dummy/lib/tasks/test.rake +0 -16
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -25
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +0 -6
- data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +0 -12
- data/spec/dummy/spec/decorators/devise_spec.rb +0 -55
- data/spec/dummy/spec/decorators/helpers_spec.rb +0 -21
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +0 -64
- data/spec/dummy/spec/decorators/spec_type_spec.rb +0 -7
- data/spec/dummy/spec/decorators/view_context_spec.rb +0 -22
- data/spec/dummy/spec/jobs/publish_post_job_spec.rb +0 -11
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +0 -25
- data/spec/dummy/spec/models/application_spec.rb +0 -7
- data/spec/dummy/spec/models/mongoid_post_spec.rb +0 -8
- data/spec/dummy/spec/models/post_spec.rb +0 -51
- data/spec/dummy/spec/rails_helper.rb +0 -69
- data/spec/dummy/spec/shared_examples/decoratable.rb +0 -22
- data/spec/dummy/spec/spec_helper.rb +0 -93
- data/spec/dummy/test/decorators/minitest/devise_test.rb +0 -55
- data/spec/dummy/test/decorators/minitest/helpers_test.rb +0 -21
- data/spec/dummy/test/decorators/minitest/spec_type_test.rb +0 -52
- data/spec/dummy/test/decorators/minitest/view_context_test.rb +0 -24
- data/spec/dummy/test/decorators/test_unit/devise_test.rb +0 -55
- data/spec/dummy/test/decorators/test_unit/helpers_test.rb +0 -21
- data/spec/dummy/test/decorators/test_unit/view_context_test.rb +0 -24
- data/spec/dummy/test/minitest_helper.rb +0 -2
- data/spec/dummy/test/test_helper.rb +0 -3
- data/spec/generators/controller/controller_generator_spec.rb +0 -23
- data/spec/generators/decorator/decorator_generator_spec.rb +0 -93
- data/spec/generators/install/install_generator_spec.rb +0 -19
- data/spec/integration/integration_spec.rb +0 -70
- data/spec/performance/active_record.rb +0 -4
- data/spec/performance/benchmark.rb +0 -55
- data/spec/performance/decorators.rb +0 -45
- data/spec/performance/models.rb +0 -20
- data/spec/spec_helper.rb +0 -50
- data/spec/support/dummy_app.rb +0 -85
- data/spec/support/matchers/have_text.rb +0 -50
- data/spec/support/shared_examples/decoratable_equality.rb +0 -40
- data/spec/support/shared_examples/view_helpers.rb +0 -39
data/spec/draper/draper_spec.rb
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'support/shared_examples/view_helpers'
|
|
3
|
-
SimpleCov.command_name 'test:unit'
|
|
4
|
-
|
|
5
|
-
module Draper
|
|
6
|
-
describe Draper do
|
|
7
|
-
describe '.setup_action_controller' do
|
|
8
|
-
it 'includes api only compatability if base is ActionController::API' do
|
|
9
|
-
base = ActionController::API
|
|
10
|
-
|
|
11
|
-
Draper.setup_action_controller(base)
|
|
12
|
-
|
|
13
|
-
expect(base.included_modules).to include(Draper::Compatibility::ApiOnly)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'does not include api only compatibility if base ActionController::Base' do
|
|
17
|
-
base = ActionController::Base
|
|
18
|
-
|
|
19
|
-
Draper.setup_action_controller(base)
|
|
20
|
-
|
|
21
|
-
expect(base.included_modules).not_to include(Draper::Compatibility::ApiOnly)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
data/spec/draper/factory_spec.rb
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
module Draper
|
|
4
|
-
describe Factory do
|
|
5
|
-
describe "#initialize" do
|
|
6
|
-
it "accepts valid options" do
|
|
7
|
-
valid_options = {with: Decorator, context: {foo: "bar"}}
|
|
8
|
-
expect{Factory.new(valid_options)}.not_to raise_error
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it "rejects invalid options" do
|
|
12
|
-
expect{Factory.new(foo: "bar")}.to raise_error ArgumentError, /Unknown key/
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
describe "#decorate" do
|
|
17
|
-
context "when object is nil" do
|
|
18
|
-
it "returns nil" do
|
|
19
|
-
factory = Factory.new
|
|
20
|
-
|
|
21
|
-
expect(factory.decorate(nil)).to be_nil
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
it "calls a worker" do
|
|
26
|
-
factory = Factory.new
|
|
27
|
-
worker = ->(*){ :decorated }
|
|
28
|
-
|
|
29
|
-
expect(Factory::Worker).to receive(:new).and_return(worker)
|
|
30
|
-
expect(factory.decorate(double)).to be :decorated
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
it "passes the object to the worker" do
|
|
34
|
-
factory = Factory.new
|
|
35
|
-
object = double
|
|
36
|
-
|
|
37
|
-
expect(Factory::Worker).to receive(:new).with(anything(), object).and_return(->(*){})
|
|
38
|
-
factory.decorate(object)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
context "when the :with option was given" do
|
|
42
|
-
it "passes the decorator class to the worker" do
|
|
43
|
-
decorator_class = double
|
|
44
|
-
factory = Factory.new(with: decorator_class)
|
|
45
|
-
|
|
46
|
-
expect(Factory::Worker).to receive(:new).with(decorator_class, anything()).and_return(->(*){})
|
|
47
|
-
factory.decorate(double)
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
context "when the :with option was omitted" do
|
|
52
|
-
it "passes nil to the worker" do
|
|
53
|
-
factory = Factory.new
|
|
54
|
-
|
|
55
|
-
expect(Factory::Worker).to receive(:new).with(nil, anything()).and_return(->(*){})
|
|
56
|
-
factory.decorate(double)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
it "passes options to the call" do
|
|
61
|
-
factory = Factory.new
|
|
62
|
-
worker = ->(*){}
|
|
63
|
-
allow(Factory::Worker).to receive(:new).and_return(worker)
|
|
64
|
-
options = {foo: "bar"}
|
|
65
|
-
|
|
66
|
-
allow(worker).to receive(:call).with(options)
|
|
67
|
-
factory.decorate(double, options)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
context "when the :context option was given" do
|
|
71
|
-
it "sets the passed context" do
|
|
72
|
-
factory = Factory.new(context: {foo: "bar"})
|
|
73
|
-
worker = ->(*){}
|
|
74
|
-
allow(Factory::Worker).to receive_messages new: worker
|
|
75
|
-
|
|
76
|
-
expect(worker).to receive(:call).with({baz: "qux", context: {foo: "bar"}})
|
|
77
|
-
factory.decorate(double, {baz: "qux"})
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it "is overridden by explicitly-specified context" do
|
|
81
|
-
factory = Factory.new({context: {foo: "bar"}})
|
|
82
|
-
worker = ->(*){}
|
|
83
|
-
allow(Factory::Worker).to receive_messages new: worker
|
|
84
|
-
|
|
85
|
-
expect(worker).to receive(:call).with({context: {baz: "qux"}})
|
|
86
|
-
factory.decorate(double, {context: {baz: "qux"}})
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
describe Factory::Worker do
|
|
93
|
-
describe "#call" do
|
|
94
|
-
it "calls the decorator method" do
|
|
95
|
-
object = double
|
|
96
|
-
options = {foo: "bar"}
|
|
97
|
-
worker = Factory::Worker.new(double, object)
|
|
98
|
-
decorator = ->(*){}
|
|
99
|
-
allow(worker).to receive(:decorator){ decorator }
|
|
100
|
-
|
|
101
|
-
allow(decorator).to receive(:call).with(object, options).and_return(:decorated)
|
|
102
|
-
expect(worker.call(options)).to be :decorated
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
context "when the :context option is callable" do
|
|
106
|
-
it "calls it" do
|
|
107
|
-
worker = Factory::Worker.new(double, double)
|
|
108
|
-
decorator = ->(*){}
|
|
109
|
-
allow(worker).to receive_messages decorator: decorator
|
|
110
|
-
context = {foo: "bar"}
|
|
111
|
-
|
|
112
|
-
expect(decorator).to receive(:call).with(anything(), {context: context})
|
|
113
|
-
worker.call(context: ->{ context })
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
it "receives arguments from the :context_args option" do
|
|
117
|
-
worker = Factory::Worker.new(double, double)
|
|
118
|
-
allow(worker).to receive_messages decorator: ->(*){}
|
|
119
|
-
context = ->{}
|
|
120
|
-
|
|
121
|
-
expect(context).to receive(:call).with(:foo, :bar)
|
|
122
|
-
worker.call(context: context, context_args: [:foo, :bar])
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
it "wraps non-arrays passed to :context_args" do
|
|
126
|
-
worker = Factory::Worker.new(double, double)
|
|
127
|
-
allow(worker).to receive_messages decorator: ->(*){}
|
|
128
|
-
context = ->{}
|
|
129
|
-
hash = {foo: "bar"}
|
|
130
|
-
|
|
131
|
-
expect(context).to receive(:call).with(hash)
|
|
132
|
-
worker.call(context: context, context_args: hash)
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
context "when the :context option is not callable" do
|
|
137
|
-
it "doesn't call it" do
|
|
138
|
-
worker = Factory::Worker.new(double, double)
|
|
139
|
-
decorator = ->(*){}
|
|
140
|
-
allow(worker).to receive_messages decorator: decorator
|
|
141
|
-
context = {foo: "bar"}
|
|
142
|
-
|
|
143
|
-
expect(decorator).to receive(:call).with(anything(), {context: context})
|
|
144
|
-
worker.call(context: context)
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
it "does not pass the :context_args option to the decorator" do
|
|
149
|
-
worker = Factory::Worker.new(double, double)
|
|
150
|
-
decorator = ->(*){}
|
|
151
|
-
allow(worker).to receive_messages decorator: decorator
|
|
152
|
-
|
|
153
|
-
expect(decorator).to receive(:call).with(anything(), {foo: "bar"})
|
|
154
|
-
worker.call(foo: "bar", context_args: [])
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
describe "#decorator" do
|
|
159
|
-
context "for a singular object" do
|
|
160
|
-
context "when decorator_class is specified" do
|
|
161
|
-
it "returns the .decorate method from the decorator" do
|
|
162
|
-
decorator_class = Class.new(Decorator)
|
|
163
|
-
worker = Factory::Worker.new(decorator_class, double)
|
|
164
|
-
|
|
165
|
-
expect(worker.decorator).to eq decorator_class.method(:decorate)
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
context "when decorator_class is unspecified" do
|
|
170
|
-
context "and the object is decoratable" do
|
|
171
|
-
it "returns the object's #decorate method" do
|
|
172
|
-
object = double
|
|
173
|
-
options = {foo: "bar"}
|
|
174
|
-
worker = Factory::Worker.new(nil, object)
|
|
175
|
-
|
|
176
|
-
expect(object).to receive(:decorate).with(options).and_return(:decorated)
|
|
177
|
-
expect(worker.decorator.call(object, options)).to be :decorated
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
context "and the object is not decoratable" do
|
|
182
|
-
it "raises an error" do
|
|
183
|
-
object = double
|
|
184
|
-
worker = Factory::Worker.new(nil, object)
|
|
185
|
-
|
|
186
|
-
expect{worker.decorator}.to raise_error UninferrableDecoratorError
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
context "when the object is a struct" do
|
|
192
|
-
it "returns a singular decorator" do
|
|
193
|
-
object = Struct.new(:stuff).new("things")
|
|
194
|
-
|
|
195
|
-
decorator_class = Class.new(Decorator)
|
|
196
|
-
worker = Factory::Worker.new(decorator_class, object)
|
|
197
|
-
|
|
198
|
-
expect(worker.decorator).to eq decorator_class.method(:decorate)
|
|
199
|
-
end
|
|
200
|
-
end
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
context "for a collection object" do
|
|
204
|
-
context "when decorator_class is a CollectionDecorator" do
|
|
205
|
-
it "returns the .decorate method from the collection decorator" do
|
|
206
|
-
decorator_class = Class.new(CollectionDecorator)
|
|
207
|
-
worker = Factory::Worker.new(decorator_class, [])
|
|
208
|
-
|
|
209
|
-
expect(worker.decorator).to eq decorator_class.method(:decorate)
|
|
210
|
-
end
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
context "when decorator_class is a Decorator" do
|
|
214
|
-
it "returns the .decorate_collection method from the decorator" do
|
|
215
|
-
decorator_class = Class.new(Decorator)
|
|
216
|
-
worker = Factory::Worker.new(decorator_class, [])
|
|
217
|
-
|
|
218
|
-
expect(worker.decorator).to eq decorator_class.method(:decorate_collection)
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
|
-
|
|
222
|
-
context "when decorator_class is unspecified" do
|
|
223
|
-
context "and the object is decoratable" do
|
|
224
|
-
it "returns the .decorate_collection method from the object's decorator" do
|
|
225
|
-
object = []
|
|
226
|
-
decorator_class = Class.new(Decorator)
|
|
227
|
-
allow(object).to receive(:decorator_class){ decorator_class }
|
|
228
|
-
allow(object).to receive(:decorate){ nil }
|
|
229
|
-
worker = Factory::Worker.new(nil, object)
|
|
230
|
-
|
|
231
|
-
expect(decorator_class).to receive(:decorate_collection).with(object, {foo: "bar", with: nil}).and_return(:decorated)
|
|
232
|
-
expect(worker.decorator.call(object, foo: "bar")).to be :decorated
|
|
233
|
-
end
|
|
234
|
-
end
|
|
235
|
-
|
|
236
|
-
context "and the object is not decoratable" do
|
|
237
|
-
it "returns the .decorate method from CollectionDecorator" do
|
|
238
|
-
worker = Factory::Worker.new(nil, [])
|
|
239
|
-
|
|
240
|
-
expect(worker.decorator).to eq CollectionDecorator.method(:decorate)
|
|
241
|
-
end
|
|
242
|
-
end
|
|
243
|
-
end
|
|
244
|
-
end
|
|
245
|
-
end
|
|
246
|
-
end
|
|
247
|
-
end
|
data/spec/draper/finders_spec.rb
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
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
|
-
expect(Product).to receive(:find).with(1)
|
|
11
|
-
ProductDecorator.find(1)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "decorates the result" do
|
|
15
|
-
found = Product.new
|
|
16
|
-
allow(Product).to receive(: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
|
-
allow(Product).to receive(: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
|
-
expect(Product).to 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
|
-
allow(Product).to receive(: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
|
-
expect(Product).to 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
|
-
expect(Product).to 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
|
-
expect(Product).to 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
|
-
expect(Product).to 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
|
-
allow(Product).to receive(: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
|
-
expect(Product).to 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
|
-
allow(Product).to receive(: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
|
-
allow(Product).to receive(: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
|
-
allow(Product).to receive_messages 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
|
-
allow(Product).to receive(: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
|
-
expect(Product).to receive(:first)
|
|
122
|
-
ProductDecorator.first
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
it "decorates the result" do
|
|
126
|
-
first = Product.new
|
|
127
|
-
allow(Product).to receive(: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
|
-
allow(Product).to receive(: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
|
-
expect(Product).to receive(:last)
|
|
145
|
-
ProductDecorator.last
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
it "decorates the result" do
|
|
149
|
-
last = Product.new
|
|
150
|
-
allow(Product).to receive(: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
|
-
allow(Product).to receive(: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
|
|
@@ -1,61 +0,0 @@
|
|
|
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
|
-
allow(view_context).to receive(: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
|
-
allow(view_context).to receive(: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
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
module Draper
|
|
4
|
-
describe LazyHelpers do
|
|
5
|
-
describe "#method_missing" do
|
|
6
|
-
let(:decorator) do
|
|
7
|
-
Struct.new(:helpers){include Draper::LazyHelpers}.new(double)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "proxies methods to #helpers" do
|
|
11
|
-
allow(decorator.helpers).to receive(:foo) { |arg| arg }
|
|
12
|
-
expect(decorator.foo(:passed)).to be :passed
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it "passes blocks" do
|
|
16
|
-
allow(decorator.helpers).to receive(:foo) { |&block| block.call }
|
|
17
|
-
expect(decorator.foo{:yielded}).to be :yielded
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'active_record'
|
|
3
|
-
|
|
4
|
-
module Draper
|
|
5
|
-
module QueryMethods
|
|
6
|
-
describe LoadStrategy do
|
|
7
|
-
describe '#new' do
|
|
8
|
-
subject { described_class.new(:active_record) }
|
|
9
|
-
|
|
10
|
-
it { is_expected.to be_an_instance_of(LoadStrategy::ActiveRecord) }
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
describe LoadStrategy::ActiveRecord do
|
|
15
|
-
describe '#allowed?' do
|
|
16
|
-
it 'checks whether or not ActiveRecord::Relation::VALUE_METHODS has the given method' do
|
|
17
|
-
allow(::ActiveRecord::Relation::VALUE_METHODS).to receive(:include?)
|
|
18
|
-
|
|
19
|
-
described_class.new.allowed? :foo
|
|
20
|
-
|
|
21
|
-
expect(::ActiveRecord::Relation::VALUE_METHODS).to have_received(:include?).with(:foo)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require_relative '../dummy/app/decorators/post_decorator'
|
|
3
|
-
|
|
4
|
-
Post = Struct.new(:id) { }
|
|
5
|
-
|
|
6
|
-
module Draper
|
|
7
|
-
describe QueryMethods do
|
|
8
|
-
let(:fake_strategy) { instance_double(QueryMethods::LoadStrategy::ActiveRecord) }
|
|
9
|
-
|
|
10
|
-
before { allow(QueryMethods::LoadStrategy).to receive(:new).and_return(fake_strategy) }
|
|
11
|
-
|
|
12
|
-
describe '#method_missing' do
|
|
13
|
-
let(:collection) { [ Post.new, Post.new ] }
|
|
14
|
-
let(:collection_context) { { user: 'foo' } }
|
|
15
|
-
let(:collection_decorator) { PostDecorator.decorate_collection(collection, context: collection_context) }
|
|
16
|
-
|
|
17
|
-
context 'when strategy allows collection to call the method' do
|
|
18
|
-
let(:results) { spy(:results) }
|
|
19
|
-
|
|
20
|
-
before do
|
|
21
|
-
allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(true)
|
|
22
|
-
allow(collection).to receive(:send).with(:some_query_method).and_return(results)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
it 'calls the method on the collection and decorate it results' do
|
|
26
|
-
collection_decorator.some_query_method
|
|
27
|
-
|
|
28
|
-
expect(results).to have_received(:decorate)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'calls the method on the collection and keeps the decoration options' do
|
|
32
|
-
collection_decorator.some_query_method
|
|
33
|
-
|
|
34
|
-
expect(results).to have_received(:decorate).with({ context: collection_context, with: PostDecorator })
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
context 'when strategy does not allow collection to call the method' do
|
|
39
|
-
before { allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(false) }
|
|
40
|
-
|
|
41
|
-
it 'raises NoMethodError' do
|
|
42
|
-
expect { collection_decorator.some_query_method }.to raise_exception(NoMethodError)
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
describe "#respond_to?" do
|
|
48
|
-
let(:collection) { [ Post.new, Post.new ] }
|
|
49
|
-
let(:collection_decorator) { PostDecorator.decorate_collection(collection) }
|
|
50
|
-
|
|
51
|
-
subject { collection_decorator.respond_to?(:some_query_method) }
|
|
52
|
-
|
|
53
|
-
context 'when strategy allows collection to call the method' do
|
|
54
|
-
before do
|
|
55
|
-
allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(true)
|
|
56
|
-
allow(collection).to receive(:respond_to?).with(:some_query_method).and_return(true)
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
it { is_expected.to eq(true) }
|
|
60
|
-
|
|
61
|
-
context 'and collection does not implement the method' do
|
|
62
|
-
before do
|
|
63
|
-
allow(collection).to receive(:respond_to?).with(:some_query_method).and_return(false)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
it { is_expected.to eq(false) }
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
context 'when strategy does not allow collection to call the method' do
|
|
71
|
-
before do
|
|
72
|
-
allow(fake_strategy).to receive(:allowed?).with(:some_query_method).and_return(false)
|
|
73
|
-
allow(collection).to receive(:respond_to?).with(:some_query_method).and_return(true)
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it { is_expected.to eq(false) }
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Draper, '.undecorate_chain' do
|
|
4
|
-
let!(:object) { Model.new }
|
|
5
|
-
let!(:decorated_inner) { Class.new(Draper::Decorator).new(object) }
|
|
6
|
-
let!(:decorated_outer) { Class.new(Draper::Decorator).new(decorated_inner) }
|
|
7
|
-
|
|
8
|
-
it 'undecorates full chain of decorated objects' do
|
|
9
|
-
expect(Draper.undecorate_chain(decorated_outer)).to equal object
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'passes a non-decorated object through' do
|
|
13
|
-
expect(Draper.undecorate_chain(object)).to equal object
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'passes a non-decorator object through' do
|
|
17
|
-
object = Object.new
|
|
18
|
-
expect(Draper.undecorate_chain(object)).to equal object
|
|
19
|
-
end
|
|
20
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Draper, '.undecorate' do
|
|
4
|
-
it 'undecorates a decorated object' do
|
|
5
|
-
object = Model.new
|
|
6
|
-
decorator = Draper::Decorator.new(object)
|
|
7
|
-
expect(Draper.undecorate(decorator)).to equal object
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it 'passes a non-decorated object through' do
|
|
11
|
-
object = Model.new
|
|
12
|
-
expect(Draper.undecorate(object)).to equal object
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it 'passes a non-decorator object through' do
|
|
16
|
-
object = Object.new
|
|
17
|
-
expect(Draper.undecorate(object)).to equal object
|
|
18
|
-
end
|
|
19
|
-
end
|