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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52e2c8712efee843e682f2e3182e2f64023915c3
4
- data.tar.gz: c49375a53ac4065124d60d21abf975d7399f577b
3
+ metadata.gz: 245241e4780fa67fb2d4bfa415969da3469475f6
4
+ data.tar.gz: 17c852515fbd44b8d5c0fbce156cdab402b2edf5
5
5
  SHA512:
6
- metadata.gz: fac9d779069f6e4cc5b17f476fd9755f79d9d942451c0f48d8cadc50bbc83207fcaa79b90b7085bd40f05b50e42d281b78fc5e068ed38fa650f23bb415aa2a6e
7
- data.tar.gz: a04a2234a5025980b4063c40ca7a1c9817c7b6ece10ea44f0f50548a6ab3d56e109419157c070f69ab55f9a671e17b077dbed4aef205d4c79e9cca7a03ece43f
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
- Decorators add methods to a model. `pres` does not do that. `pres` encourages you
16
- to whitelist model methods (via delegation).
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 those methods.
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. Move everything in `application_helper.rb` to
194
- `app/presenters/shared`:
205
+ You can mix in common methods.
195
206
 
196
207
  ```ruby
197
208
  module Shared
198
- def logout_link
199
- # whatever
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 logout_link
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
  ```
@@ -1,8 +1,16 @@
1
1
  # Base Presenter class
2
2
  # Construct with object, view_context, and optional options
3
- class Presenter < Struct.new(:object, :view_context, :options)
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)
@@ -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("#{ object.class.name }Presenter")
31
+ presenter ||= Object.const_get("#{object.class.name}Presenter")
32
32
  presenter.new(object, view_context, **args)
33
33
  end
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module Pres
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-03-25 00:00:00.000000000 Z
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.4.6
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
- - Gemfile
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
@@ -1,11 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- if ENV["TRAVIS"]
4
- gem "coveralls", require: false
5
- end
6
-
7
- if ENV["BYEBUG"]
8
- gem "byebug"
9
- end
10
-
11
- gemspec
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- task default: [:test]
5
-
6
- Rake::TestTask.new(:test) do |t|
7
- t.libs << "test"
8
- t.test_files = %w(test/**/*.rb)
9
- t.verbose = false
10
- end
@@ -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
@@ -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
@@ -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