oprah 0.1.2 → 0.1.3
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/.gitignore +1 -0
- data/.travis.yml +1 -1
- data/CHANGELOG.md +11 -0
- data/README.md +50 -1
- data/lib/oprah.rb +11 -12
- data/lib/oprah/controller_helpers.rb +80 -17
- data/lib/oprah/presenter.rb +38 -13
- data/lib/oprah/test_helpers.rb +40 -4
- data/lib/oprah/version.rb +1 -1
- data/oprah.gemspec +6 -4
- data/test/controllers/posts_controller_test.rb +11 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/controllers/posts_controller.rb +5 -0
- data/test/dummy/app/models/post.rb +2 -0
- data/test/dummy/app/presenters/post_presenter.rb +5 -0
- data/test/dummy/app/views/posts/show.html.erb +3 -0
- data/test/dummy/config/database.yml +6 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/init.rb +29 -0
- data/test/dummy/tmp/.gitkeep +0 -0
- data/test/oprah/controller_helpers_test.rb +8 -8
- data/test/oprah/presenter_test.rb +58 -0
- metadata +65 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4011edd6b8e184f5ec62fabddffa5cade44f4b0b
|
4
|
+
data.tar.gz: b6403c0fbb058e6c94115604eb59f6a748888e2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c80a890fa2610c667edd35148f7b2beee0c4c7fbac4f7804ffe8924c26689d1173b7b89124832a0bf8e00afd3ac0a2eb517bdd4a7b4bff597a42adeb477e8025
|
7
|
+
data.tar.gz: 7afe2120219f96cd17d5b820cff5cb2654b7e7a4b85aae8632cf086d9b73033119968dccdd7feb7995d102c930d756ae52e844be4bddbf58d6f85b718f1bb35b
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
0.1.3
|
2
|
+
-----
|
3
|
+
|
4
|
+
- Presenters can now be specified using the `only:` keyword
|
5
|
+
argument, which takes either a Class or an Array of classes
|
6
|
+
- Replace repeated method default arguments with splats
|
7
|
+
- Add assertions to TestHelper
|
8
|
+
- Delegate to most recent view context in presenters created
|
9
|
+
from within Rails controllers [#1]
|
10
|
+
- Add #present and #present_many methods to Presenter [#2]
|
11
|
+
|
1
12
|
0.1.2
|
2
13
|
-----
|
3
14
|
|
data/README.md
CHANGED
@@ -18,6 +18,10 @@ Opinionated presenters for Rails 5 - without the cruft.
|
|
18
18
|
* [Composition](#composition)
|
19
19
|
+ [Performance](#performance)
|
20
20
|
+ [Ordering](#ordering)
|
21
|
+
+ [Choosing presenters](#choosing-presenters)
|
22
|
+
* [Testing](#testing)
|
23
|
+
* [API Documentation](#api-documentation)
|
24
|
+
* [Contributing](#contributing)
|
21
25
|
* [License](#license)
|
22
26
|
* [Author](#author)
|
23
27
|
|
@@ -32,7 +36,7 @@ goals in mind only covered partially (or not at all) by other gems:
|
|
32
36
|
|
33
37
|
- Lightweight
|
34
38
|
- Presenters should be easy to test
|
35
|
-
-
|
39
|
+
- Avoid monkey patching, where possible :monkey::gun:
|
36
40
|
- Embrace convention over configuration
|
37
41
|
- First-class support for composition (modules and concerns)
|
38
42
|
|
@@ -239,6 +243,51 @@ Oprah.present(User.new).baz
|
|
239
243
|
# => "foobar"
|
240
244
|
```
|
241
245
|
|
246
|
+
### Choosing presenters
|
247
|
+
|
248
|
+
When presenting an object you can optionally choose which presenter classes
|
249
|
+
to use:
|
250
|
+
|
251
|
+
``` ruby
|
252
|
+
Oprah.present(User.new, only: DescribablePresenter)
|
253
|
+
```
|
254
|
+
|
255
|
+
This parameter takes either a single presenter or an `Array` of presenters.
|
256
|
+
The presenter(s) given need to match the object's class or one of it's
|
257
|
+
ancestors. Non-matching presenters given will be ignored.
|
258
|
+
|
259
|
+
## Testing
|
260
|
+
|
261
|
+
Testing presenters is as simple as testing a regular class. Oprah also
|
262
|
+
provides couple of helpers to make it even easier:
|
263
|
+
|
264
|
+
``` ruby
|
265
|
+
class UserPresenterTest < Minitest::Test
|
266
|
+
include Oprah::TestHelpers
|
267
|
+
|
268
|
+
def setup
|
269
|
+
@presenter = present User.new
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_presented
|
273
|
+
assert_presented @presenter
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_name
|
277
|
+
assert_equal "John Doe", @presenter.name
|
278
|
+
end
|
279
|
+
end
|
280
|
+
```
|
281
|
+
|
282
|
+
## API Documentation
|
283
|
+
|
284
|
+
Comprehensive API Documentation is available at
|
285
|
+
[rubydoc.info](http://www.rubydoc.info/gems/oprah).
|
286
|
+
|
287
|
+
## Contributing
|
288
|
+
|
289
|
+
Please check out our [contributing guidelines](CONTRIBUTING.md).
|
290
|
+
|
242
291
|
## License
|
243
292
|
|
244
293
|
Released under the MIT license. See the LICENSE file for details.
|
data/lib/oprah.rb
CHANGED
@@ -5,6 +5,7 @@ require 'singleton'
|
|
5
5
|
# gems
|
6
6
|
require 'active_support/concern'
|
7
7
|
require 'active_support/inflector'
|
8
|
+
require 'active_support/proxy_object'
|
8
9
|
require 'action_controller'
|
9
10
|
|
10
11
|
# internal
|
@@ -16,28 +17,26 @@ require 'oprah/version'
|
|
16
17
|
require 'oprah/railtie' if defined?(Rails)
|
17
18
|
|
18
19
|
# The Oprah namespace.
|
20
|
+
#
|
21
|
+
# @since 0.0.1
|
19
22
|
module Oprah
|
20
23
|
# @!visibility private
|
21
24
|
def debug?
|
22
25
|
!!ENV["OPRAH_DEBUG"]
|
23
26
|
end
|
24
27
|
|
25
|
-
#
|
28
|
+
# Presents a single object.
|
26
29
|
#
|
27
|
-
# @
|
28
|
-
|
29
|
-
|
30
|
-
def present(object, view_context: Presenter.default_view_context)
|
31
|
-
Presenter.present(object, view_context: view_context)
|
30
|
+
# @see Presenter.present
|
31
|
+
def present(*args, **kwargs, &block)
|
32
|
+
Presenter.present(*args, **kwargs, &block)
|
32
33
|
end
|
33
34
|
|
34
|
-
#
|
35
|
+
# Presents a collection of objects.
|
35
36
|
#
|
36
|
-
# @
|
37
|
-
|
38
|
-
|
39
|
-
def present_many(objects, view_context: Presenter.default_view_context)
|
40
|
-
Presenter.present_many(objects, view_context: view_context)
|
37
|
+
# @see Presenter.present_many
|
38
|
+
def present_many(*args, **kwargs, &block)
|
39
|
+
Presenter.present_many(*args, **kwargs, &block)
|
41
40
|
end
|
42
41
|
|
43
42
|
extend self
|
@@ -2,6 +2,29 @@ module Oprah
|
|
2
2
|
# Helpers that will be mixed into `ActionController::Base` by
|
3
3
|
# the {Oprah::Railtie}.
|
4
4
|
module ControllerHelpers
|
5
|
+
# A proxy class to delegate method calls to view contexts in presenters
|
6
|
+
# to the most recently created view context by
|
7
|
+
# {ControllerHelpers#view_context}.
|
8
|
+
#
|
9
|
+
# `ViewContextProxy` objects are automatically created in
|
10
|
+
# {ControllerHelpers#present} and {ControllerHelpers#present_many} and
|
11
|
+
# shouldn't have to be created manually.
|
12
|
+
#
|
13
|
+
# @since 0.1.3
|
14
|
+
class ViewContextProxy < ActiveSupport::ProxyObject
|
15
|
+
# @param [ActionController::Base] controller
|
16
|
+
# The controller to delegate to.
|
17
|
+
def initialize(controller)
|
18
|
+
@controller = controller
|
19
|
+
end
|
20
|
+
|
21
|
+
# Delegates all method calls to the `ActionView::Base` returned from
|
22
|
+
# {ControllerHelpers#oprah_view_context}.
|
23
|
+
def method_missing(meth, *args, &block)
|
24
|
+
@controller.oprah_view_context.__send__(meth, *args, &block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
5
28
|
extend ActiveSupport::Concern
|
6
29
|
|
7
30
|
included do
|
@@ -9,38 +32,78 @@ module Oprah
|
|
9
32
|
helper_method :present_many
|
10
33
|
end
|
11
34
|
|
12
|
-
# Presents
|
35
|
+
# Presents a single object.
|
13
36
|
#
|
14
37
|
# Will pass the view context returned from {#oprah_view_context} to the
|
15
|
-
# presenter.
|
38
|
+
# presenter by default. This can be overridden.
|
16
39
|
#
|
17
|
-
# @
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
40
|
+
# @see Presenter.present
|
41
|
+
def present(*args, **kwargs, &block)
|
42
|
+
kwargs = {
|
43
|
+
view_context: oprah_view_context_proxy
|
44
|
+
}.merge(kwargs)
|
45
|
+
|
46
|
+
Presenter.present(*args, **kwargs, &block)
|
22
47
|
end
|
23
48
|
|
24
|
-
# Presents
|
49
|
+
# Presents a collection of objects.
|
25
50
|
#
|
26
51
|
# Will pass the view context returned from {#oprah_view_context} to the
|
27
|
-
# presenter.
|
52
|
+
# presenter by default. This can be overridden.
|
28
53
|
#
|
29
|
-
# @
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
54
|
+
# @see Presenter.present_many
|
55
|
+
def present_many(*args, **kwargs, &block)
|
56
|
+
kwargs = {
|
57
|
+
view_context: oprah_view_context_proxy
|
58
|
+
}.merge(kwargs)
|
59
|
+
|
60
|
+
Presenter.present_many(*args, **kwargs, &block)
|
34
61
|
end
|
35
62
|
|
36
|
-
# The view context automatically passed to presented
|
63
|
+
# The view context automatically passed to objects presented from this
|
64
|
+
# controller.
|
37
65
|
#
|
38
66
|
# You can override this method pass a custom view context to all
|
39
67
|
# presented objects from the controller scope.
|
40
68
|
#
|
41
|
-
# @
|
69
|
+
# @see #oprah_view_context=
|
70
|
+
# @return [ActionView::Base]
|
42
71
|
def oprah_view_context
|
43
|
-
view_context
|
72
|
+
@oprah_view_context || view_context
|
73
|
+
end
|
74
|
+
|
75
|
+
# Assigns the view context returned from {#oprah_view_context}.
|
76
|
+
#
|
77
|
+
# You can override this method pass a custom view context to all
|
78
|
+
# presented objects from the controller scope.
|
79
|
+
#
|
80
|
+
# @since 0.1.3
|
81
|
+
# @see #oprah_view_context
|
82
|
+
# @param [ActionView::Base] view_context The view context to assign
|
83
|
+
# @return [ActionView::Base]
|
84
|
+
def oprah_view_context=(view_context)
|
85
|
+
@oprah_view_context = view_context
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns an instance of a view class and sets the current view context
|
89
|
+
# returned by {#oprah_view_context}.
|
90
|
+
#
|
91
|
+
# If you override this method in your controller ensure you keep Oprah's
|
92
|
+
# view context updated using {#oprah_view_context=}.
|
93
|
+
#
|
94
|
+
# @since 0.1.3
|
95
|
+
# @see http://api.rubyonrails.org/classes/ActionView/Rendering.html#method-i-view_context
|
96
|
+
# Rails API Documentation
|
97
|
+
# @return [ActionView::Base]
|
98
|
+
def view_context
|
99
|
+
self.oprah_view_context = super
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
# @since 0.1.3
|
105
|
+
def oprah_view_context_proxy
|
106
|
+
@oprah_view_context_proxy ||= ViewContextProxy.new(self)
|
44
107
|
end
|
45
108
|
end
|
46
109
|
end
|
data/lib/oprah/presenter.rb
CHANGED
@@ -34,46 +34,49 @@ module Oprah
|
|
34
34
|
#
|
35
35
|
# @param object [Object] The object to present
|
36
36
|
# @param view_context [ActionView::Context] View context to assign
|
37
|
+
# @param only [Class] Class or Array of presenters to use
|
37
38
|
# @return [Presenter] Presented object
|
38
|
-
def present(object, view_context: default_view_context)
|
39
|
-
@@cache.lookup(object)
|
39
|
+
def present(object, view_context: default_view_context, only: nil)
|
40
|
+
presenters = @@cache.lookup(object)
|
41
|
+
presenters = presenters & (only.kind_of?(Array) ? only : [only]) if only
|
42
|
+
|
43
|
+
presenters.inject(object) do |memo, presenter|
|
40
44
|
presenter.new(memo, view_context: view_context)
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
48
|
# Presents the given `objects` with all their matching presenters.
|
45
|
-
# The
|
49
|
+
# The behaviour and parameters are identical to `.present`'s.
|
46
50
|
#
|
47
51
|
# @param objects [Enumerable] The objects to present
|
48
|
-
# @
|
49
|
-
|
50
|
-
|
51
|
-
objects.map do |object|
|
52
|
-
present(object, view_context: view_context)
|
53
|
-
end
|
52
|
+
# @see .present
|
53
|
+
def present_many(objects, **kwargs)
|
54
|
+
objects.map { |object| present(object, **kwargs) }
|
54
55
|
end
|
55
56
|
|
56
57
|
# Automatically wrap the objects returned by the given one-to-one
|
57
58
|
# `association` method in presenters.
|
58
59
|
#
|
60
|
+
# Presenters will re-use the parent's assigned view context.
|
61
|
+
#
|
59
62
|
# @param association [Symbol] Name of the association
|
60
63
|
# @return [Boolean]
|
61
64
|
def presents_one(association)
|
62
65
|
define_method association do
|
63
|
-
|
64
|
-
object.__send__(association), view_context: view_context)
|
66
|
+
present(object.__send__(association), view_context: view_context)
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
68
70
|
# Automatically wrap the objects returned by the given one-to-many
|
69
71
|
# or many-to-many `association` method in presenters.
|
70
72
|
#
|
73
|
+
# Presenters will re-use the parent's assigned view context.
|
74
|
+
#
|
71
75
|
# @param association [Symbol] Name of the association
|
72
76
|
# @return [Boolean]
|
73
77
|
def presents_many(association)
|
74
78
|
define_method association do
|
75
|
-
|
76
|
-
object.__send__(association), view_context: view_context)
|
79
|
+
present_many(object.__send__(association), view_context: view_context)
|
77
80
|
end
|
78
81
|
|
79
82
|
true
|
@@ -106,6 +109,28 @@ module Oprah
|
|
106
109
|
end
|
107
110
|
end
|
108
111
|
|
112
|
+
# Presents a single object.
|
113
|
+
#
|
114
|
+
# Will re-use the presenter's assigned view context if no `view_context`:
|
115
|
+
# parameter is given.
|
116
|
+
#
|
117
|
+
# @see .present
|
118
|
+
def present(*args, **kwargs, &block)
|
119
|
+
kwargs = { view_context: view_context }.merge(kwargs)
|
120
|
+
self.class.present(*args, **kwargs, &block)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Presents a collection of objects.
|
124
|
+
#
|
125
|
+
# Will re-use the presenter's assigned view context if no `view_context`:
|
126
|
+
# parameter is given.
|
127
|
+
#
|
128
|
+
# @see .present_many
|
129
|
+
def present_many(*args, **kwargs, &block)
|
130
|
+
kwargs = { view_context: view_context }.merge(kwargs)
|
131
|
+
self.class.present_many(*args, **kwargs, &block)
|
132
|
+
end
|
133
|
+
|
109
134
|
# Returns true if either `object` or `self` responds to the given method
|
110
135
|
# name.
|
111
136
|
#
|
data/lib/oprah/test_helpers.rb
CHANGED
@@ -1,11 +1,47 @@
|
|
1
1
|
module Oprah
|
2
|
+
# Test helpers that can be included into `Minitest::Test` or
|
3
|
+
# `ActiveSupport::TestCase`.
|
4
|
+
#
|
5
|
+
# @since 0.1.2
|
2
6
|
module TestHelpers
|
3
|
-
|
4
|
-
|
7
|
+
# Presents a collection of objects.
|
8
|
+
#
|
9
|
+
# @see Presenter.present_many
|
10
|
+
def present_many(*args, **kwargs, &block)
|
11
|
+
Presenter.present_many(*args, **kwargs, &block)
|
5
12
|
end
|
6
13
|
|
7
|
-
|
8
|
-
|
14
|
+
# Presents a single object.
|
15
|
+
#
|
16
|
+
# @see Presenter.present
|
17
|
+
def present(*args, **kwargs, &block)
|
18
|
+
Presenter.present(*args, **kwargs, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Fails unless `object` is a presenter.
|
22
|
+
#
|
23
|
+
# @since 0.1.3
|
24
|
+
# @param [Object] object The object to be tested
|
25
|
+
# @return [Boolean]
|
26
|
+
def assert_presented(object)
|
27
|
+
msg = message(msg) do
|
28
|
+
"Expected #{mu_pp(object)} to be an Oprah::Presenter"
|
29
|
+
end
|
30
|
+
|
31
|
+
assert object.kind_of?(Oprah::Presenter), msg
|
32
|
+
end
|
33
|
+
|
34
|
+
# Fails if `object` is a presenter.
|
35
|
+
#
|
36
|
+
# @since 0.1.3
|
37
|
+
# @param [Object] object The object to be tested
|
38
|
+
# @return [Boolean]
|
39
|
+
def refute_presented(object)
|
40
|
+
msg = message(msg) do
|
41
|
+
"Expected #{mu_pp(object)} to not be an Oprah::Presenter"
|
42
|
+
end
|
43
|
+
|
44
|
+
refute object.kind_of?(Oprah::Presenter), msg
|
9
45
|
end
|
10
46
|
end
|
11
47
|
end
|
data/lib/oprah/version.rb
CHANGED
data/oprah.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.summary = "Opinionated presenters for Rails 5 - without the cruft"
|
12
12
|
gem.description = gem.summary
|
13
13
|
gem.homepage = "https://github.com/endofunky/oprah"
|
14
|
-
gem.license = "
|
14
|
+
gem.license = "MIT"
|
15
15
|
|
16
16
|
gem.files = `git ls-files -z`.split("\x0")
|
17
17
|
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
@@ -23,9 +23,11 @@ Gem::Specification.new do |gem|
|
|
23
23
|
gem.add_dependency "activesupport", ">= 5.0.0"
|
24
24
|
gem.add_dependency "actionpack", ">= 5.0.0"
|
25
25
|
|
26
|
-
gem.add_development_dependency "yard", "~> 0.9.5"
|
27
|
-
gem.add_development_dependency "redcarpet", "~> 3.3.4"
|
28
26
|
gem.add_development_dependency "bundler", "~> 1.7"
|
27
|
+
gem.add_development_dependency "minitest", "~> 5.9.0"
|
28
|
+
gem.add_development_dependency "rails", ">= 5.0.0"
|
29
29
|
gem.add_development_dependency "rake", "~> 10.0"
|
30
|
-
gem.add_development_dependency "
|
30
|
+
gem.add_development_dependency "redcarpet", "~> 3.3.4"
|
31
|
+
gem.add_development_dependency "yard", "~> 0.9.5"
|
32
|
+
gem.add_development_dependency "sqlite3", "~> 1.3.11"
|
31
33
|
end
|
data/test/dummy/init.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __FILE__)
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'rails/all'
|
5
|
+
Bundler.require(:default, Rails.env)
|
6
|
+
|
7
|
+
module Dummy
|
8
|
+
class Application < ::Rails::Application
|
9
|
+
config.root = File.join __FILE__, '..'
|
10
|
+
config.cache_store = :memory_store
|
11
|
+
config.assets.enabled = false
|
12
|
+
config.active_support.test_order = :random
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
config.action_dispatch.show_exceptions = false
|
16
|
+
config.action_controller.allow_forgery_protection = false
|
17
|
+
config.action_mailer.delivery_method = :test
|
18
|
+
config.active_support.deprecation = :stderr
|
19
|
+
config.allow_concurrency = true
|
20
|
+
config.cache_classes = true
|
21
|
+
config.dependency_loading = true
|
22
|
+
config.preload_frameworks = true
|
23
|
+
config.eager_load = true
|
24
|
+
config.secret_key_base = '012345678901234567890123456789'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Dummy::Application.initialize!
|
29
|
+
require 'rails/test_help'
|
File without changes
|
@@ -35,25 +35,25 @@ module Oprah
|
|
35
35
|
assert_kind_of UserPresenter, presenter
|
36
36
|
assert_kind_of EntityPresenter, presenter
|
37
37
|
|
38
|
-
assert_equal
|
38
|
+
assert_equal "ok", presenter.view_context.to_s
|
39
39
|
end
|
40
40
|
|
41
|
-
def
|
41
|
+
def test_present_custom_view_context
|
42
|
+
presenter = @controller.present(User.new, view_context: :foobar)
|
43
|
+
assert_equal :foobar, presenter.view_context
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_present_many
|
42
47
|
presenters = @controller.present_many([User.new, User.new])
|
43
48
|
|
44
49
|
assert_equal 2, presenters.length
|
45
50
|
|
46
51
|
presenters.each do |presenter|
|
47
52
|
assert_equal "Foo Bar", presenter.name
|
48
|
-
assert_equal
|
53
|
+
assert_equal "ok", presenter.view_context.to_s
|
49
54
|
end
|
50
55
|
end
|
51
56
|
|
52
|
-
def test_present_custom_view_context
|
53
|
-
presenter = @controller.present(User.new, view_context: :foobar)
|
54
|
-
assert_equal :foobar, presenter.view_context
|
55
|
-
end
|
56
|
-
|
57
57
|
def test_helper_method
|
58
58
|
assert_equal [:present, :present_many], Controller.helper_methods
|
59
59
|
end
|
@@ -10,6 +10,32 @@ module Oprah
|
|
10
10
|
assert_equal UserPresenter.cache, Presenter.cache
|
11
11
|
end
|
12
12
|
|
13
|
+
def test_present
|
14
|
+
assert_presented present(User.new)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_present_no_matching_presenter
|
18
|
+
object = Object.new
|
19
|
+
presented = present(object)
|
20
|
+
|
21
|
+
refute_presented presented
|
22
|
+
assert_equal presented, object
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_present_only
|
26
|
+
presenter = present(User.new, only: UserPresenter)
|
27
|
+
|
28
|
+
assert_kind_of UserPresenter, presenter
|
29
|
+
refute_kind_of EntityPresenter, presenter
|
30
|
+
|
31
|
+
classes = [UserPresenter, EntityPresenter, CommentPresenter]
|
32
|
+
presenter = present(User.new, only: classes)
|
33
|
+
|
34
|
+
assert_kind_of UserPresenter, presenter
|
35
|
+
assert_kind_of EntityPresenter, presenter
|
36
|
+
refute_kind_of CommentPresenter, presenter
|
37
|
+
end
|
38
|
+
|
13
39
|
def test_present_many
|
14
40
|
present_many([User.new, User.new]).each do |presenter|
|
15
41
|
assert_equal "Foo Bar", presenter.name
|
@@ -27,6 +53,38 @@ module Oprah
|
|
27
53
|
end
|
28
54
|
end
|
29
55
|
|
56
|
+
def test_present_from_instance
|
57
|
+
view_context = Object.new
|
58
|
+
presenter = present(User.new, view_context: view_context)
|
59
|
+
presented = presenter.present(User.new)
|
60
|
+
|
61
|
+
assert_equal view_context.object_id, presented.view_context.object_id
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_present_many_from_instance
|
65
|
+
view_context = Object.new
|
66
|
+
presenter = present(User.new, view_context: view_context)
|
67
|
+
presented = presenter.present_many([User.new]).first
|
68
|
+
|
69
|
+
assert_equal view_context.object_id, presented.view_context.object_id
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_present_from_instance_custom_view_context
|
73
|
+
view_context = Object.new
|
74
|
+
presenter = present(User.new, view_context: view_context)
|
75
|
+
presented = presenter.present(User.new, view_context: :ok)
|
76
|
+
|
77
|
+
assert_equal :ok, presented.view_context
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_present_many_from_instance_custom_view_context
|
81
|
+
view_context = Object.new
|
82
|
+
presenter = present(User.new, view_context: view_context)
|
83
|
+
presented = presenter.present_many([User.new], view_context: :ok).first
|
84
|
+
|
85
|
+
assert_equal :ok, presented.view_context
|
86
|
+
end
|
87
|
+
|
30
88
|
def test_presents_one
|
31
89
|
project = present(Project.new)
|
32
90
|
owner = project.owner
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oprah
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Svensson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -39,47 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 5.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: '1.7'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: '1.7'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 5.9.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 5.9.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 5.0.0
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 5.0.0
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rake
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,19 +95,47 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '10.0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: redcarpet
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.3.4
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.3.4
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.9.5
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.9.5
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: sqlite3
|
99
127
|
requirement: !ruby/object:Gem::Requirement
|
100
128
|
requirements:
|
101
129
|
- - "~>"
|
102
130
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
131
|
+
version: 1.3.11
|
104
132
|
type: :development
|
105
133
|
prerelease: false
|
106
134
|
version_requirements: !ruby/object:Gem::Requirement
|
107
135
|
requirements:
|
108
136
|
- - "~>"
|
109
137
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
138
|
+
version: 1.3.11
|
111
139
|
description: Opinionated presenters for Rails 5 - without the cruft
|
112
140
|
email:
|
113
141
|
- tob@tobiassvensson.co.uk
|
@@ -135,13 +163,23 @@ files:
|
|
135
163
|
- tasks/console.rb
|
136
164
|
- tasks/minitest.rb
|
137
165
|
- tasks/yard.rb
|
166
|
+
- test/controllers/posts_controller_test.rb
|
167
|
+
- test/dummy/app/controllers/application_controller.rb
|
168
|
+
- test/dummy/app/controllers/posts_controller.rb
|
169
|
+
- test/dummy/app/models/post.rb
|
170
|
+
- test/dummy/app/presenters/post_presenter.rb
|
171
|
+
- test/dummy/app/views/posts/show.html.erb
|
172
|
+
- test/dummy/config/database.yml
|
173
|
+
- test/dummy/config/routes.rb
|
174
|
+
- test/dummy/init.rb
|
175
|
+
- test/dummy/tmp/.gitkeep
|
138
176
|
- test/helper.rb
|
139
177
|
- test/oprah/controller_helpers_test.rb
|
140
178
|
- test/oprah/oprah_test.rb
|
141
179
|
- test/oprah/presenter_test.rb
|
142
180
|
homepage: https://github.com/endofunky/oprah
|
143
181
|
licenses:
|
144
|
-
-
|
182
|
+
- MIT
|
145
183
|
metadata: {}
|
146
184
|
post_install_message:
|
147
185
|
rdoc_options: []
|
@@ -164,6 +202,16 @@ signing_key:
|
|
164
202
|
specification_version: 4
|
165
203
|
summary: Opinionated presenters for Rails 5 - without the cruft
|
166
204
|
test_files:
|
205
|
+
- test/controllers/posts_controller_test.rb
|
206
|
+
- test/dummy/app/controllers/application_controller.rb
|
207
|
+
- test/dummy/app/controllers/posts_controller.rb
|
208
|
+
- test/dummy/app/models/post.rb
|
209
|
+
- test/dummy/app/presenters/post_presenter.rb
|
210
|
+
- test/dummy/app/views/posts/show.html.erb
|
211
|
+
- test/dummy/config/database.yml
|
212
|
+
- test/dummy/config/routes.rb
|
213
|
+
- test/dummy/init.rb
|
214
|
+
- test/dummy/tmp/.gitkeep
|
167
215
|
- test/helper.rb
|
168
216
|
- test/oprah/controller_helpers_test.rb
|
169
217
|
- test/oprah/oprah_test.rb
|