pres 1.6.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +91 -60
- data/lib/pres/presents.rb +3 -3
- data/lib/pres/version.rb +1 -1
- data/lib/pres/view_delegation.rb +2 -2
- metadata +10 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fcb5ff4379eb2e6e8a3f93f5774253c5e9700dbfb4dc38ee406633dc8a97cf1
|
4
|
+
data.tar.gz: 183813355351056d0ed31642b13dcb37d23fe2058febc7cf40a49fe89692d122
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfcf47dd97b4b68f1c4236829773c9864d580307371e27e00e10d9964f90850c2804aff9d6a6746dd8d49d4586aa1f7bd0237c8123b04fabd4a923ad9cc41e36
|
7
|
+
data.tar.gz: ce1bc25ff680fa951b43155769d116579f2aae2c83b6cfedf0fe0660a4ed21ab096606493cd280dcee4dbbb634db96bcfcbcb9c0f55d7a147657386bb777a094
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# `pres`
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/pres.svg)](http://badge.fury.io/rb/pres)
|
4
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/neighborland/pres/actions/workflows/ruby.yml/badge.svg)](https://github.com/neighborland/pres/actions/workflows/ruby.yml)
|
5
5
|
|
6
6
|
## What?
|
7
7
|
|
@@ -16,8 +16,7 @@ solution with no runtime gem dependencies.
|
|
16
16
|
|
17
17
|
## How and Why?
|
18
18
|
|
19
|
-
Presenters are an alternative to an unorganized
|
20
|
-
methods in your ruby application.
|
19
|
+
Presenters are an alternative to an unorganized pile of helper methods in your rails application.
|
21
20
|
|
22
21
|
Rails' `ViewContext` contains convenience methods for views, such as `link_to`,
|
23
22
|
`url_for`, `truncate`, `number_to_currency`, etc. It's the thing that makes
|
@@ -42,12 +41,25 @@ gem "pres"
|
|
42
41
|
Include the `Pres::Presents` module:
|
43
42
|
|
44
43
|
```ruby
|
45
|
-
class
|
44
|
+
class ApplicationHelper
|
46
45
|
include Pres::Presents
|
47
46
|
end
|
48
47
|
```
|
49
48
|
|
50
|
-
|
49
|
+
This will make the `present` method available in your views.
|
50
|
+
|
51
|
+
## Use
|
52
|
+
|
53
|
+
There are two main approaches:
|
54
|
+
|
55
|
+
(1) Follow the traditional rails way with view templates, but move your helper methods into a presenter class.
|
56
|
+
You'll probably want to start here if you have an existing rails app.
|
57
|
+
|
58
|
+
(2) Create self-contained rendering components (see "Components" below).
|
59
|
+
|
60
|
+
You can use both techniques.
|
61
|
+
|
62
|
+
### (1) With View Templates
|
51
63
|
|
52
64
|
The quickest way to get started is to use the `Pres::Presenter` base class.
|
53
65
|
|
@@ -65,7 +77,7 @@ class DogePresenter < Pres::Presenter
|
|
65
77
|
|
66
78
|
def name_header
|
67
79
|
# object is the Doge used to initialize the presenter
|
68
|
-
content_tag(:h1, object.name)
|
80
|
+
content_tag(:h1, object.name)
|
69
81
|
end
|
70
82
|
|
71
83
|
def signed_in_status
|
@@ -75,41 +87,36 @@ class DogePresenter < Pres::Presenter
|
|
75
87
|
else
|
76
88
|
"Signed out"
|
77
89
|
end
|
78
|
-
end
|
90
|
+
end
|
79
91
|
end
|
80
92
|
```
|
81
93
|
|
82
|
-
|
94
|
+
Standard rails controller method:
|
83
95
|
|
84
96
|
```ruby
|
85
97
|
class DogesController
|
86
98
|
def show
|
87
|
-
@doge =
|
99
|
+
@doge = Doge.find(params[:id])
|
88
100
|
end
|
89
101
|
end
|
90
102
|
```
|
91
103
|
|
92
|
-
|
104
|
+
Wrap your model object in a presenter in your view with `present`:
|
105
|
+
|
106
|
+
`doges/show.haml.html`
|
93
107
|
|
94
108
|
```haml
|
95
|
-
|
96
|
-
.
|
97
|
-
|
98
|
-
.
|
99
|
-
.
|
109
|
+
- present(@doge) do |doge|
|
110
|
+
= doge.name_header
|
111
|
+
.status
|
112
|
+
You are #{doge.signed_in_status}
|
113
|
+
.links
|
114
|
+
.meme-link= doge.know_your_meme_link
|
100
115
|
```
|
101
116
|
|
102
117
|
#### Collections
|
103
118
|
|
104
|
-
|
105
|
-
|
106
|
-
```ruby
|
107
|
-
class DogePresenter < Pres::Presenter
|
108
|
-
# same as above
|
109
|
-
end
|
110
|
-
```
|
111
|
-
|
112
|
-
Build an array of presenters in your controller with `present`:
|
119
|
+
Standard rails controller method:
|
113
120
|
|
114
121
|
```ruby
|
115
122
|
class DogesController
|
@@ -119,21 +126,76 @@ class DogesController
|
|
119
126
|
end
|
120
127
|
```
|
121
128
|
|
122
|
-
|
129
|
+
Build an array of presenters in your view with `present`:
|
130
|
+
|
131
|
+
`doges/index.haml.html`
|
123
132
|
|
124
133
|
This renders "doges/_doge.html.haml" for each item, following rails' usual conventions:
|
125
134
|
|
126
135
|
```haml
|
127
|
-
= render @doges
|
136
|
+
= render present(@doges)
|
128
137
|
```
|
129
138
|
|
130
139
|
Or use each:
|
131
140
|
|
132
141
|
```haml
|
133
|
-
- @doges.each do |doge|
|
142
|
+
- present(@doges).each do |doge|
|
134
143
|
= doge.name_header
|
135
144
|
```
|
136
145
|
|
146
|
+
## (2) Components
|
147
|
+
|
148
|
+
You can use `pres` to build components that directly render HTML:
|
149
|
+
|
150
|
+
_Note: `#sanitize` is a method on the `view_context`._
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
class NameHeader < Pres::Presenter
|
154
|
+
def render
|
155
|
+
return unless object&.name
|
156
|
+
<<~HTML.html_safe
|
157
|
+
<h1>#{sanitize(object.name.titleize)}</h1>
|
158
|
+
HTML
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
user = User.new(name: "joe cool <")
|
163
|
+
|
164
|
+
NameHeader.new(user, view_context).render
|
165
|
+
=> "<h1>Joe Cool <</h1>"
|
166
|
+
|
167
|
+
present(user, presenter: NameHeader).render
|
168
|
+
=> "<h1>Joe Cool <</h1>"
|
169
|
+
```
|
170
|
+
|
171
|
+
You may notice that you could do without `pres` altogether when you don't need
|
172
|
+
the `view_context` helper methods:
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
class PlusTwo
|
176
|
+
def initialize(object)
|
177
|
+
@object = object
|
178
|
+
end
|
179
|
+
|
180
|
+
def render
|
181
|
+
return unless @object
|
182
|
+
<<~HTML.html_safe
|
183
|
+
<p>#{@object + 2}</p>
|
184
|
+
HTML
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
PlusTwo.new(2).render
|
189
|
+
=> "<p>4</p>"
|
190
|
+
|
191
|
+
present(2, presenter: PlusTwo).render
|
192
|
+
=> "<p>4</p>"
|
193
|
+
```
|
194
|
+
|
195
|
+
If `render` is confusing, name that method `#to_html` or something else.
|
196
|
+
|
197
|
+
## Options
|
198
|
+
|
137
199
|
#### Present with options
|
138
200
|
|
139
201
|
Pass additional options to a Presenter as a hash. The presenter class exposes the
|
@@ -145,6 +207,7 @@ user = User.new
|
|
145
207
|
# These two lines are the same:
|
146
208
|
# 1. explicit
|
147
209
|
presenter = UserPresenter.new(user, view_context, something: 123)
|
210
|
+
|
148
211
|
# 2. using #present
|
149
212
|
presenter = present(user, something: 123)
|
150
213
|
=> #<UserPresenter object: #<User> ...>
|
@@ -179,38 +242,6 @@ present(User.new)
|
|
179
242
|
# => #<MyPresenter object: #<User> ...>
|
180
243
|
```
|
181
244
|
|
182
|
-
#### Create presenters in views
|
183
|
-
|
184
|
-
You can create a presenter in your view code. First make the `present` method
|
185
|
-
visible to your views by declaring it a `helper_method`:
|
186
|
-
|
187
|
-
```ruby
|
188
|
-
class ApplicationController
|
189
|
-
include Pres::Presents
|
190
|
-
helper_method :present
|
191
|
-
end
|
192
|
-
```
|
193
|
-
|
194
|
-
Then you can create presenters inline in a view:
|
195
|
-
|
196
|
-
```haml
|
197
|
-
- present(@doge) do |doge|
|
198
|
-
= doge.name_header
|
199
|
-
```
|
200
|
-
|
201
|
-
You can override methods as usual:
|
202
|
-
|
203
|
-
```ruby
|
204
|
-
class DogePresenter < Pres::Presenter
|
205
|
-
include Shared
|
206
|
-
|
207
|
-
def truncated_name(length: 60)
|
208
|
-
# override
|
209
|
-
super(length: length)
|
210
|
-
end
|
211
|
-
end
|
212
|
-
```
|
213
|
-
|
214
245
|
#### Presenters can create other presenters
|
215
246
|
|
216
247
|
Presenters can wrap child objects in presenters of their own.
|
@@ -219,7 +250,7 @@ Presenters can wrap child objects in presenters of their own.
|
|
219
250
|
class DogePresenter < Pres::Presenter
|
220
251
|
def cats
|
221
252
|
present(object.cats)
|
222
|
-
end
|
253
|
+
end
|
223
254
|
end
|
224
255
|
```
|
225
256
|
|
data/lib/pres/presents.rb
CHANGED
@@ -51,12 +51,12 @@ module Pres
|
|
51
51
|
#
|
52
52
|
# Returns a new Presenter object or array of new Presenter objects
|
53
53
|
# Yields a new Presenter object if a block is given
|
54
|
-
def present(object, presenter: nil, **
|
54
|
+
def present(object, presenter: nil, **kwargs)
|
55
55
|
if object.respond_to?(:to_ary)
|
56
|
-
object.map { |item| present(item, presenter
|
56
|
+
object.map { |item| present(item, presenter:, **kwargs) }
|
57
57
|
else
|
58
58
|
presenter ||= presenter_klass(object)
|
59
|
-
wrapper = presenter.new(object, view_context, **
|
59
|
+
wrapper = presenter.new(object, view_context, **kwargs)
|
60
60
|
block_given? ? yield(wrapper) : wrapper
|
61
61
|
end
|
62
62
|
end
|
data/lib/pres/version.rb
CHANGED
data/lib/pres/view_delegation.rb
CHANGED
@@ -7,9 +7,9 @@ module Pres
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# Send missing methods to view_context first
|
10
|
-
def method_missing(method,
|
10
|
+
def method_missing(method, ...)
|
11
11
|
if view_context.respond_to?(method, true)
|
12
|
-
view_context.send(method,
|
12
|
+
view_context.send(method, ...)
|
13
13
|
else
|
14
14
|
super
|
15
15
|
end
|
metadata
CHANGED
@@ -1,44 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pres
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
8
|
- Scott Jacobsen
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: minitest
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - "~>"
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '5.5'
|
21
|
-
type: :development
|
22
|
-
prerelease: false
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
25
|
-
- - "~>"
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
version: '5.5'
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: rake
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
31
|
-
requirements:
|
32
|
-
- - "~>"
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: '12.0'
|
35
|
-
type: :development
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - "~>"
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: '12.0'
|
12
|
+
date: 2024-12-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
42
14
|
description: A Simple Rails Presenter base class and controller helper
|
43
15
|
email:
|
44
16
|
- tee@neighborland.com
|
@@ -57,8 +29,9 @@ files:
|
|
57
29
|
homepage: https://github.com/neighborland/pres
|
58
30
|
licenses:
|
59
31
|
- MIT
|
60
|
-
metadata:
|
61
|
-
|
32
|
+
metadata:
|
33
|
+
rubygems_mfa_required: 'true'
|
34
|
+
post_install_message:
|
62
35
|
rdoc_options: []
|
63
36
|
require_paths:
|
64
37
|
- lib
|
@@ -66,15 +39,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
39
|
requirements:
|
67
40
|
- - ">="
|
68
41
|
- !ruby/object:Gem::Version
|
69
|
-
version: 2.
|
42
|
+
version: 3.2.0
|
70
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
44
|
requirements:
|
72
45
|
- - ">="
|
73
46
|
- !ruby/object:Gem::Version
|
74
47
|
version: '0'
|
75
48
|
requirements: []
|
76
|
-
rubygems_version: 3.
|
77
|
-
signing_key:
|
49
|
+
rubygems_version: 3.5.23
|
50
|
+
signing_key:
|
78
51
|
specification_version: 4
|
79
52
|
summary: A Simple Rails Presenter
|
80
53
|
test_files: []
|