draper 0.7.2 → 0.8.0
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/.gitignore +2 -0
- data/.travis.yml +7 -0
- data/.yardopts +1 -0
- data/Gemfile +12 -2
- data/Readme.markdown +39 -24
- data/doc/ApplicationDecorator.html +147 -0
- data/doc/Draper/AllHelpers.html +256 -0
- data/doc/Draper/Base.html +1222 -0
- data/doc/Draper/DecoratorGenerator.html +216 -0
- data/doc/Draper/LazyHelpers.html +174 -0
- data/doc/Draper/ModelSupport.html +164 -0
- data/doc/Draper/System.html +179 -0
- data/doc/Draper.html +123 -0
- data/doc/_index.html +172 -0
- data/doc/class_list.html +47 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +53 -0
- data/doc/css/style.css +320 -0
- data/doc/file_list.html +46 -0
- data/doc/frames.html +13 -0
- data/doc/index.html +172 -0
- data/doc/js/app.js +205 -0
- data/doc/js/full_list.js +150 -0
- data/doc/js/jquery.js +16 -0
- data/doc/method_list.html +174 -0
- data/doc/top-level-namespace.html +103 -0
- data/draper.gemspec +0 -16
- data/lib/draper/base.rb +101 -16
- data/lib/draper/model_support.rb +1 -0
- data/lib/draper/system.rb +1 -1
- data/lib/draper/version.rb +1 -1
- data/lib/draper/view_context.rb +11 -0
- data/lib/draper.rb +1 -1
- data/lib/generators/rails/decorator_generator.rb +15 -0
- data/spec/base_spec.rb +23 -4
- data/spec/draper/model_support_spec.rb +5 -0
- data/spec/generators/draper/decorator/decorator_generator_spec.rb +28 -0
- data/spec/samples/active_record.rb +1 -4
- data/spec/samples/application_controller.rb +28 -6
- data/spec/samples/decorator_with_allows.rb +1 -1
- data/spec/samples/product_decorator.rb +4 -0
- data/spec/spec_helper.rb +4 -22
- data/spec/view_context_spec.rb +25 -0
- metadata +32 -103
- data/lib/draper/all_helpers.rb +0 -50
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/.yardopts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
yardoc 'lib/draper/**/*.rb' -m markdown
|
data/Gemfile
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
1
|
source :rubygems
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
gem 'rake'
|
|
4
|
+
gem 'rspec', '~> 2.0'
|
|
5
|
+
gem 'activesupport', '~> 3.0.10'
|
|
6
|
+
gem 'actionpack', "~> 3.0.10", :require => 'action_view'
|
|
7
|
+
gem 'ammeter', '~> 0.1.3', :require => 'ammeter/init'
|
|
8
|
+
gem 'guard'
|
|
9
|
+
gem 'guard-rspec'
|
|
10
|
+
gem 'launchy'
|
|
11
|
+
gem 'yard'
|
|
12
|
+
|
|
13
|
+
gemspec
|
data/Readme.markdown
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Draper: View Models for Rails
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+
|
|
3
5
|
## Quick Start
|
|
4
6
|
|
|
5
7
|
1. Add `gem 'draper'` to your `Gemfile` and `bundle`
|
|
@@ -28,7 +30,7 @@ This gem makes it easy to apply the decorator pattern to domain models in a Rail
|
|
|
28
30
|
|
|
29
31
|
### 1. Object Oriented Helpers
|
|
30
32
|
|
|
31
|
-
Why hate helpers? In Ruby/Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.The job of a helper is to take in data and output a presentation-ready string. We can do that with a decorator.
|
|
33
|
+
Why hate normal helpers? In Ruby/Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.The job of a helper is to take in data and output a presentation-ready string. We can do that with a decorator.
|
|
32
34
|
|
|
33
35
|
A decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then the decorator could override `.published_at` to use formatted output like this:
|
|
34
36
|
|
|
@@ -51,7 +53,7 @@ Or, in the course of formatting this data, did you wish you could access `curren
|
|
|
51
53
|
|
|
52
54
|
How would you handle this in the model layer? You'd probably pass the `current_user` or some role/flag down to `to_json`. That should still feel slimy.
|
|
53
55
|
|
|
54
|
-
When you use a decorator you have the power of a Ruby object but it's a part of the view layer. This is where your `
|
|
56
|
+
When you use a decorator you have the power of a Ruby object but it's a part of the view layer. This is where your `to_json` belongs. You can access your `current_user` helper method using the `h` proxy available in the decorator:
|
|
55
57
|
|
|
56
58
|
```ruby
|
|
57
59
|
class ArticleDecorator < ApplicationDecorator
|
|
@@ -59,9 +61,9 @@ class ArticleDecorator < ApplicationDecorator
|
|
|
59
61
|
ADMIN_VISIBLE_ATTRIBUTES = [:title, :body, :author, :status]
|
|
60
62
|
PUBLIC_VISIBLE_ATTRIBUTES = [:title, :body]
|
|
61
63
|
|
|
62
|
-
def
|
|
64
|
+
def to_json
|
|
63
65
|
attr_set = h.current_user.admin? ? ADMIN_VISIBLE_ATTRIBUTES : PUBLIC_VISIBLE_ATTRIBUTES
|
|
64
|
-
model.
|
|
66
|
+
model.to_json(:only => attr_set)
|
|
65
67
|
end
|
|
66
68
|
end
|
|
67
69
|
```
|
|
@@ -84,9 +86,9 @@ end
|
|
|
84
86
|
Then, to test it:
|
|
85
87
|
|
|
86
88
|
```irb
|
|
87
|
-
|
|
89
|
+
> ad = ArticleDecorator.find(1)
|
|
88
90
|
=> #<ArticleDecorator:0x000001020d7728 @model=#<Article id: 1, title: "Hello, World">>
|
|
89
|
-
|
|
91
|
+
> ad.title
|
|
90
92
|
NoMethodError: undefined method `title' for #<ArticleDecorator:0x000001020d7728>
|
|
91
93
|
```
|
|
92
94
|
|
|
@@ -102,11 +104,11 @@ end
|
|
|
102
104
|
```
|
|
103
105
|
|
|
104
106
|
```irb
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
> ad = ArticleDecorator.find(1)
|
|
108
|
+
=> #<ArticleDecorator:0x000001020d7728 @model=#<Article id: 1, title: "Hello, World">>
|
|
109
|
+
> ad.title
|
|
110
|
+
=> "Hello, World"
|
|
111
|
+
> ad.created_at
|
|
110
112
|
NoMethodError: undefined method `created_at' for #<ArticleDecorator:0x000001020d7728>
|
|
111
113
|
```
|
|
112
114
|
|
|
@@ -135,9 +137,22 @@ config.generators do |g|
|
|
|
135
137
|
g.helper false
|
|
136
138
|
end
|
|
137
139
|
```
|
|
138
|
-
|
|
139
140
|
If you want a helper, you can still call `rails generate helper` directly.
|
|
140
141
|
|
|
142
|
+
|
|
143
|
+
#### Add DecoratorGenerator to ActiveRecord Generator (Optional)
|
|
144
|
+
|
|
145
|
+
Add the following to your `config/application.rb`
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
config.generators do |g|
|
|
149
|
+
g.orm :decorator, :invoke_after_finished => "active_record:model"
|
|
150
|
+
end
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
From now on, every model you generate will first invoke the DecoratorGenerator. The Decorator will then invoke the active_record:model Generator.
|
|
154
|
+
|
|
155
|
+
|
|
141
156
|
### Generate the Decorator
|
|
142
157
|
|
|
143
158
|
To decorate a model named `Article`:
|
|
@@ -199,20 +214,22 @@ When writing your controller actions, you have three options:
|
|
|
199
214
|
|
|
200
215
|
* Call `.new` and pass in the object to be wrapped
|
|
201
216
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
217
|
+
```ruby
|
|
218
|
+
ArticleDecorator.new(Article.find(params[:id]))`
|
|
219
|
+
```
|
|
205
220
|
|
|
206
221
|
* Call `.decorate` and pass in an object or collection of objects to be wrapped:
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
222
|
+
|
|
223
|
+
```ruby
|
|
224
|
+
ArticleDecorator.decorate(Article.first) # Returns one instance of ArticleDecorator
|
|
225
|
+
ArticleDecorator.decorate(Article.all) # Returns an array of ArticleDecorator instances
|
|
226
|
+
```
|
|
211
227
|
|
|
212
228
|
* Call `.find` to do automatically do a lookup on the `decorates` class:
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
229
|
+
|
|
230
|
+
```ruby
|
|
231
|
+
ArticleDecorator.find(1)
|
|
232
|
+
```
|
|
216
233
|
|
|
217
234
|
### In Your Views
|
|
218
235
|
|
|
@@ -296,7 +313,6 @@ end
|
|
|
296
313
|
## Issues / Pending
|
|
297
314
|
|
|
298
315
|
* Documentation
|
|
299
|
-
* Keep revising Readme for better organization/clarity
|
|
300
316
|
* Add more information about using "context"
|
|
301
317
|
* Add information about the `.decorator` method
|
|
302
318
|
* Make clear the pattern of overriding accessor methods of the wrapped model
|
|
@@ -305,7 +321,6 @@ end
|
|
|
305
321
|
* Add YARD documentation to source
|
|
306
322
|
* Add a section about contributing
|
|
307
323
|
* Generators
|
|
308
|
-
* Test coverage for generators (help!)
|
|
309
324
|
* Implement hook so generating a controller/scaffold generates a decorator
|
|
310
325
|
* Add generators for...
|
|
311
326
|
* `draper:model`: Model + Decorator
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
4
|
+
<head>
|
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
6
|
+
<title>
|
|
7
|
+
Class: ApplicationDecorator
|
|
8
|
+
|
|
9
|
+
— Documentation by YARD 0.7.2
|
|
10
|
+
|
|
11
|
+
</title>
|
|
12
|
+
|
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
|
|
14
|
+
|
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
|
|
16
|
+
|
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
|
18
|
+
relpath = '';
|
|
19
|
+
if (relpath != '') relpath += '/';
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
|
23
|
+
|
|
24
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
</head>
|
|
28
|
+
<body>
|
|
29
|
+
<script type="text/javascript" charset="utf-8">
|
|
30
|
+
if (window.top.frames.main) document.body.className = 'frames';
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<div id="header">
|
|
34
|
+
<div id="menu">
|
|
35
|
+
|
|
36
|
+
<a href="_index.html">Index (A)</a> »
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
<span class="title">ApplicationDecorator</span>
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
<div id="search">
|
|
46
|
+
|
|
47
|
+
<a id="class_list_link" href="#">Class List</a>
|
|
48
|
+
|
|
49
|
+
<a id="method_list_link" href="#">Method List</a>
|
|
50
|
+
|
|
51
|
+
<a id="file_list_link" href="#">File List</a>
|
|
52
|
+
|
|
53
|
+
</div>
|
|
54
|
+
<div class="clear"></div>
|
|
55
|
+
</div>
|
|
56
|
+
|
|
57
|
+
<iframe id="search_frame"></iframe>
|
|
58
|
+
|
|
59
|
+
<div id="content"><h1>Class: ApplicationDecorator
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
</h1>
|
|
64
|
+
|
|
65
|
+
<dl class="box">
|
|
66
|
+
|
|
67
|
+
<dt class="r1">Inherits:</dt>
|
|
68
|
+
<dd class="r1">
|
|
69
|
+
<span class="inheritName"><span class='object_link'><a href="Draper/Base.html" title="Draper::Base (class)">Draper::Base</a></span></span>
|
|
70
|
+
|
|
71
|
+
<ul class="fullTree">
|
|
72
|
+
<li>Object</li>
|
|
73
|
+
|
|
74
|
+
<li class="next"><span class='object_link'><a href="Draper/Base.html" title="Draper::Base (class)">Draper::Base</a></span></li>
|
|
75
|
+
|
|
76
|
+
<li class="next">ApplicationDecorator</li>
|
|
77
|
+
|
|
78
|
+
</ul>
|
|
79
|
+
<a href="#" class="inheritanceTree">show all</a>
|
|
80
|
+
|
|
81
|
+
</dd>
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
<dt class="r2 last">Defined in:</dt>
|
|
92
|
+
<dd class="r2 last">lib/generators/draper/decorator/templates/application_decorator.rb</dd>
|
|
93
|
+
|
|
94
|
+
</dl>
|
|
95
|
+
<div class="clear"></div>
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
<h2>Constant Summary</h2>
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
<h3 class="inherited">Constants inherited from <span class='object_link'><a href="Draper/Base.html" title="Draper::Base (class)">Draper::Base</a></span></h3>
|
|
107
|
+
<p class="inherited"><span class='object_link'><a href="Draper/Base.html#DEFAULT_DENIED-constant" title="Draper::Base::DEFAULT_DENIED (constant)">DEFAULT_DENIED</a></span>, <span class='object_link'><a href="Draper/Base.html#FORCED_PROXY-constant" title="Draper::Base::FORCED_PROXY (constant)">FORCED_PROXY</a></span></p>
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
<h2>Instance Attribute Summary</h2>
|
|
115
|
+
|
|
116
|
+
<h3 class="inherited">Attributes inherited from <span class='object_link'><a href="Draper/Base.html" title="Draper::Base (class)">Draper::Base</a></span></h3>
|
|
117
|
+
<p class="inherited"><span class='object_link'><a href="Draper/Base.html#context-instance_method" title="Draper::Base#context (method)">context</a></span>, <span class='object_link'><a href="Draper/Base.html#model-instance_method" title="Draper::Base#model (method)">model</a></span></p>
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
<h2>Method Summary</h2>
|
|
127
|
+
|
|
128
|
+
<h3 class="inherited">Methods inherited from <span class='object_link'><a href="Draper/Base.html" title="Draper::Base (class)">Draper::Base</a></span></h3>
|
|
129
|
+
<p class="inherited"><span class='object_link'><a href="Draper/Base.html#allows-class_method" title="Draper::Base.allows (method)">allows</a></span>, <span class='object_link'><a href="Draper/Base.html#decorate-class_method" title="Draper::Base.decorate (method)">decorate</a></span>, <span class='object_link'><a href="Draper/Base.html#decorates-class_method" title="Draper::Base.decorates (method)">decorates</a></span>, <span class='object_link'><a href="Draper/Base.html#denies-class_method" title="Draper::Base.denies (method)">denies</a></span>, <span class='object_link'><a href="Draper/Base.html#find-class_method" title="Draper::Base.find (method)">find</a></span>, <span class='object_link'><a href="Draper/Base.html#helpers-instance_method" title="Draper::Base#helpers (method)">#helpers</a></span>, <span class='object_link'><a href="Draper/Base.html#initialize-instance_method" title="Draper::Base#initialize (method)">#initialize</a></span>, <span class='object_link'><a href="Draper/Base.html#lazy_helpers-class_method" title="Draper::Base.lazy_helpers (method)">lazy_helpers</a></span>, <span class='object_link'><a href="Draper/Base.html#model_name-class_method" title="Draper::Base.model_name (method)">model_name</a></span>, <span class='object_link'><a href="Draper/Base.html#to_model-instance_method" title="Draper::Base#to_model (method)">#to_model</a></span></p>
|
|
130
|
+
<div id="constructor_details" class="method_details_list">
|
|
131
|
+
<h2>Constructor Details</h2>
|
|
132
|
+
|
|
133
|
+
<p class="notice">This class inherits a constructor from <span class='object_link'><a href="Draper/Base.html#initialize-instance_method" title="Draper::Base#initialize (method)">Draper::Base</a></span></p>
|
|
134
|
+
|
|
135
|
+
</div>
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
</div>
|
|
139
|
+
|
|
140
|
+
<div id="footer">
|
|
141
|
+
Generated on Wed Aug 31 23:53:09 2011 by
|
|
142
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
143
|
+
0.7.2 (ruby-1.8.7).
|
|
144
|
+
</div>
|
|
145
|
+
|
|
146
|
+
</body>
|
|
147
|
+
</html>
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
4
|
+
<head>
|
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
6
|
+
<title>
|
|
7
|
+
Module: Draper::AllHelpers
|
|
8
|
+
|
|
9
|
+
— Documentation by YARD 0.7.2
|
|
10
|
+
|
|
11
|
+
</title>
|
|
12
|
+
|
|
13
|
+
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen" charset="utf-8" />
|
|
14
|
+
|
|
15
|
+
<link rel="stylesheet" href="../css/common.css" type="text/css" media="screen" charset="utf-8" />
|
|
16
|
+
|
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
|
18
|
+
relpath = '..';
|
|
19
|
+
if (relpath != '') relpath += '/';
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
|
|
23
|
+
|
|
24
|
+
<script type="text/javascript" charset="utf-8" src="../js/app.js"></script>
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
</head>
|
|
28
|
+
<body>
|
|
29
|
+
<script type="text/javascript" charset="utf-8">
|
|
30
|
+
if (window.top.frames.main) document.body.className = 'frames';
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<div id="header">
|
|
34
|
+
<div id="menu">
|
|
35
|
+
|
|
36
|
+
<a href="../_index.html">Index (A)</a> »
|
|
37
|
+
<span class='title'><span class='object_link'><a href="../Draper.html" title="Draper (module)">Draper</a></span></span>
|
|
38
|
+
»
|
|
39
|
+
<span class="title">AllHelpers</span>
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
<div id="search">
|
|
46
|
+
|
|
47
|
+
<a id="class_list_link" href="#">Class List</a>
|
|
48
|
+
|
|
49
|
+
<a id="method_list_link" href="#">Method List</a>
|
|
50
|
+
|
|
51
|
+
<a id="file_list_link" href="#">File List</a>
|
|
52
|
+
|
|
53
|
+
</div>
|
|
54
|
+
<div class="clear"></div>
|
|
55
|
+
</div>
|
|
56
|
+
|
|
57
|
+
<iframe id="search_frame"></iframe>
|
|
58
|
+
|
|
59
|
+
<div id="content"><h1>Module: Draper::AllHelpers
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
</h1>
|
|
64
|
+
|
|
65
|
+
<dl class="box">
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
<dt class="r1 last">Defined in:</dt>
|
|
75
|
+
<dd class="r1 last">lib/draper/all_helpers.rb</dd>
|
|
76
|
+
|
|
77
|
+
</dl>
|
|
78
|
+
<div class="clear"></div>
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
<h2>
|
|
87
|
+
Instance Method Summary
|
|
88
|
+
<small>(<a href="#" class="summary_toggle">collapse</a>)</small>
|
|
89
|
+
</h2>
|
|
90
|
+
|
|
91
|
+
<ul class="summary">
|
|
92
|
+
|
|
93
|
+
<li class="public ">
|
|
94
|
+
<span class="summary_signature">
|
|
95
|
+
|
|
96
|
+
<a href="#all_helpers-instance_method" title="#all_helpers (instance method)">- (Object) <strong>all_helpers</strong> </a>
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
</span>
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
<span class="summary_desc"><div class='inline'><p>Most of the black magic here thanks to Xavier Shay (@xshay)
|
|
110
|
+
Provide access to helper methods from outside controllers and views,
|
|
111
|
+
such as in Presenter objects.</p>
|
|
112
|
+
</div></span>
|
|
113
|
+
|
|
114
|
+
</li>
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
</ul>
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
<div id="instance_method_details" class="method_details_list">
|
|
123
|
+
<h2>Instance Method Details</h2>
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
<div class="method_details first">
|
|
127
|
+
<p class="signature first" id="all_helpers-instance_method">
|
|
128
|
+
|
|
129
|
+
- (<tt>Object</tt>) <strong>all_helpers</strong>
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
</p><div class="docstring">
|
|
134
|
+
<div class="discussion">
|
|
135
|
+
<p>Most of the black magic here thanks to Xavier Shay (@xshay)
|
|
136
|
+
Provide access to helper methods from outside controllers and views,
|
|
137
|
+
such as in Presenter objects. Rails provides ActionController::Base.helpers,
|
|
138
|
+
but this does not include any of our application helpers.</p>
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
</div>
|
|
142
|
+
</div>
|
|
143
|
+
<div class="tags">
|
|
144
|
+
|
|
145
|
+
</div><table class="source_code">
|
|
146
|
+
<tr>
|
|
147
|
+
<td>
|
|
148
|
+
<pre class="lines">
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
7
|
|
152
|
+
8
|
|
153
|
+
9
|
|
154
|
+
10
|
|
155
|
+
11
|
|
156
|
+
12
|
|
157
|
+
13
|
|
158
|
+
14
|
|
159
|
+
15
|
|
160
|
+
16
|
|
161
|
+
17
|
|
162
|
+
18
|
|
163
|
+
19
|
|
164
|
+
20
|
|
165
|
+
21
|
|
166
|
+
22
|
|
167
|
+
23
|
|
168
|
+
24
|
|
169
|
+
25
|
|
170
|
+
26
|
|
171
|
+
27
|
|
172
|
+
28
|
|
173
|
+
29
|
|
174
|
+
30
|
|
175
|
+
31
|
|
176
|
+
32
|
|
177
|
+
33
|
|
178
|
+
34
|
|
179
|
+
35
|
|
180
|
+
36
|
|
181
|
+
37
|
|
182
|
+
38
|
|
183
|
+
39
|
|
184
|
+
40
|
|
185
|
+
41
|
|
186
|
+
42
|
|
187
|
+
43
|
|
188
|
+
44
|
|
189
|
+
45
|
|
190
|
+
46
|
|
191
|
+
47
|
|
192
|
+
48</pre>
|
|
193
|
+
</td>
|
|
194
|
+
<td>
|
|
195
|
+
<pre class="code"><span class="info file"># File 'lib/draper/all_helpers.rb', line 7</span>
|
|
196
|
+
|
|
197
|
+
<span class='def def kw'>def</span> <span class='all_helpers identifier id'>all_helpers</span>
|
|
198
|
+
<span class='@all_helpers_proxy ivar id'>@all_helpers_proxy</span> <span class='opasgn op'>||=</span> <span class='begin begin kw'>begin</span>
|
|
199
|
+
<span class='comment val'># Start with just the rails helpers. This is the same method used</span>
|
|
200
|
+
<span class='comment val'># by ActionController::Base.helpers</span>
|
|
201
|
+
<span class='comment val'># proxy = ActionView::Base.new.extend(_helpers)</span>
|
|
202
|
+
<span class='proxy identifier id'>proxy</span> <span class='assign token'>=</span> <span class='ActionController constant id'>ActionController</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span><span class='dot token'>.</span><span class='helpers identifier id'>helpers</span>
|
|
203
|
+
|
|
204
|
+
<span class='comment val'># url_for depends on _routes method being defined</span>
|
|
205
|
+
<span class='proxy identifier id'>proxy</span><span class='dot token'>.</span><span class='instance_eval identifier id'>instance_eval</span> <span class='do do kw'>do</span>
|
|
206
|
+
<span class='def def kw'>def</span> <span class='_routes identifier id'>_routes</span>
|
|
207
|
+
<span class='Rails constant id'>Rails</span><span class='dot token'>.</span><span class='application identifier id'>application</span><span class='dot token'>.</span><span class='routes identifier id'>routes</span>
|
|
208
|
+
<span class='end end kw'>end</span>
|
|
209
|
+
<span class='end end kw'>end</span>
|
|
210
|
+
|
|
211
|
+
<span class='comment val'># Import all named path methods</span>
|
|
212
|
+
<span class='proxy identifier id'>proxy</span><span class='dot token'>.</span><span class='extend identifier id'>extend</span><span class='lparen token'>(</span><span class='Rails constant id'>Rails</span><span class='dot token'>.</span><span class='application identifier id'>application</span><span class='dot token'>.</span><span class='routes identifier id'>routes</span><span class='dot token'>.</span><span class='named_routes identifier id'>named_routes</span><span class='dot token'>.</span><span class='module identifier id'>module</span><span class='rparen token'>)</span>
|
|
213
|
+
|
|
214
|
+
<span class='comment val'># Load all our application helpers to extend</span>
|
|
215
|
+
<span class='modules_for_helpers identifier id'>modules_for_helpers</span><span class='lparen token'>(</span><span class='lbrack token'>[</span><span class='symbol val'>:all</span><span class='rbrack token'>]</span><span class='rparen token'>)</span><span class='dot token'>.</span><span class='each identifier id'>each</span> <span class='do do kw'>do</span> <span class='bitor op'>|</span><span class='mod identifier id'>mod</span><span class='bitor op'>|</span>
|
|
216
|
+
<span class='proxy identifier id'>proxy</span><span class='dot token'>.</span><span class='extend identifier id'>extend</span><span class='lparen token'>(</span><span class='mod identifier id'>mod</span><span class='rparen token'>)</span>
|
|
217
|
+
<span class='end end kw'>end</span>
|
|
218
|
+
|
|
219
|
+
<span class='proxy identifier id'>proxy</span><span class='dot token'>.</span><span class='instance_eval identifier id'>instance_eval</span> <span class='do do kw'>do</span>
|
|
220
|
+
<span class='def def kw'>def</span> <span class='controller identifier id'>controller</span>
|
|
221
|
+
<span class='comment val'>#Object.controller</span>
|
|
222
|
+
<span class='end end kw'>end</span>
|
|
223
|
+
<span class='end end kw'>end</span>
|
|
224
|
+
|
|
225
|
+
<span class='proxy identifier id'>proxy</span><span class='dot token'>.</span><span class='instance_eval identifier id'>instance_eval</span> <span class='do do kw'>do</span>
|
|
226
|
+
<span class='comment val'># A hack since this proxy doesn't pick up default_url_options from anywhere</span>
|
|
227
|
+
<span class='def def kw'>def</span> <span class='url_for identifier id'>url_for</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
|
|
228
|
+
<span class='if if kw'>if</span> <span class='args identifier id'>args</span><span class='dot token'>.</span><span class='last identifier id'>last</span><span class='dot token'>.</span><span class='is_a? fid id'>is_a?</span><span class='lparen token'>(</span><span class='Hash constant id'>Hash</span><span class='rparen token'>)</span> <span class='andop op'>&&</span> <span class='notop op'>!</span><span class='args identifier id'>args</span><span class='dot token'>.</span><span class='last identifier id'>last</span><span class='lbrack token'>[</span><span class='symbol val'>:only_path</span><span class='rbrack token'>]</span>
|
|
229
|
+
<span class='args identifier id'>args</span> <span class='assign token'>=</span> <span class='args identifier id'>args</span><span class='dot token'>.</span><span class='dup identifier id'>dup</span>
|
|
230
|
+
<span class='args identifier id'>args</span> <span class='lshft op'><<</span> <span class='args identifier id'>args</span><span class='dot token'>.</span><span class='pop identifier id'>pop</span><span class='dot token'>.</span><span class='merge identifier id'>merge</span><span class='lparen token'>(</span><span class='string val'>'host'</span> <span class='assign token'>=</span><span class='gt op'>></span> <span class='ActionMailer constant id'>ActionMailer</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span><span class='dot token'>.</span><span class='default_url_options identifier id'>default_url_options</span><span class='lbrack token'>[</span><span class='symbol val'>:host</span><span class='rbrack token'>]</span><span class='rparen token'>)</span>
|
|
231
|
+
<span class='end end kw'>end</span>
|
|
232
|
+
<span class='super super kw'>super</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
|
|
233
|
+
<span class='end end kw'>end</span>
|
|
234
|
+
<span class='end end kw'>end</span>
|
|
235
|
+
|
|
236
|
+
<span class='proxy identifier id'>proxy</span>
|
|
237
|
+
<span class='end end kw'>end</span>
|
|
238
|
+
<span class='end end kw'>end</span>
|
|
239
|
+
</pre>
|
|
240
|
+
</td>
|
|
241
|
+
</tr>
|
|
242
|
+
</table>
|
|
243
|
+
</div>
|
|
244
|
+
|
|
245
|
+
</div>
|
|
246
|
+
|
|
247
|
+
</div>
|
|
248
|
+
|
|
249
|
+
<div id="footer">
|
|
250
|
+
Generated on Thu Sep 1 01:13:37 2011 by
|
|
251
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
252
|
+
0.7.2 (ruby-1.8.7).
|
|
253
|
+
</div>
|
|
254
|
+
|
|
255
|
+
</body>
|
|
256
|
+
</html>
|