draper 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- 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/spec/draper/factory_spec.rb
CHANGED
@@ -15,7 +15,7 @@ module Draper
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe "#decorate" do
|
18
|
-
context "when
|
18
|
+
context "when object is nil" do
|
19
19
|
it "returns nil" do
|
20
20
|
factory = Factory.new
|
21
21
|
|
@@ -31,12 +31,12 @@ module Draper
|
|
31
31
|
expect(factory.decorate(double)).to be :decorated
|
32
32
|
end
|
33
33
|
|
34
|
-
it "passes the
|
34
|
+
it "passes the object to the worker" do
|
35
35
|
factory = Factory.new
|
36
|
-
|
36
|
+
object = double
|
37
37
|
|
38
|
-
Factory::Worker.should_receive(:new).with(anything(),
|
39
|
-
factory.decorate(
|
38
|
+
Factory::Worker.should_receive(:new).with(anything(), object).and_return(->(*){})
|
39
|
+
factory.decorate(object)
|
40
40
|
end
|
41
41
|
|
42
42
|
context "when the :with option was given" do
|
@@ -95,13 +95,13 @@ module Draper
|
|
95
95
|
|
96
96
|
describe "#call" do
|
97
97
|
it "calls the decorator method" do
|
98
|
-
|
98
|
+
object = double
|
99
99
|
options = {foo: "bar"}
|
100
|
-
worker = Factory::Worker.new(double,
|
100
|
+
worker = Factory::Worker.new(double, object)
|
101
101
|
decorator = ->(*){}
|
102
102
|
worker.stub decorator: decorator
|
103
103
|
|
104
|
-
decorator.should_receive(:call).with(
|
104
|
+
decorator.should_receive(:call).with(object, options).and_return(:decorated)
|
105
105
|
expect(worker.call(options)).to be :decorated
|
106
106
|
end
|
107
107
|
|
@@ -159,7 +159,7 @@ module Draper
|
|
159
159
|
end
|
160
160
|
|
161
161
|
describe "#decorator" do
|
162
|
-
context "for a singular
|
162
|
+
context "for a singular object" do
|
163
163
|
context "when decorator_class is specified" do
|
164
164
|
it "returns the .decorate method from the decorator" do
|
165
165
|
decorator_class = Class.new(Decorator)
|
@@ -170,21 +170,21 @@ module Draper
|
|
170
170
|
end
|
171
171
|
|
172
172
|
context "when decorator_class is unspecified" do
|
173
|
-
context "and the
|
174
|
-
it "returns the
|
175
|
-
|
173
|
+
context "and the object is decoratable" do
|
174
|
+
it "returns the object's #decorate method" do
|
175
|
+
object = double
|
176
176
|
options = {foo: "bar"}
|
177
|
-
worker = Factory::Worker.new(nil,
|
177
|
+
worker = Factory::Worker.new(nil, object)
|
178
178
|
|
179
|
-
|
180
|
-
expect(worker.decorator.call(
|
179
|
+
object.should_receive(:decorate).with(options).and_return(:decorated)
|
180
|
+
expect(worker.decorator.call(object, options)).to be :decorated
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
|
-
context "and the
|
184
|
+
context "and the object is not decoratable" do
|
185
185
|
it "raises an error" do
|
186
|
-
|
187
|
-
worker = Factory::Worker.new(nil,
|
186
|
+
object = double
|
187
|
+
worker = Factory::Worker.new(nil, object)
|
188
188
|
|
189
189
|
expect{worker.decorator}.to raise_error UninferrableDecoratorError
|
190
190
|
end
|
@@ -192,7 +192,7 @@ module Draper
|
|
192
192
|
end
|
193
193
|
end
|
194
194
|
|
195
|
-
context "for a collection
|
195
|
+
context "for a collection object" do
|
196
196
|
context "when decorator_class is a CollectionDecorator" do
|
197
197
|
it "returns the .decorate method from the collection decorator" do
|
198
198
|
decorator_class = Class.new(CollectionDecorator)
|
@@ -212,18 +212,20 @@ module Draper
|
|
212
212
|
end
|
213
213
|
|
214
214
|
context "when decorator_class is unspecified" do
|
215
|
-
context "and the
|
216
|
-
it "returns the
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
215
|
+
context "and the object is decoratable" do
|
216
|
+
it "returns the .decorate_collection method from the object's decorator" do
|
217
|
+
object = []
|
218
|
+
decorator_class = Class.new(Decorator)
|
219
|
+
object.stub decorator_class: decorator_class
|
220
|
+
object.stub decorate: nil
|
221
|
+
worker = Factory::Worker.new(nil, object)
|
222
|
+
|
223
|
+
decorator_class.should_receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated)
|
224
|
+
expect(worker.decorator.call(object, foo: "bar")).to be :decorated
|
223
225
|
end
|
224
226
|
end
|
225
227
|
|
226
|
-
context "and the
|
228
|
+
context "and the object is not decoratable" do
|
227
229
|
it "returns the .decorate method from CollectionDecorator" do
|
228
230
|
worker = Factory::Worker.new(nil, [])
|
229
231
|
|
data/spec/draper/finders_spec.rb
CHANGED
@@ -16,7 +16,7 @@ module Draper
|
|
16
16
|
Product.stub(:find).and_return(found)
|
17
17
|
decorator = ProductDecorator.find(1)
|
18
18
|
expect(decorator).to be_a ProductDecorator
|
19
|
-
expect(decorator.
|
19
|
+
expect(decorator.object).to be found
|
20
20
|
end
|
21
21
|
|
22
22
|
it "passes context to the decorator" do
|
@@ -39,7 +39,7 @@ module Draper
|
|
39
39
|
Product.stub(:find_by_name).and_return(found)
|
40
40
|
decorator = ProductDecorator.find_by_name("apples")
|
41
41
|
expect(decorator).to be_a ProductDecorator
|
42
|
-
expect(decorator.
|
42
|
+
expect(decorator.object).to be found
|
43
43
|
end
|
44
44
|
|
45
45
|
it "proxies complex ProductDecorators" do
|
@@ -127,7 +127,7 @@ module Draper
|
|
127
127
|
Product.stub(:first).and_return(first)
|
128
128
|
decorator = ProductDecorator.first
|
129
129
|
expect(decorator).to be_a ProductDecorator
|
130
|
-
expect(decorator.
|
130
|
+
expect(decorator.object).to be first
|
131
131
|
end
|
132
132
|
|
133
133
|
it "passes context to the decorator" do
|
@@ -150,7 +150,7 @@ module Draper
|
|
150
150
|
Product.stub(:last).and_return(last)
|
151
151
|
decorator = ProductDecorator.last
|
152
152
|
expect(decorator).to be_a ProductDecorator
|
153
|
-
expect(decorator.
|
153
|
+
expect(decorator.object).to be last
|
154
154
|
end
|
155
155
|
|
156
156
|
it "passes context to the decorator" do
|
@@ -15,7 +15,7 @@ class PostDecorator < Draper::Decorator
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def path_with_model
|
18
|
-
h.post_path(
|
18
|
+
h.post_path(object)
|
19
19
|
end
|
20
20
|
|
21
21
|
def path_with_id
|
@@ -23,7 +23,7 @@ class PostDecorator < Draper::Decorator
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def url_with_model
|
26
|
-
h.post_url(
|
26
|
+
h.post_url(object)
|
27
27
|
end
|
28
28
|
|
29
29
|
def url_with_id
|
@@ -9,8 +9,8 @@ Draper::ViewContext.test_strategy :fast
|
|
9
9
|
Post = Struct.new(:id) { extend ActiveModel::Naming }
|
10
10
|
|
11
11
|
describe PostDecorator do
|
12
|
-
let(:decorator) { PostDecorator.new(
|
13
|
-
let(:
|
12
|
+
let(:decorator) { PostDecorator.new(object) }
|
13
|
+
let(:object) { Post.new(42) }
|
14
14
|
|
15
15
|
it "can use built-in helpers" do
|
16
16
|
expect(decorator.truncated).to eq "Once upon a..."
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PostDecorator do
|
4
|
-
let(:decorator) { PostDecorator.new(
|
5
|
-
let(:
|
4
|
+
let(:decorator) { PostDecorator.new(object) }
|
5
|
+
let(:object) { Post.create }
|
6
6
|
|
7
7
|
it "can use built-in helpers" do
|
8
8
|
expect(decorator.truncated).to eq "Once upon a..."
|
@@ -17,23 +17,23 @@ describe PostDecorator do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "can use path helpers with its model" do
|
20
|
-
expect(decorator.path_with_model).to eq "/en/posts/#{
|
20
|
+
expect(decorator.path_with_model).to eq "/en/posts/#{object.id}"
|
21
21
|
end
|
22
22
|
|
23
23
|
it "can use path helpers with its id" do
|
24
|
-
expect(decorator.path_with_id).to eq "/en/posts/#{
|
24
|
+
expect(decorator.path_with_id).to eq "/en/posts/#{object.id}"
|
25
25
|
end
|
26
26
|
|
27
27
|
it "can use url helpers with its model" do
|
28
|
-
expect(decorator.url_with_model).to eq "http://www.example.com:12345/en/posts/#{
|
28
|
+
expect(decorator.url_with_model).to eq "http://www.example.com:12345/en/posts/#{object.id}"
|
29
29
|
end
|
30
30
|
|
31
31
|
it "can use url helpers with its id" do
|
32
|
-
expect(decorator.url_with_id).to eq "http://www.example.com:12345/en/posts/#{
|
32
|
+
expect(decorator.url_with_id).to eq "http://www.example.com:12345/en/posts/#{object.id}"
|
33
33
|
end
|
34
34
|
|
35
35
|
it "can be passed implicitly to url_for" do
|
36
|
-
expect(decorator.link).to eq "<a href=\"/en/posts/#{
|
36
|
+
expect(decorator.link).to eq "<a href=\"/en/posts/#{object.id}\">#{object.id}</a>"
|
37
37
|
end
|
38
38
|
|
39
39
|
it "serializes overriden attributes" do
|
@@ -8,32 +8,32 @@ shared_examples_for "decoration-aware #==" do |subject|
|
|
8
8
|
end
|
9
9
|
|
10
10
|
it "is true for a decorated version of itself" do
|
11
|
-
decorated = double(
|
11
|
+
decorated = double(object: subject, decorated?: true)
|
12
12
|
|
13
13
|
expect(subject == decorated).to be_true
|
14
14
|
end
|
15
15
|
|
16
16
|
it "is false for a decorated other object" do
|
17
|
-
decorated = double(
|
17
|
+
decorated = double(object: Object.new, decorated?: true)
|
18
18
|
|
19
19
|
expect(subject == decorated).to be_false
|
20
20
|
end
|
21
21
|
|
22
|
-
it "is false for a decoratable object with a `
|
23
|
-
decoratable = double(
|
22
|
+
it "is false for a decoratable object with a `object` association" do
|
23
|
+
decoratable = double(object: subject, decorated?: false)
|
24
24
|
|
25
25
|
expect(subject == decoratable).to be_false
|
26
26
|
end
|
27
27
|
|
28
|
-
it "is false for an undecoratable object with a `
|
29
|
-
undecoratable = double(
|
28
|
+
it "is false for an undecoratable object with a `object` association" do
|
29
|
+
undecoratable = double(object: subject)
|
30
30
|
|
31
31
|
expect(subject == undecoratable).to be_false
|
32
32
|
end
|
33
33
|
|
34
34
|
it "is true for a multiply-decorated version of itself" do
|
35
|
-
decorated = double(
|
36
|
-
redecorated = double(
|
35
|
+
decorated = double(object: subject, decorated?: true)
|
36
|
+
redecorated = double(object: decorated, decorated?: true)
|
37
37
|
|
38
38
|
expect(subject == redecorated).to be_true
|
39
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: draper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Casimir
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|