draper 0.9.2 → 0.9.3
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.
- data/Readme.markdown +2 -1
- data/lib/draper.rb +1 -0
- data/lib/draper/base.rb +14 -31
- data/lib/draper/decorated_enumerable_proxy.rb +27 -0
- data/lib/draper/version.rb +1 -1
- data/spec/base_spec.rb +14 -0
- metadata +2 -1
data/Readme.markdown
CHANGED
@@ -22,8 +22,9 @@
|
|
22
22
|
|
23
23
|
## What's New
|
24
24
|
|
25
|
-
### Version 0.9.
|
25
|
+
### Version 0.9.3
|
26
26
|
|
27
|
+
* Helpers are available from the decorator class level, so you could call `ArticleDecorator.new_article_link`
|
27
28
|
* Automatically generate a named accessor for the wrapped object, so now inside of `ArticleDecorator` you can use `article` instead of just `model`
|
28
29
|
* Removed the `lazy_helpers` method to favor using `include Draper::LazyHelpers`
|
29
30
|
* Refactored how methods are selected for delegation to the wrapped model
|
data/lib/draper.rb
CHANGED
data/lib/draper/base.rb
CHANGED
@@ -21,7 +21,6 @@ module Draper
|
|
21
21
|
# @param [Object] context (optional)
|
22
22
|
def initialize(input, context = {})
|
23
23
|
input.inspect # forces evaluation of a lazy query from AR
|
24
|
-
input.inspect
|
25
24
|
self.class.model_class = input.class if model_class.nil?
|
26
25
|
@model = input
|
27
26
|
self.context = context
|
@@ -94,7 +93,7 @@ module Draper
|
|
94
93
|
# @param [Object] instance(s) to wrap
|
95
94
|
# @param [Object] context (optional)
|
96
95
|
def self.decorate(input, context = {})
|
97
|
-
input.respond_to?(:each) ? DecoratedEnumerableProxy.new(input, self, context) : new(input, context)
|
96
|
+
input.respond_to?(:each) ? Draper::DecoratedEnumerableProxy.new(input, self, context) : new(input, context)
|
98
97
|
end
|
99
98
|
|
100
99
|
# Access the helpers proxy to call built-in and user-defined
|
@@ -102,10 +101,21 @@ module Draper
|
|
102
101
|
#
|
103
102
|
# @return [Object] proxy
|
104
103
|
def helpers
|
105
|
-
|
104
|
+
self.class.helpers
|
106
105
|
end
|
107
106
|
alias :h :helpers
|
108
107
|
|
108
|
+
# Access the helpers proxy to call built-in and user-defined
|
109
|
+
# Rails helpers from a class context.
|
110
|
+
#
|
111
|
+
# @return [Object] proxy
|
112
|
+
class << self
|
113
|
+
def helpers
|
114
|
+
Thread.current[:current_view_context]
|
115
|
+
end
|
116
|
+
alias :h :helpers
|
117
|
+
end
|
118
|
+
|
109
119
|
# Fetch the original wrapped model.
|
110
120
|
#
|
111
121
|
# @return [Object] original_model
|
@@ -147,33 +157,6 @@ module Draper
|
|
147
157
|
private
|
148
158
|
def allow?(method)
|
149
159
|
(!allowed? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method)
|
150
|
-
end
|
151
|
-
|
152
|
-
class DecoratedEnumerableProxy
|
153
|
-
include Enumerable
|
154
|
-
|
155
|
-
def initialize(collection, klass, context)
|
156
|
-
@wrapped_collection, @klass, @context = collection, klass, context
|
157
|
-
end
|
158
|
-
|
159
|
-
# Implementation of Enumerable#each that proxyes to the wrapped collection
|
160
|
-
def each(&block)
|
161
|
-
@wrapped_collection.each { |member| block.call(@klass.new(member, @context)) }
|
162
|
-
end
|
163
|
-
|
164
|
-
# Implement to_arry so that render @decorated_collection is happy
|
165
|
-
def to_ary
|
166
|
-
@wrapped_collection.to_ary
|
167
|
-
end
|
168
|
-
|
169
|
-
def method_missing (meth, *args, &block)
|
170
|
-
@wrapped_collection.send(meth, *args, &block)
|
171
|
-
end
|
172
|
-
|
173
|
-
def to_s
|
174
|
-
"#<DecoratedEnumerableProxy of #{@klass} for #{@wrapped_collection.inspect}>"
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
160
|
+
end
|
178
161
|
end
|
179
162
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Draper
|
2
|
+
class DecoratedEnumerableProxy
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(collection, klass, context)
|
6
|
+
@wrapped_collection, @klass, @context = collection, klass, context
|
7
|
+
end
|
8
|
+
|
9
|
+
# Implementation of Enumerable#each that proxyes to the wrapped collection
|
10
|
+
def each(&block)
|
11
|
+
@wrapped_collection.each { |member| block.call(@klass.new(member, @context)) }
|
12
|
+
end
|
13
|
+
|
14
|
+
# Implement to_arry so that render @decorated_collection is happy
|
15
|
+
def to_ary
|
16
|
+
@wrapped_collection.to_ary
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing (meth, *args, &block)
|
20
|
+
@wrapped_collection.send(meth, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
"#<DecoratedEnumerableProxy of #{@klass} for #{@wrapped_collection.inspect}>"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/draper/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -23,8 +23,22 @@ describe Draper::Base do
|
|
23
23
|
context(".helpers") do
|
24
24
|
it "should have a valid view_context" do
|
25
25
|
subject.helpers.should be
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be aliased to .h" do
|
29
|
+
subject.h.should == subject.helpers
|
26
30
|
end
|
27
31
|
end
|
32
|
+
|
33
|
+
context("#helpers") do
|
34
|
+
it "should have a valid view_context" do
|
35
|
+
Decorator.helpers.should be
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should be aliased to #h" do
|
39
|
+
Decorator.h.should == Decorator.helpers
|
40
|
+
end
|
41
|
+
end
|
28
42
|
|
29
43
|
context(".decorates") do
|
30
44
|
it "sets the model class for the decorator" do
|
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: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- draper.gemspec
|
51
51
|
- lib/draper.rb
|
52
52
|
- lib/draper/base.rb
|
53
|
+
- lib/draper/decorated_enumerable_proxy.rb
|
53
54
|
- lib/draper/lazy_helpers.rb
|
54
55
|
- lib/draper/model_support.rb
|
55
56
|
- lib/draper/system.rb
|