pres 0.2.0 → 1.0.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: 245241e4780fa67fb2d4bfa415969da3469475f6
4
- data.tar.gz: 17c852515fbd44b8d5c0fbce156cdab402b2edf5
3
+ metadata.gz: 38f3df21d4a8eec7d64db8e94ea4b032811c5af4
4
+ data.tar.gz: 3bb195b7290f4c41b96ac063e1ed2e49344d61cb
5
5
  SHA512:
6
- metadata.gz: deda6b5cd350392c43eccf280bbb814494752622e027e630cb4c9189e986acebf9c840abe0620b4c41bc58a32e0986963728fb02ad41aaa8db8b7ce5519338f0
7
- data.tar.gz: 1a1da379bfeea98e5d7912a116c46f96c995e67e0e918b12bf65f7e5f22e883e41960ab08a492178f571cbe24790b5e01233a9d83ee923a2501eb31b8c8064e9
6
+ metadata.gz: ed39b6d60be0ac37a431a4b5d0921b6ed7bd11e3ba1d8f95c2e2a157d3d889ac08e60c6d8ca9807f1f1fe2b2e495ebecf20eba07cdfa31fce07bd8210d9dc80c
7
+ data.tar.gz: d007458b744e863ea1ab9db627236032930121ec89cf3d2686516748d7b0c43932ebbff90c3d14b11913db13a9c4799e4f0bc8eb2486527dc15b6d9e408bab83
data/README.md CHANGED
@@ -20,14 +20,9 @@ Other presenter libraries mix in all the methods from the Rails `ViewContext` to
20
20
  make it easy to call those methods in the Presenter class. This causes method
21
21
  bloat. `pres` instead injects the `ViewContext` as a dependency into the
22
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
23
+ So `pres` produces small classes that contain and delegate to an existing object
24
24
  that handles server-side rendering.
25
25
 
26
- ## Presenter vs. Decorator
27
-
28
- Decorators add methods to a model. `pres` encourages you to whitelist model methods
29
- via delegation.
30
-
31
26
  ## Install
32
27
 
33
28
  Add it to your Gemfile:
@@ -36,11 +31,11 @@ Add it to your Gemfile:
36
31
  gem "pres"
37
32
  ```
38
33
 
39
- Include the `Presents` module:
34
+ Include the `Pres::Presents` module:
40
35
 
41
36
  ```ruby
42
37
  class ApplicationController
43
- include Presents
38
+ include Pres::Presents
44
39
  end
45
40
  ```
46
41
 
@@ -50,12 +45,14 @@ Add `app/presenters` to your application's autoload paths in `application.rb`:
50
45
  config.autoload_paths += %W( #{ config.root }/app/presenters )
51
46
  ```
52
47
 
53
- #### Example Usage
48
+ ### Usage
49
+
50
+ The quickest way to get started is to use the `Pres::Presenter` base class.
54
51
 
55
52
  Create a presenter class in `app/presenters`:
56
53
 
57
54
  ```ruby
58
- class DogePresenter < Presenter
55
+ class DogePresenter < Pres::Presenter
59
56
  # explicitly delegate methods to the model
60
57
  delegate :name, to: :object
61
58
 
@@ -88,9 +85,9 @@ class DogesController
88
85
  end
89
86
 
90
87
  private
91
-
88
+
92
89
  helper_method :doge
93
-
90
+
94
91
  def doge
95
92
  @doge ||= present(Doge.find(params[:id]))
96
93
  end
@@ -112,7 +109,7 @@ Use the presenter object in `doges/show.haml.html`
112
109
  Create a presenter class in `app/presenters`:
113
110
 
114
111
  ```ruby
115
- class DogePresenter < Presenter
112
+ class DogePresenter < Pres::Presenter
116
113
  # same as above
117
114
  end
118
115
  ```
@@ -151,7 +148,7 @@ Or use each:
151
148
 
152
149
  #### Present with options
153
150
 
154
- Use keyword arguements (or an options hash) to pass additional options to a
151
+ Use keyword arguments (or an options hash) to pass additional options to a
155
152
  Presenter:
156
153
 
157
154
  ```ruby
@@ -193,12 +190,12 @@ a presenter in your view code, make the `present` method visible to your views:
193
190
 
194
191
  ```ruby
195
192
  class ApplicationController
196
- include Presents
193
+ include Pres::Presents
197
194
  helper_method :present
198
195
  end
199
196
  ```
200
197
 
201
- ## More Goodness
198
+ ### More Goodness
202
199
 
203
200
  #### Presenters are objects
204
201
 
@@ -219,7 +216,7 @@ end
219
216
  You can override methods without resorting to convoluted method names:
220
217
 
221
218
  ```ruby
222
- class DogePresenter < Presenter
219
+ class DogePresenter < Pres::Presenter
223
220
  include Shared
224
221
 
225
222
  def truncated_name(length: 60)
@@ -232,7 +229,7 @@ end
232
229
  #### Presenters can create other presenters
233
230
 
234
231
  ```ruby
235
- class DogePresenter < Presenter
232
+ class DogePresenter < Pres::Presenter
236
233
  def cats
237
234
  present(object.cats)
238
235
  end
@@ -243,6 +240,34 @@ end
243
240
  = render doge.cats
244
241
  ```
245
242
 
243
+ ### Using mixins instead of inheritance
244
+
245
+ If you don't want to inherit from `Pres::Presenter`, you can include
246
+ `Pres::ViewDelegation` and implement your own initializer (so the `present` helper
247
+ will work).
248
+
249
+ ```ruby
250
+ class DogePresenter < SimpleDelegator
251
+ include Pres::ViewDelegation
252
+
253
+ # you need to write your own initializer with SimpleDelegator
254
+ def initialize(object, view_context, options = {})
255
+ super
256
+ @view_context = view_context
257
+ end
258
+ ```
259
+
260
+ ```haml
261
+ = doge.name
262
+ ```
263
+
264
+ see [SimpleDelegator](http://ruby-doc.org/stdlib-2.3.0/libdoc/delegate/rdoc/SimpleDelegator.html)
265
+
266
+ ## Updating to version 1.0
267
+
268
+ Modules and classes have been moved into the `Pres` namespace with version 1.0.
269
+ Change your code references to `Pres::Presents` and `Pres::Presenter`.
270
+
246
271
  ## References
247
272
 
248
273
  * http://nithinbekal.com/posts/rails-presenters/
@@ -1,22 +1,18 @@
1
1
  # Base Presenter class
2
2
  # Construct with object, view_context, and optional options
3
- class Presenter
4
- attr_accessor :object, :options, :view_context
3
+ module Pres
4
+ class Presenter
5
+ include Presents
6
+ include ViewDelegation
5
7
 
6
- delegate :id, :to_partial_path, to: :object
8
+ attr_reader :object, :options
7
9
 
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
-
14
- # Send missing symbols to view_context
15
- def method_missing(symbol, *args, &block)
16
- view_context.send(symbol, *args, &block)
17
- end
10
+ delegate :id, :to_partial_path, to: :object
18
11
 
19
- def respond_to?(symbol, _ = false)
20
- super || view_context.respond_to?(symbol, true)
12
+ def initialize(object, view_context = nil, options = {})
13
+ @object = object
14
+ @view_context = view_context
15
+ @options = options
16
+ end
21
17
  end
22
18
  end
data/lib/pres/presents.rb CHANGED
@@ -1,35 +1,37 @@
1
- module Presents
2
- private
1
+ module Pres
2
+ module Presents
3
+ private
3
4
 
4
- # Wrap an object or collection of objects with a presenter class.
5
- #
6
- # object - a ruby class, or nil
7
- # presenter - a Presenter class (optional)
8
- #
9
- # Examples
10
- #
11
- # user = User.new
12
- # present(user, cool: true)
13
- # => #<UserPresenter object: #<User> ...>
14
- #
15
- # user = User.new
16
- # present(user, presenter: NiceUserPresenter, cool: true)
17
- # => #<NiceUserPresenter object: #<User> ...>
18
- #
19
- # present([user])
20
- # => [#<UserPresenter object: #<User> ...>]
21
- #
22
- # present(nil)
23
- # => [#<Presenter object: nil ...>]
24
- #
25
- # Returns a new Presenter object or array of new Presenter objects
26
- def present(object, presenter: nil, **args)
27
- if object.respond_to?(:to_ary)
28
- object.map { |item| present(item, presenter: presenter, **args) }
29
- else
30
- presenter ||= Presenter if object.nil?
31
- presenter ||= Object.const_get("#{object.class.name}Presenter")
32
- presenter.new(object, view_context, **args)
5
+ # Wrap an object or collection of objects with a presenter class.
6
+ #
7
+ # object - a ruby class, or nil
8
+ # presenter - a Presenter class (optional)
9
+ #
10
+ # Examples
11
+ #
12
+ # user = User.new
13
+ # present(user, cool: true)
14
+ # => #<UserPresenter object: #<User> ...>
15
+ #
16
+ # user = User.new
17
+ # present(user, presenter: NiceUserPresenter, cool: true)
18
+ # => #<NiceUserPresenter object: #<User> ...>
19
+ #
20
+ # present([user])
21
+ # => [#<UserPresenter object: #<User> ...>]
22
+ #
23
+ # present(nil)
24
+ # => [#<Presenter object: nil ...>]
25
+ #
26
+ # Returns a new Presenter object or array of new Presenter objects
27
+ def present(object, presenter: nil, **args)
28
+ if object.respond_to?(:to_ary)
29
+ object.map { |item| present(item, presenter: presenter, **args) }
30
+ else
31
+ presenter ||= Presenter if object.nil?
32
+ presenter ||= Object.const_get("#{object.class.name}Presenter")
33
+ presenter.new(object, view_context, **args)
34
+ end
33
35
  end
34
36
  end
35
37
  end
data/lib/pres/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pres
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,22 @@
1
+ module Pres
2
+ module ViewDelegation
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attr_reader :view_context
7
+ end
8
+
9
+ # Send missing symbols to view_context first
10
+ def method_missing(symbol, *args, &block)
11
+ if view_context.respond_to?(symbol, true)
12
+ view_context.send(symbol, *args, &block)
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def respond_to_missing?(symbol, _ = false)
19
+ view_context.respond_to?(symbol, true) || super
20
+ end
21
+ end
22
+ end
data/lib/pres.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require "active_support/core_ext/module/delegation"
2
+ require "active_support/concern"
2
3
  require "pres/version"
3
- require "pres/presenter.rb"
4
- require "pres/presents.rb"
4
+ require "pres/view_delegation"
5
+ require "pres/presents"
6
+ require "pres/presenter"
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.2.0
4
+ version: 1.0.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-29 00:00:00.000000000 Z
12
+ date: 2016-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -95,6 +95,7 @@ files:
95
95
  - lib/pres/presenter.rb
96
96
  - lib/pres/presents.rb
97
97
  - lib/pres/version.rb
98
+ - lib/pres/view_delegation.rb
98
99
  homepage: https://github.com/neighborland/pres
99
100
  licenses:
100
101
  - MIT