pres 1.0.0 → 1.1.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.
- checksums.yaml +4 -4
- data/README.md +27 -32
- data/lib/pres/version.rb +1 -1
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c26aeb59d5a25f1249d32e9b31c52dd40c55262
|
4
|
+
data.tar.gz: fe9fb9d51ac9010b57d95cff1c7f00e55d04752d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e315003283e6fed583e5f8b7399a3640a99b9177a9da67c0eb9adf1e94cdee6d06821979cecb824fef5fc26288177eea5dfd4aa71e8885636ec92afb9769e6b0
|
7
|
+
data.tar.gz: d47003fd1650d6800cf0b1380b5cd48aeb48279a52f1cfe28bf7bf0e264db2b6b396a1268751b1b9ee58ae2d87053eb3e9cdcd30f92af11b13d780be209f685e
|
data/README.md
CHANGED
@@ -5,10 +5,8 @@
|
|
5
5
|
|
6
6
|
## What?
|
7
7
|
|
8
|
-
A Presenter is a rendering class
|
9
|
-
|
10
|
-
|
11
|
-
The `pres` gem is a lightweight presenter solution.
|
8
|
+
A Presenter is a rendering class. Presenters are an alternative to an unorganized mass of helper
|
9
|
+
methods in your Rails application. The `pres` gem is a lightweight presenter solution.
|
12
10
|
|
13
11
|
## How and Why?
|
14
12
|
|
@@ -17,11 +15,10 @@ Rails' `ViewContext` contains convenience methods for views, such as `link_to`,
|
|
17
15
|
Rails views nice to work with.
|
18
16
|
|
19
17
|
Other presenter libraries mix in all the methods from the Rails `ViewContext` to
|
20
|
-
make it easy to call those methods in the Presenter class.
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
that handles server-side rendering.
|
18
|
+
make it easy to call those methods in the Presenter class. `pres` instead injects
|
19
|
+
the `ViewContext` as a dependency into the Presenter class, and uses `method_missing`
|
20
|
+
to delegate to `ViewContext` methods. `pres` produces small classes that contain and
|
21
|
+
delegate to an existing object that handles server-side rendering.
|
25
22
|
|
26
23
|
## Install
|
27
24
|
|
@@ -104,7 +101,7 @@ Use the presenter object in `doges/show.haml.html`
|
|
104
101
|
.meme-link= doge.know_your_meme_link
|
105
102
|
```
|
106
103
|
|
107
|
-
####
|
104
|
+
#### Collections
|
108
105
|
|
109
106
|
Create a presenter class in `app/presenters`:
|
110
107
|
|
@@ -114,7 +111,7 @@ class DogePresenter < Pres::Presenter
|
|
114
111
|
end
|
115
112
|
```
|
116
113
|
|
117
|
-
Wrap your model
|
114
|
+
Wrap your model objects in your controller with `present`:
|
118
115
|
|
119
116
|
```ruby
|
120
117
|
class DogesController
|
@@ -148,11 +145,10 @@ Or use each:
|
|
148
145
|
|
149
146
|
#### Present with options
|
150
147
|
|
151
|
-
|
152
|
-
Presenter:
|
148
|
+
Pass additional options to a Presenter as a hash:
|
153
149
|
|
154
150
|
```ruby
|
155
|
-
class UserPresenter
|
151
|
+
class UserPresenter < Pres::Presenter
|
156
152
|
def initialize(object, view_context, cool: false)
|
157
153
|
super
|
158
154
|
@cool = cool
|
@@ -167,25 +163,19 @@ present(user, cool: true)
|
|
167
163
|
#### Render a custom Presenter
|
168
164
|
|
169
165
|
By default, a presenter class corresponding to the model class name is
|
170
|
-
constructed
|
171
|
-
|
166
|
+
constructed in `present`. For example, if you present a `User`, a `UserPresenter`
|
167
|
+
class is constructed. An error is raised if the presenter class does not exist.
|
168
|
+
To specify a different class, use the `presenter:` key.
|
172
169
|
|
173
170
|
```ruby
|
174
171
|
user = User.new
|
175
|
-
present(user, presenter:
|
176
|
-
=> #<
|
177
|
-
```
|
178
|
-
|
179
|
-
#### Render a collection
|
180
|
-
|
181
|
-
```ruby
|
182
|
-
present(User.first(5))
|
183
|
-
=> [#<UserPresenter object: #<User> ...>]
|
172
|
+
present(user, presenter: UserEditPresenter, cool: true)
|
173
|
+
=> #<UserEditPresenter object: #<User> ...>
|
184
174
|
```
|
185
175
|
|
186
176
|
#### Creating presenters in views
|
187
177
|
|
188
|
-
You should create presenters in your controllers. If you would like to create
|
178
|
+
You should create presenters in your controllers or within other presenters. If you would like to create
|
189
179
|
a presenter in your view code, make the `present` method visible to your views:
|
190
180
|
|
191
181
|
```ruby
|
@@ -195,9 +185,7 @@ class ApplicationController
|
|
195
185
|
end
|
196
186
|
```
|
197
187
|
|
198
|
-
###
|
199
|
-
|
200
|
-
#### Presenters are objects
|
188
|
+
### Presenters are objects
|
201
189
|
|
202
190
|
You can mix in common methods.
|
203
191
|
|
@@ -208,12 +196,12 @@ module Shared
|
|
208
196
|
end
|
209
197
|
end
|
210
198
|
|
211
|
-
class DogePresenter < Presenter
|
199
|
+
class DogePresenter < Pres::Presenter
|
212
200
|
include Shared
|
213
201
|
end
|
214
202
|
```
|
215
203
|
|
216
|
-
You can override methods
|
204
|
+
You can override methods as usual:
|
217
205
|
|
218
206
|
```ruby
|
219
207
|
class DogePresenter < Pres::Presenter
|
@@ -228,6 +216,9 @@ end
|
|
228
216
|
|
229
217
|
#### Presenters can create other presenters
|
230
218
|
|
219
|
+
If you are awesome, you could have one top-level presenter exposed per controller,
|
220
|
+
which can then wrap child objects in presenters of their own.
|
221
|
+
|
231
222
|
```ruby
|
232
223
|
class DogePresenter < Pres::Presenter
|
233
224
|
def cats
|
@@ -244,7 +235,11 @@ end
|
|
244
235
|
|
245
236
|
If you don't want to inherit from `Pres::Presenter`, you can include
|
246
237
|
`Pres::ViewDelegation` and implement your own initializer (so the `present` helper
|
247
|
-
will work).
|
238
|
+
will work).
|
239
|
+
|
240
|
+
This technique is useful if you would like to delegate all methods in a model
|
241
|
+
by default, instead of whitelisting methods on the wrapped model explicitly.
|
242
|
+
Delegating everything to the model by default is how the `draper` gem works, for example.
|
248
243
|
|
249
244
|
```ruby
|
250
245
|
class DogePresenter < SimpleDelegator
|
data/lib/pres/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pres
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
@@ -9,20 +9,20 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-01
|
12
|
+
date: 2016-09-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '4.0'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '4.0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
@@ -73,14 +73,14 @@ dependencies:
|
|
73
73
|
requirements:
|
74
74
|
- - "~>"
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version: '
|
76
|
+
version: '11.0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
81
|
- - "~>"
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
version: '
|
83
|
+
version: '11.0'
|
84
84
|
description: A Simple Rails Presenter base class and controller helper
|
85
85
|
email:
|
86
86
|
- tee@neighborland.com
|
@@ -116,9 +116,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
version: '0'
|
117
117
|
requirements: []
|
118
118
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
119
|
+
rubygems_version: 2.6.6
|
120
120
|
signing_key:
|
121
121
|
specification_version: 4
|
122
122
|
summary: A Simple Rails Presenter
|
123
123
|
test_files: []
|
124
|
-
has_rdoc:
|