pres 0.1.0 → 0.2.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 +23 -11
- data/lib/pres/presenter.rb +9 -1
- data/lib/pres/presents.rb +1 -1
- data/lib/pres/version.rb +1 -1
- metadata +5 -14
- data/Gemfile +0 -11
- data/Rakefile +0 -10
- data/test/presenter_test.rb +0 -22
- data/test/presents_test.rb +0 -59
- data/test/test_helper.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 245241e4780fa67fb2d4bfa415969da3469475f6
|
4
|
+
data.tar.gz: 17c852515fbd44b8d5c0fbce156cdab402b2edf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: deda6b5cd350392c43eccf280bbb814494752622e027e630cb4c9189e986acebf9c840abe0620b4c41bc58a32e0986963728fb02ad41aaa8db8b7ce5519338f0
|
7
|
+
data.tar.gz: 1a1da379bfeea98e5d7912a116c46f96c995e67e0e918b12bf65f7e5f22e883e41960ab08a492178f571cbe24790b5e01233a9d83ee923a2501eb31b8c8064e9
|
data/README.md
CHANGED
@@ -12,13 +12,21 @@ The `pres` gem is a lightweight presenter solution.
|
|
12
12
|
|
13
13
|
## How and Why?
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
Rails' `ViewContext` contains convenience methods for views, such as `link_to`,
|
16
|
+
`url_for`, `truncate`, `number_to_currency`, etc. It's the thing that makes
|
17
|
+
Rails views nice to work with.
|
17
18
|
|
18
|
-
Other presenter libraries mix in all the methods from the Rails ViewContext to
|
19
|
+
Other presenter libraries mix in all the methods from the Rails `ViewContext` to
|
19
20
|
make it easy to call those methods in the Presenter class. This causes method
|
20
|
-
bloat. `pres` instead injects the ViewContext as a dependency into the
|
21
|
-
Presenter class, and uses `method_missing` to delegate to
|
21
|
+
bloat. `pres` instead injects the `ViewContext` as a dependency into the
|
22
|
+
Presenter class, and uses `method_missing` to delegate to `ViewContext` methods.
|
23
|
+
So `pres` produces small classes that contain and delegate to an existing object
|
24
|
+
that handles server-side rendering.
|
25
|
+
|
26
|
+
## Presenter vs. Decorator
|
27
|
+
|
28
|
+
Decorators add methods to a model. `pres` encourages you to whitelist model methods
|
29
|
+
via delegation.
|
22
30
|
|
23
31
|
## Install
|
24
32
|
|
@@ -128,11 +136,15 @@ end
|
|
128
136
|
|
129
137
|
Use the presenter objects in `doges/index.haml.html`
|
130
138
|
|
131
|
-
```haml
|
132
139
|
This renders "doges/_doge.html.haml" for each item, as usual:
|
140
|
+
|
141
|
+
```haml
|
133
142
|
= render @doges
|
143
|
+
```
|
134
144
|
|
135
145
|
Or use each:
|
146
|
+
|
147
|
+
```haml
|
136
148
|
- doges.each do |doge|
|
137
149
|
= doge.name_header
|
138
150
|
```
|
@@ -190,13 +202,12 @@ end
|
|
190
202
|
|
191
203
|
#### Presenters are objects
|
192
204
|
|
193
|
-
You can mix in common methods.
|
194
|
-
`app/presenters/shared`:
|
205
|
+
You can mix in common methods.
|
195
206
|
|
196
207
|
```ruby
|
197
208
|
module Shared
|
198
|
-
def
|
199
|
-
|
209
|
+
def truncated_name(length: 40)
|
210
|
+
truncate object.name, length: length
|
200
211
|
end
|
201
212
|
end
|
202
213
|
|
@@ -211,8 +222,9 @@ You can override methods without resorting to convoluted method names:
|
|
211
222
|
class DogePresenter < Presenter
|
212
223
|
include Shared
|
213
224
|
|
214
|
-
def
|
225
|
+
def truncated_name(length: 60)
|
215
226
|
# whoa this one is different!
|
227
|
+
super(length: length)
|
216
228
|
end
|
217
229
|
end
|
218
230
|
```
|
data/lib/pres/presenter.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
# Base Presenter class
|
2
2
|
# Construct with object, view_context, and optional options
|
3
|
-
class Presenter
|
3
|
+
class Presenter
|
4
|
+
attr_accessor :object, :options, :view_context
|
5
|
+
|
4
6
|
delegate :id, :to_partial_path, to: :object
|
5
7
|
|
8
|
+
def initialize(object, view_context = nil, options = {})
|
9
|
+
self.object = object
|
10
|
+
self.view_context = view_context
|
11
|
+
self.options = options
|
12
|
+
end
|
13
|
+
|
6
14
|
# Send missing symbols to view_context
|
7
15
|
def method_missing(symbol, *args, &block)
|
8
16
|
view_context.send(symbol, *args, &block)
|
data/lib/pres/presents.rb
CHANGED
@@ -28,7 +28,7 @@ module Presents
|
|
28
28
|
object.map { |item| present(item, presenter: presenter, **args) }
|
29
29
|
else
|
30
30
|
presenter ||= Presenter if object.nil?
|
31
|
-
presenter ||= Object.const_get("#{
|
31
|
+
presenter ||= Object.const_get("#{object.class.name}Presenter")
|
32
32
|
presenter.new(object, view_context, **args)
|
33
33
|
end
|
34
34
|
end
|
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: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -89,17 +89,12 @@ executables: []
|
|
89
89
|
extensions: []
|
90
90
|
extra_rdoc_files: []
|
91
91
|
files:
|
92
|
-
- Gemfile
|
93
92
|
- LICENSE.txt
|
94
93
|
- README.md
|
95
|
-
- Rakefile
|
96
94
|
- lib/pres.rb
|
97
95
|
- lib/pres/presenter.rb
|
98
96
|
- lib/pres/presents.rb
|
99
97
|
- lib/pres/version.rb
|
100
|
-
- test/presenter_test.rb
|
101
|
-
- test/presents_test.rb
|
102
|
-
- test/test_helper.rb
|
103
98
|
homepage: https://github.com/neighborland/pres
|
104
99
|
licenses:
|
105
100
|
- MIT
|
@@ -120,13 +115,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
115
|
version: '0'
|
121
116
|
requirements: []
|
122
117
|
rubyforge_project:
|
123
|
-
rubygems_version: 2.
|
118
|
+
rubygems_version: 2.5.1
|
124
119
|
signing_key:
|
125
120
|
specification_version: 4
|
126
121
|
summary: A Simple Rails Presenter
|
127
|
-
test_files:
|
128
|
-
|
129
|
-
- Rakefile
|
130
|
-
- test/presenter_test.rb
|
131
|
-
- test/presents_test.rb
|
132
|
-
- test/test_helper.rb
|
122
|
+
test_files: []
|
123
|
+
has_rdoc:
|
data/Gemfile
DELETED
data/Rakefile
DELETED
data/test/presenter_test.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
describe Presenter do
|
4
|
-
it "delegates to view_context" do
|
5
|
-
view_context = FakeViewContext.new
|
6
|
-
presenter = Presenter.new(nil, view_context)
|
7
|
-
assert presenter.respond_to?(:current_user)
|
8
|
-
assert presenter.respond_to?(:link_to)
|
9
|
-
view_context.expects(:link_to)
|
10
|
-
presenter.link_to "something"
|
11
|
-
end
|
12
|
-
|
13
|
-
it "is constructed without options" do
|
14
|
-
assert Presenter.new(nil, nil)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "is constructed with options" do
|
18
|
-
presenter = Presenter.new(nil, nil, something: 42, secrets: "none")
|
19
|
-
assert_equal 42, presenter.options[:something]
|
20
|
-
assert_equal "none", presenter.options[:secrets]
|
21
|
-
end
|
22
|
-
end
|
data/test/presents_test.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
describe Presents do
|
4
|
-
class Doge
|
5
|
-
end
|
6
|
-
|
7
|
-
class DogePresenter < Presenter
|
8
|
-
end
|
9
|
-
|
10
|
-
class VeryDogePresenter < Presenter
|
11
|
-
end
|
12
|
-
|
13
|
-
class FakeController
|
14
|
-
include Presents
|
15
|
-
|
16
|
-
def wrap(object)
|
17
|
-
present(object)
|
18
|
-
end
|
19
|
-
|
20
|
-
def very_wrap(object)
|
21
|
-
present(object, presenter: VeryDogePresenter)
|
22
|
-
end
|
23
|
-
|
24
|
-
def view_context
|
25
|
-
FakeViewContext.new
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
let(:controller) { FakeController.new }
|
30
|
-
|
31
|
-
it "creates the default presenter" do
|
32
|
-
presenter = controller.wrap(Doge.new)
|
33
|
-
assert presenter.is_a?(DogePresenter)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "creates the specified presenter" do
|
37
|
-
presenter = controller.very_wrap(Doge.new)
|
38
|
-
assert presenter.is_a?(VeryDogePresenter)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "creates an array of default presenters" do
|
42
|
-
presenters = controller.wrap([Doge.new, Doge.new])
|
43
|
-
assert presenters.is_a?(Array)
|
44
|
-
assert_equal 2, presenters.size
|
45
|
-
assert presenters[0].is_a?(DogePresenter)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "creates an array of specified presenters" do
|
49
|
-
presenters = controller.very_wrap([Doge.new, Doge.new])
|
50
|
-
assert presenters.is_a?(Array)
|
51
|
-
assert_equal 2, presenters.size
|
52
|
-
assert presenters[0].is_a?(VeryDogePresenter)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "creates the default presenter for nil object" do
|
56
|
-
presenter = controller.wrap(nil)
|
57
|
-
assert presenter.is_a?(Presenter)
|
58
|
-
end
|
59
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
if ENV["TRAVIS"]
|
2
|
-
require "coveralls"
|
3
|
-
Coveralls.wear!
|
4
|
-
end
|
5
|
-
|
6
|
-
require "minitest/autorun"
|
7
|
-
require "mocha/mini_test"
|
8
|
-
require "byebug" if ENV["BYEBUG"]
|
9
|
-
require "pres"
|
10
|
-
|
11
|
-
class FakeViewContext
|
12
|
-
# An example of a method mixed into the view_context
|
13
|
-
def current_user
|
14
|
-
nil
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
# An example of a private method provided by the view_context
|
20
|
-
def link_to(*_)
|
21
|
-
nil
|
22
|
-
end
|
23
|
-
end
|