meta_presenter 0.1.3 → 0.1.4
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/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/meta_presenter.rb +7 -0
- data/lib/meta_presenter/base.rb +12 -10
- data/lib/meta_presenter/base/delegate_all_to.rb +16 -5
- data/lib/meta_presenter/base/delegate_to_controller.rb +15 -1
- data/lib/meta_presenter/builder.rb +19 -3
- data/lib/meta_presenter/helpers.rb +13 -0
- data/meta_presenter.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f337e9c7d7719689f5bf9f34215265b34eb197dd1dde628c1d227812db8461bc
|
4
|
+
data.tar.gz: a0c2c339a4b9afa58085b50d815054aa935a4369a5477fbb7eaaf0fbc1028310
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec286fb53acbae17137ca1cb6b84f7d9a64faab49b4e2b355ba19a8df41400fe1fb90b0bbb681038adedba4aca01a8033ecfd9af4376abedba6ca0521abc58dd
|
7
|
+
data.tar.gz: 11f1d08524cde6ed6c5cbfaf336f5d54bb99786e0cdb28263fc372d539faf84ee57a23bdc3252347fa3d71d82a4c63122adc2297295a03948aee31d20f17d365
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://badge.fury.io/rb/meta_presenter) [](https://travis-ci.org/szTheory/meta_presenter) [](https://coveralls.io/github/szTheory/meta_presenter?branch=master) [](http://inch-ci.org/github/szTheory/meta_presenter) [](https://github.com/szTheory/meta_presenter/blob/master/LICENSE.txt) [](https://rubygems.org/gems/meta_presenter) [](https://github.com/szTheory/meta_presenter)
|
1
|
+
[](https://badge.fury.io/rb/meta_presenter) [](https://travis-ci.org/szTheory/meta_presenter) [](https://coveralls.io/github/szTheory/meta_presenter?branch=master) [](http://inch-ci.org/github/szTheory/meta_presenter) [](https://codeclimate.com/github/szTheory/meta_presenter/maintainability) [](https://github.com/szTheory/meta_presenter/blob/master/LICENSE.txt) [](https://rubygems.org/gems/meta_presenter) [](https://github.com/szTheory/meta_presenter)
|
2
2
|
|
3
3
|
# MetaPresenter
|
4
4
|
|
data/lib/meta_presenter.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
require 'meta_presenter/helpers'
|
2
2
|
require 'meta_presenter/base'
|
3
3
|
|
4
|
+
# MetaPresenter is a Ruby gem that gives you access to the
|
5
|
+
# powerful presenter pattern in your Rails controllers. For each
|
6
|
+
# controller/action pair you get a presenter class in `app/presenters`
|
7
|
+
# that you can use in your views with with `presenter.method_name`.
|
8
|
+
# This helps you decompose your helper logic into tight, easily
|
9
|
+
# testable classes. There's even a DSL for method delegation
|
10
|
+
# on objects to reduce boilerplate.
|
4
11
|
module MetaPresenter
|
5
12
|
end
|
data/lib/meta_presenter/base.rb
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
require_relative 'base/delegate_all_to.rb'
|
2
2
|
require_relative 'base/delegate_to_controller.rb'
|
3
3
|
|
4
|
-
# Base presenter class. Inherit from this it in order
|
5
|
-
# to get a presenter you can use in your views
|
6
|
-
#
|
7
|
-
# # app/presenters/application_presenter.rb
|
8
|
-
# class ApplicationPresenter < MetaPresenter::Base
|
9
|
-
# def message
|
10
|
-
# "Hello"
|
11
|
-
# end
|
12
|
-
# end
|
13
|
-
#
|
14
4
|
module MetaPresenter
|
5
|
+
|
6
|
+
# Base presenter class. Inherit from this it in order
|
7
|
+
# to get a presenter you can use in your views
|
8
|
+
#
|
9
|
+
# # app/presenters/application_presenter.rb
|
10
|
+
# class ApplicationPresenter < MetaPresenter::Base
|
11
|
+
# def message
|
12
|
+
# "Hello"
|
13
|
+
# end
|
14
|
+
# end
|
15
15
|
class Base
|
16
16
|
include DelegateToController
|
17
17
|
# Comes last so `delegate_all_to` takes priority
|
18
18
|
# over default controller actions
|
19
19
|
include DelegateAllTo
|
20
20
|
|
21
|
+
# Displayed in errors involving the presenter
|
21
22
|
def inspect
|
23
|
+
# Concise to not dump too much information on the dev
|
22
24
|
"#<#{self.class.name}>"
|
23
25
|
end
|
24
26
|
end
|
@@ -3,15 +3,26 @@ require 'active_support/core_ext/class/attribute'
|
|
3
3
|
|
4
4
|
module MetaPresenter
|
5
5
|
class Base
|
6
|
+
# Give the presenter the ability to delegate methods to an object
|
6
7
|
module DelegateAllTo
|
8
|
+
|
7
9
|
extend ActiveSupport::Concern
|
8
10
|
included do
|
11
|
+
# Name of an object that when specified will delegate to
|
12
|
+
# all incoming methods send to the presenter that
|
13
|
+
# we not already handled by the presenter otherwise
|
14
|
+
# (such as explicitly defining the method)
|
9
15
|
class_attribute :delegate_all_to
|
10
|
-
|
16
|
+
|
11
17
|
include InstanceMethods
|
12
18
|
end
|
13
19
|
|
14
|
-
module InstanceMethods
|
20
|
+
module InstanceMethods # :nodoc:
|
21
|
+
|
22
|
+
# Check to see whether a method has been either
|
23
|
+
# defined by or is delegated by this presenter
|
24
|
+
#
|
25
|
+
# @param *args method name and the other arguments
|
15
26
|
def respond_to_missing?(*args)
|
16
27
|
method_name = args.first
|
17
28
|
delegate_all_responds_to?(method_name) || super
|
@@ -37,15 +48,15 @@ module MetaPresenter
|
|
37
48
|
# Temporarily set a flag that we are delegating
|
38
49
|
# to an underlying method. this allows us
|
39
50
|
# to chain additional methods calls onto the end
|
40
|
-
delegating = true
|
51
|
+
@delegating = true
|
41
52
|
send(self.class.delegate_all_to)
|
42
53
|
ensure
|
43
54
|
# Cleanup the flag afterwards to close the door behind us
|
44
|
-
delegating = false
|
55
|
+
@delegating = false
|
45
56
|
end
|
46
57
|
|
47
58
|
def delegating_all_to?
|
48
|
-
delegating == true
|
59
|
+
@delegating == true
|
49
60
|
end
|
50
61
|
|
51
62
|
def delegate_all_to?
|
@@ -2,20 +2,34 @@ require 'active_support/concern'
|
|
2
2
|
|
3
3
|
module MetaPresenter
|
4
4
|
class Base
|
5
|
+
# Give the presenter the ability to delegate methods to
|
6
|
+
# a controller (ActionController::Base or ActionMailer::Base)
|
5
7
|
module DelegateToController
|
8
|
+
|
9
|
+
# Presenters delegate to private controller methods by default
|
6
10
|
INCLUDE_PRIVATE_METHODS = true
|
7
11
|
|
8
12
|
extend ActiveSupport::Concern
|
9
13
|
included do
|
14
|
+
# Controller that this presenter will delegate methods to
|
10
15
|
attr_reader :controller
|
16
|
+
|
11
17
|
include InstanceMethods
|
12
18
|
end
|
13
19
|
|
14
|
-
module InstanceMethods
|
20
|
+
module InstanceMethods # :nodoc:
|
21
|
+
|
22
|
+
# Creates a new presenter
|
23
|
+
#
|
24
|
+
# @param [ActionController::Base, ActionMailer::Base] controller Controller that this presenter will delegate methods to
|
15
25
|
def initialize(controller)
|
16
26
|
@controller = controller
|
17
27
|
end
|
18
28
|
|
29
|
+
# Check to see whether a method has been either
|
30
|
+
# defined by or is delegated by this presenter
|
31
|
+
#
|
32
|
+
# @param *args method name and the other arguments
|
19
33
|
def respond_to_missing?(*args)
|
20
34
|
method_name = args.first
|
21
35
|
delegates_controller_method? || super
|
@@ -1,21 +1,37 @@
|
|
1
1
|
require 'active_support/core_ext/object/try'
|
2
2
|
|
3
3
|
module MetaPresenter
|
4
|
+
|
5
|
+
# Builds a presenter class for a controller and method
|
4
6
|
class Builder
|
5
|
-
|
7
|
+
|
8
|
+
# Controller that this presenter will delegate methods to
|
9
|
+
attr_reader :controller
|
10
|
+
|
11
|
+
# Name of the controller's action to hook the presenter up to
|
12
|
+
attr_reader :action_name
|
13
|
+
|
14
|
+
# Creates a new Builder
|
15
|
+
#
|
16
|
+
# @param [ActionController::Base, ActionMailer::Base] controller Controller that this presenter will delegate methods to
|
17
|
+
# @param [String] action_name Name of the controller's action to hook the presenter up to
|
6
18
|
def initialize(controller, action_name)
|
7
19
|
@controller = controller
|
8
20
|
@action_name = action_name
|
9
21
|
end
|
10
22
|
|
11
|
-
#
|
12
|
-
# has gone wrong that the dev really should know about!
|
23
|
+
# Error for there's no presenter class defined but a file for it exists
|
13
24
|
class FileExistsButPresenterNotDefinedError < NameError
|
25
|
+
# Create a new error
|
26
|
+
#
|
27
|
+
# @param [String] presenter_class_name Class name of presenter
|
28
|
+
# @param [String] presenter_file_path File path where the presenter Ruby class was found at
|
14
29
|
def initialize(presenter_class_name, presenter_file_path)
|
15
30
|
super("Presenter class #{presenter_class_name} is not defined but file exists at #{presenter_file_path}")
|
16
31
|
end
|
17
32
|
end
|
18
33
|
|
34
|
+
# @return [Class] the presenter class for our controller and action combination
|
19
35
|
def presenter_class
|
20
36
|
# Try to find the class (it's not guaranteed)
|
21
37
|
klass_name = ancestors.find do |klass_name|
|
@@ -2,6 +2,19 @@ require 'meta_presenter/builder'
|
|
2
2
|
require 'active_support/concern'
|
3
3
|
|
4
4
|
module MetaPresenter
|
5
|
+
|
6
|
+
# Including this module in your controller will give
|
7
|
+
# your views access to a `presenter` method that
|
8
|
+
# delegates to controller methods
|
9
|
+
#
|
10
|
+
# class ApplicationController < ActionController::Base
|
11
|
+
# include MetaPresenter::Base
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# class ApplicationMailer < ActionMailer::Base
|
15
|
+
# include MetaPresenter::Base
|
16
|
+
# end
|
17
|
+
#
|
5
18
|
module Helpers
|
6
19
|
extend ActiveSupport::Concern
|
7
20
|
included do
|
data/meta_presenter.gemspec
CHANGED