jamesgolick-draper 1.1.1a
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +22 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +150 -0
- data/CONTRIBUTING.md +15 -0
- data/Gemfile +39 -0
- data/Guardfile +26 -0
- data/LICENSE +7 -0
- data/README.md +417 -0
- data/Rakefile +69 -0
- data/draper.gemspec +32 -0
- data/lib/draper.rb +64 -0
- data/lib/draper/automatic_delegation.rb +56 -0
- data/lib/draper/collection_decorator.rb +96 -0
- data/lib/draper/decoratable.rb +82 -0
- data/lib/draper/decoratable/equality.rb +18 -0
- data/lib/draper/decorated_association.rb +35 -0
- data/lib/draper/decorates_assigned.rb +44 -0
- data/lib/draper/decorator.rb +248 -0
- data/lib/draper/delegation.rb +13 -0
- data/lib/draper/factory.rb +87 -0
- data/lib/draper/finders.rb +37 -0
- data/lib/draper/helper_proxy.rb +38 -0
- data/lib/draper/helper_support.rb +5 -0
- data/lib/draper/lazy_helpers.rb +15 -0
- data/lib/draper/railtie.rb +63 -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 +16 -0
- data/lib/draper/test_case.rb +53 -0
- data/lib/draper/version.rb +3 -0
- data/lib/draper/view_context.rb +99 -0
- data/lib/draper/view_context/build_strategy.rb +48 -0
- data/lib/draper/view_helpers.rb +37 -0
- data/lib/generators/decorator/decorator_generator.rb +36 -0
- data/lib/generators/decorator/templates/decorator.rb +19 -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/resource_override.rb +12 -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 +279 -0
- data/spec/draper/decoratable/equality_spec.rb +10 -0
- data/spec/draper/decoratable_spec.rb +176 -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 +634 -0
- data/spec/draper/factory_spec.rb +238 -0
- data/spec/draper/finders_spec.rb +166 -0
- data/spec/draper/helper_proxy_spec.rb +53 -0
- data/spec/draper/lazy_helpers_spec.rb +21 -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 +2 -0
- data/spec/dummy/app/decorators/post_decorator.rb +56 -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 +34 -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 +80 -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 +38 -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 +11 -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 +58 -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 +9 -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 +4 -0
- data/spec/dummy/test/test_helper.rb +3 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +130 -0
- data/spec/integration/integration_spec.rb +58 -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 +37 -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 +451 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
<%- module_namespacing do -%>
|
2
|
+
<%- if parent_class_name.present? -%>
|
3
|
+
class <%= class_name %>Decorator < <%= parent_class_name %>
|
4
|
+
<%- else -%>
|
5
|
+
class <%= class_name %>
|
6
|
+
<%- end -%>
|
7
|
+
delegate_all
|
8
|
+
|
9
|
+
# Define presentation-specific methods here. Helpers are accessed through
|
10
|
+
# `helpers` (aka `h`). You can override attributes, for example:
|
11
|
+
#
|
12
|
+
# def created_at
|
13
|
+
# helpers.content_tag :span, class: 'time' do
|
14
|
+
# source.created_at.strftime("%a %m/%d/%y")
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
|
18
|
+
end
|
19
|
+
<% end -%>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'generators/mini_test'
|
2
|
+
|
3
|
+
module MiniTest
|
4
|
+
module Generators
|
5
|
+
class DecoratorGenerator < Base
|
6
|
+
def self.source_root
|
7
|
+
File.expand_path('../templates', __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
class_option :spec, :type => :boolean, :default => false, :desc => "Use MiniTest::Spec DSL"
|
11
|
+
|
12
|
+
check_class_collision suffix: "DecoratorTest"
|
13
|
+
|
14
|
+
def create_test_file
|
15
|
+
template_type = options[:spec] ? "spec" : "test"
|
16
|
+
template "decorator_#{template_type}.rb", File.join("test/decorators", class_path, "#{singular_name}_decorator_test.rb")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Rspec
|
2
|
+
class DecoratorGenerator < ::Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def create_spec_file
|
6
|
+
template 'decorator_spec.rb', File.join('spec/decorators', class_path, "#{singular_name}_decorator_spec.rb")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module TestUnit
|
2
|
+
class DecoratorGenerator < ::Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def create_test_file
|
6
|
+
template 'decorator_test.rb', File.join('test/decorators', class_path, "#{singular_name}_decorator_test.rb")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/shared_examples/view_helpers'
|
3
|
+
|
4
|
+
module Draper
|
5
|
+
describe CollectionDecorator do
|
6
|
+
it_behaves_like "view helpers", CollectionDecorator.new([])
|
7
|
+
|
8
|
+
describe "#initialize" do
|
9
|
+
describe "options validation" do
|
10
|
+
|
11
|
+
it "does not raise error on valid options" do
|
12
|
+
valid_options = {with: Decorator, context: {}}
|
13
|
+
expect{CollectionDecorator.new([], valid_options)}.not_to raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises error on invalid options" do
|
17
|
+
expect{CollectionDecorator.new([], foo: "bar")}.to raise_error ArgumentError, /Unknown key/
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "with context" do
|
23
|
+
it "stores the context itself" do
|
24
|
+
context = {some: "context"}
|
25
|
+
decorator = CollectionDecorator.new([], context: context)
|
26
|
+
|
27
|
+
expect(decorator.context).to be context
|
28
|
+
end
|
29
|
+
|
30
|
+
it "passes context to the individual decorators" do
|
31
|
+
context = {some: "context"}
|
32
|
+
decorator = CollectionDecorator.new([Product.new, Product.new], context: context)
|
33
|
+
|
34
|
+
decorator.each do |item|
|
35
|
+
expect(item.context).to be context
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#context=" do
|
41
|
+
it "updates the stored context" do
|
42
|
+
decorator = CollectionDecorator.new([], context: {some: "context"})
|
43
|
+
new_context = {other: "context"}
|
44
|
+
|
45
|
+
decorator.context = new_context
|
46
|
+
expect(decorator.context).to be new_context
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when the collection is already decorated" do
|
50
|
+
it "updates the items' context" do
|
51
|
+
decorator = CollectionDecorator.new([Product.new, Product.new], context: {some: "context"})
|
52
|
+
decorator.decorated_collection # trigger decoration
|
53
|
+
new_context = {other: "context"}
|
54
|
+
|
55
|
+
decorator.context = new_context
|
56
|
+
decorator.each do |item|
|
57
|
+
expect(item.context).to be new_context
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when the collection has not yet been decorated" do
|
63
|
+
it "does not trigger decoration" do
|
64
|
+
decorator = CollectionDecorator.new([])
|
65
|
+
|
66
|
+
decorator.should_not_receive(:decorated_collection)
|
67
|
+
decorator.context = {other: "context"}
|
68
|
+
end
|
69
|
+
|
70
|
+
it "sets context after decoration is triggered" do
|
71
|
+
decorator = CollectionDecorator.new([Product.new, Product.new], context: {some: "context"})
|
72
|
+
new_context = {other: "context"}
|
73
|
+
|
74
|
+
decorator.context = new_context
|
75
|
+
decorator.each do |item|
|
76
|
+
expect(item.context).to be new_context
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "item decoration" do
|
83
|
+
it "sets decorated items' source models" do
|
84
|
+
collection = [Product.new, Product.new]
|
85
|
+
decorator = CollectionDecorator.new(collection)
|
86
|
+
|
87
|
+
decorator.zip collection do |item, source|
|
88
|
+
expect(item.source).to be source
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "when the :with option was given" do
|
93
|
+
it "uses the :with option" do
|
94
|
+
decorator = CollectionDecorator.new([Product.new], with: OtherDecorator).first
|
95
|
+
|
96
|
+
expect(decorator).to be_decorated_with OtherDecorator
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "when the :with option was not given" do
|
101
|
+
it "infers the item decorator from each item" do
|
102
|
+
decorator = CollectionDecorator.new([double(decorate: :inferred_decorator)]).first
|
103
|
+
|
104
|
+
expect(decorator).to be :inferred_decorator
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe ".delegate" do
|
110
|
+
protect_class ProductsDecorator
|
111
|
+
|
112
|
+
it "defaults the :to option to :source" do
|
113
|
+
Object.should_receive(:delegate).with(:foo, :bar, to: :source)
|
114
|
+
ProductsDecorator.delegate :foo, :bar
|
115
|
+
end
|
116
|
+
|
117
|
+
it "does not overwrite the :to option if supplied" do
|
118
|
+
Object.should_receive(:delegate).with(:foo, :bar, to: :baz)
|
119
|
+
ProductsDecorator.delegate :foo, :bar, to: :baz
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "#find" do
|
124
|
+
context "with a block" do
|
125
|
+
it "decorates Enumerable#find" do
|
126
|
+
decorator = CollectionDecorator.new([])
|
127
|
+
|
128
|
+
decorator.decorated_collection.should_receive(:find).and_return(:delegated)
|
129
|
+
expect(decorator.find{|p| p.title == "title"}).to be :delegated
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "without a block" do
|
134
|
+
it "decorates Model.find" do
|
135
|
+
item_decorator = Class.new
|
136
|
+
decorator = CollectionDecorator.new([], with: item_decorator)
|
137
|
+
|
138
|
+
item_decorator.should_receive(:find).with(1).and_return(:delegated)
|
139
|
+
expect(decorator.find(1)).to be :delegated
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "#to_ary" do
|
145
|
+
# required for `render @collection` in Rails
|
146
|
+
it "delegates to the decorated collection" do
|
147
|
+
decorator = CollectionDecorator.new([])
|
148
|
+
|
149
|
+
decorator.decorated_collection.should_receive(:to_ary).and_return(:delegated)
|
150
|
+
expect(decorator.to_ary).to be :delegated
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
it "delegates array methods to the decorated collection" do
|
155
|
+
decorator = CollectionDecorator.new([])
|
156
|
+
|
157
|
+
decorator.decorated_collection.should_receive(:[]).with(42).and_return(:delegated)
|
158
|
+
expect(decorator[42]).to be :delegated
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "#==" do
|
162
|
+
context "when comparing to a collection decorator with the same source" do
|
163
|
+
it "returns true" do
|
164
|
+
source = [Product.new, Product.new]
|
165
|
+
decorator = CollectionDecorator.new(source)
|
166
|
+
other = ProductsDecorator.new(source)
|
167
|
+
|
168
|
+
expect(decorator == other).to be_true
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context "when comparing to a collection decorator with a different source" do
|
173
|
+
it "returns false" do
|
174
|
+
decorator = CollectionDecorator.new([Product.new, Product.new])
|
175
|
+
other = ProductsDecorator.new([Product.new, Product.new])
|
176
|
+
|
177
|
+
expect(decorator == other).to be_false
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context "when comparing to a collection of the same items" do
|
182
|
+
it "returns true" do
|
183
|
+
source = [Product.new, Product.new]
|
184
|
+
decorator = CollectionDecorator.new(source)
|
185
|
+
other = source.dup
|
186
|
+
|
187
|
+
expect(decorator == other).to be_true
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
context "when comparing to a collection of different items" do
|
192
|
+
it "returns false" do
|
193
|
+
decorator = CollectionDecorator.new([Product.new, Product.new])
|
194
|
+
other = [Product.new, Product.new]
|
195
|
+
|
196
|
+
expect(decorator == other).to be_false
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context "when the decorated collection has been modified" do
|
201
|
+
it "is no longer equal to the source" do
|
202
|
+
source = [Product.new, Product.new]
|
203
|
+
decorator = CollectionDecorator.new(source)
|
204
|
+
other = source.dup
|
205
|
+
|
206
|
+
decorator << Product.new.decorate
|
207
|
+
expect(decorator == other).to be_false
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "#to_s" do
|
213
|
+
context "when :with option was given" do
|
214
|
+
it "returns a string representation of the collection decorator" do
|
215
|
+
decorator = CollectionDecorator.new(["a", "b", "c"], with: ProductDecorator)
|
216
|
+
|
217
|
+
expect(decorator.to_s).to eq '#<Draper::CollectionDecorator of ProductDecorator for ["a", "b", "c"]>'
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "when :with option was not given" do
|
222
|
+
it "returns a string representation of the collection decorator" do
|
223
|
+
decorator = CollectionDecorator.new(["a", "b", "c"])
|
224
|
+
|
225
|
+
expect(decorator.to_s).to eq '#<Draper::CollectionDecorator of inferred decorators for ["a", "b", "c"]>'
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
context "for a custom subclass" do
|
230
|
+
it "uses the custom class name" do
|
231
|
+
decorator = ProductsDecorator.new([])
|
232
|
+
|
233
|
+
expect(decorator.to_s).to match /ProductsDecorator/
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
describe '#decorated?' do
|
239
|
+
it 'returns true' do
|
240
|
+
decorator = ProductsDecorator.new([Product.new])
|
241
|
+
|
242
|
+
expect(decorator).to be_decorated
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe '#decorated_with?' do
|
247
|
+
it "checks if a decorator has been applied to a collection" do
|
248
|
+
decorator = ProductsDecorator.new([Product.new])
|
249
|
+
|
250
|
+
expect(decorator).to be_decorated_with ProductsDecorator
|
251
|
+
expect(decorator).not_to be_decorated_with OtherDecorator
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
describe '#kind_of?' do
|
256
|
+
it 'asks the kind of its decorated collection' do
|
257
|
+
decorator = ProductsDecorator.new([])
|
258
|
+
decorator.decorated_collection.should_receive(:kind_of?).with(Array).and_return("true")
|
259
|
+
expect(decorator.kind_of?(Array)).to eq "true"
|
260
|
+
end
|
261
|
+
|
262
|
+
context 'when asking the underlying collection returns false' do
|
263
|
+
it 'asks the CollectionDecorator instance itself' do
|
264
|
+
decorator = ProductsDecorator.new([])
|
265
|
+
decorator.decorated_collection.stub(:kind_of?).with(::Draper::CollectionDecorator).and_return(false)
|
266
|
+
expect(decorator.kind_of?(::Draper::CollectionDecorator)).to be true
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
describe '#is_a?' do
|
272
|
+
it 'aliases to #kind_of?' do
|
273
|
+
decorator = ProductsDecorator.new([])
|
274
|
+
expect(decorator.method(:kind_of?)).to eq decorator.method(:is_a?)
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
end
|
279
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/shared_examples/decoratable_equality'
|
3
|
+
|
4
|
+
module Draper
|
5
|
+
describe Decoratable do
|
6
|
+
|
7
|
+
describe "#decorate" do
|
8
|
+
it "returns a decorator for self" do
|
9
|
+
product = Product.new
|
10
|
+
decorator = product.decorate
|
11
|
+
|
12
|
+
expect(decorator).to be_a ProductDecorator
|
13
|
+
expect(decorator.source).to be product
|
14
|
+
end
|
15
|
+
|
16
|
+
it "accepts context" do
|
17
|
+
context = {some: "context"}
|
18
|
+
decorator = Product.new.decorate(context: context)
|
19
|
+
|
20
|
+
expect(decorator.context).to be context
|
21
|
+
end
|
22
|
+
|
23
|
+
it "uses the #decorator_class" do
|
24
|
+
product = Product.new
|
25
|
+
product.stub decorator_class: OtherDecorator
|
26
|
+
|
27
|
+
expect(product.decorate).to be_an_instance_of OtherDecorator
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#applied_decorators" do
|
32
|
+
it "returns an empty list" do
|
33
|
+
expect(Product.new.applied_decorators).to eq []
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#decorated_with?" do
|
38
|
+
it "returns false" do
|
39
|
+
expect(Product.new).not_to be_decorated_with Decorator
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#decorated?" do
|
44
|
+
it "returns false" do
|
45
|
+
expect(Product.new).not_to be_decorated
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#decorator_class" do
|
50
|
+
it "delegates to .decorator_class" do
|
51
|
+
product = Product.new
|
52
|
+
|
53
|
+
Product.should_receive(:decorator_class).and_return(:some_decorator)
|
54
|
+
expect(product.decorator_class).to be :some_decorator
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#==" do
|
59
|
+
it_behaves_like "decoration-aware #==", Product.new
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#===" do
|
63
|
+
it "is true when #== is true" do
|
64
|
+
product = Product.new
|
65
|
+
|
66
|
+
product.should_receive(:==).and_return(true)
|
67
|
+
expect(product === :anything).to be_true
|
68
|
+
end
|
69
|
+
|
70
|
+
it "is false when #== is false" do
|
71
|
+
product = Product.new
|
72
|
+
|
73
|
+
product.should_receive(:==).and_return(false)
|
74
|
+
expect(product === :anything).to be_false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe ".====" do
|
79
|
+
it "is true for an instance" do
|
80
|
+
expect(Product === Product.new).to be_true
|
81
|
+
end
|
82
|
+
|
83
|
+
it "is true for a derived instance" do
|
84
|
+
expect(Product === Class.new(Product).new).to be_true
|
85
|
+
end
|
86
|
+
|
87
|
+
it "is false for an unrelated instance" do
|
88
|
+
expect(Product === Model.new).to be_false
|
89
|
+
end
|
90
|
+
|
91
|
+
it "is true for a decorated instance" do
|
92
|
+
decorator = double(source: Product.new)
|
93
|
+
|
94
|
+
expect(Product === decorator).to be_true
|
95
|
+
end
|
96
|
+
|
97
|
+
it "is true for a decorated derived instance" do
|
98
|
+
decorator = double(source: Class.new(Product).new)
|
99
|
+
|
100
|
+
expect(Product === decorator).to be_true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "is false for a decorated unrelated instance" do
|
104
|
+
decorator = double(source: Model.new)
|
105
|
+
|
106
|
+
expect(Product === decorator).to be_false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe ".decorate" do
|
111
|
+
let(:scoping_method) { Rails::VERSION::MAJOR >= 4 ? :all : :scoped }
|
112
|
+
|
113
|
+
it "calls #decorate_collection on .decorator_class" do
|
114
|
+
scoped = [Product.new]
|
115
|
+
Product.stub scoping_method => scoped
|
116
|
+
|
117
|
+
Product.decorator_class.should_receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection)
|
118
|
+
expect(Product.decorate).to be :decorated_collection
|
119
|
+
end
|
120
|
+
|
121
|
+
it "accepts options" do
|
122
|
+
options = {with: ProductDecorator, context: {some: "context"}}
|
123
|
+
Product.stub scoping_method => []
|
124
|
+
|
125
|
+
Product.decorator_class.should_receive(:decorate_collection).with([], options)
|
126
|
+
Product.decorate(options)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe ".decorator_class" do
|
131
|
+
context "for classes" do
|
132
|
+
it "infers the decorator from the class" do
|
133
|
+
expect(Product.decorator_class).to be ProductDecorator
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context "for ActiveModel classes" do
|
138
|
+
it "infers the decorator from the model name" do
|
139
|
+
Product.stub(:model_name).and_return("Other")
|
140
|
+
|
141
|
+
expect(Product.decorator_class).to be OtherDecorator
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context "in a namespace" do
|
146
|
+
context "for classes" do
|
147
|
+
it "infers the decorator from the class" do
|
148
|
+
expect(Namespaced::Product.decorator_class).to be Namespaced::ProductDecorator
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context "for ActiveModel classes" do
|
153
|
+
it "infers the decorator from the model name" do
|
154
|
+
Namespaced::Product.stub(:model_name).and_return("Namespaced::Other")
|
155
|
+
|
156
|
+
expect(Namespaced::Product.decorator_class).to be Namespaced::OtherDecorator
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "when the decorator can't be inferred" do
|
162
|
+
it "throws an UninferrableDecoratorError" do
|
163
|
+
expect{Model.decorator_class}.to raise_error UninferrableDecoratorError
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "when an unrelated NameError is thrown" do
|
168
|
+
it "re-raises that error" do
|
169
|
+
String.any_instance.stub(:constantize).and_return{Draper::Base}
|
170
|
+
expect{Product.decorator_class}.to raise_error NameError, /Draper::Base/
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
end
|