Class: Lokap::Presenter
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Lokap::Presenter
- Defined in:
- lib/lokap/presenter.rb
Overview
Lokap::Presenter
is the base class for which all presenters inherit
Instance Attribute Summary collapse
-
#context ⇒ ActionView
Rails view context.
-
#options ⇒ Hash
Options passed to the presenter and the template.
Class Method Summary collapse
-
.presents(name) ⇒ Object
private
Defines a method to access the presented entity.
Instance Method Summary collapse
-
#class_path ⇒ String
private
Generates the name for the class segment of the path.
-
#default_path(partial = nil) ⇒ Object
private
Generates the default partial path when a template is not found or specified.
-
#default_template ⇒ Object
private
The default name for the template when specified template can't be found.
-
#initialize(entity, context, options = {}) ⇒ Lokap::Presenter
constructor
Creates a new
Lokap::Presenter
object. -
#locals ⇒ Object
private
Makes the presenter available to it's templates via
p.<method>
orpresenter.<method>
. -
#partial_options(partial = nil) ⇒ Hash
private
Generate Hash options to pass to the renderer.
-
#render(partial = nil) ⇒ String
Renders the specified template for a presenter.
-
#template_path(partial = nil) ⇒ String
private
The full path of the template to be rendered.
Constructor Details
#initialize(entity, context, options = {}) ⇒ Lokap::Presenter
Creates a new Lokap::Presenter
object
35 36 37 38 39 40 41 |
# File 'lib/lokap/presenter.rb', line 35 def initialize(entity, context, ={}) @entity = entity @context = context @options = super(@context) end |
Instance Attribute Details
#context ⇒ ActionView
Returns Rails view context.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lokap/presenter.rb', line 18 class Presenter < SimpleDelegator attr_reader :context attr_accessor :options # Creates a new `Lokap::Presenter` object # @see Lokap::ActionView#present # # @example # present(@person, :share).render # present(@person, :share).render(:twitter) # present(@person, :share).render('social/twitter') # # @param entity [Object] Object/entity to present # @param context [ActionView::Context] Rails view context for rendering # @param options [Hash] Options passed to the presenter and template # # @return [Lokap::Presenter] def initialize(entity, context, ={}) @entity = entity @context = context @options = super(@context) end # Renders the specified template for a presenter # @param partial [String|Symbol] The name of the partial in `app/views/presenters` # @return [String] HTML output def render(partial=nil) super (partial) rescue Lokap::ActionView::MissingTemplate super (default_path(partial)) end private # Defines a method to access the presented entity # @param name [String|Symbol] Name of the entity def self.presents(name) define_method(name) { @entity } end # Makes the presenter available to it's templates via `p.<method>` or # `presenter.<method>` def locals { presenter: self, p: self }.merge() end # Generate Hash options to pass to the renderer # @param partial [String] path to the template's partial # @return [Hash] These options are passed to `render :partial, <options>` def (partial=nil) { partial: template_path(partial), locals: locals } end # The full path of the template to be rendered # @param partial [String] The name of the partial to render # @return [String] Full path to the template's partial def template_path(partial=nil) template = partial || default_template "#{class_path}/#{template}".downcase end # Generates the name for the class segment of the path # @note Removes `_presenter` from the resulting string if present # @return [String] Path for the subclassed presenter def class_path self.class.name.underscore.gsub('_presenter', '').gsub('/presenter', '') end # Generates the default partial path when a template is not found or specified # @param partial [String] full path to the template's partial # @returns [String] full path to the specified template or the default template def default_path(partial=nil) return unless partial prefix = partial.to_s.split('/')[0..-2].join('/') [prefix, default_template].compact.join('/') end # The default name for the template when specified template can't be found def default_template 'default' end end |
#options ⇒ Hash
Returns Options passed to the presenter and the template.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lokap/presenter.rb', line 18 class Presenter < SimpleDelegator attr_reader :context attr_accessor :options # Creates a new `Lokap::Presenter` object # @see Lokap::ActionView#present # # @example # present(@person, :share).render # present(@person, :share).render(:twitter) # present(@person, :share).render('social/twitter') # # @param entity [Object] Object/entity to present # @param context [ActionView::Context] Rails view context for rendering # @param options [Hash] Options passed to the presenter and template # # @return [Lokap::Presenter] def initialize(entity, context, ={}) @entity = entity @context = context @options = super(@context) end # Renders the specified template for a presenter # @param partial [String|Symbol] The name of the partial in `app/views/presenters` # @return [String] HTML output def render(partial=nil) super (partial) rescue Lokap::ActionView::MissingTemplate super (default_path(partial)) end private # Defines a method to access the presented entity # @param name [String|Symbol] Name of the entity def self.presents(name) define_method(name) { @entity } end # Makes the presenter available to it's templates via `p.<method>` or # `presenter.<method>` def locals { presenter: self, p: self }.merge() end # Generate Hash options to pass to the renderer # @param partial [String] path to the template's partial # @return [Hash] These options are passed to `render :partial, <options>` def (partial=nil) { partial: template_path(partial), locals: locals } end # The full path of the template to be rendered # @param partial [String] The name of the partial to render # @return [String] Full path to the template's partial def template_path(partial=nil) template = partial || default_template "#{class_path}/#{template}".downcase end # Generates the name for the class segment of the path # @note Removes `_presenter` from the resulting string if present # @return [String] Path for the subclassed presenter def class_path self.class.name.underscore.gsub('_presenter', '').gsub('/presenter', '') end # Generates the default partial path when a template is not found or specified # @param partial [String] full path to the template's partial # @returns [String] full path to the specified template or the default template def default_path(partial=nil) return unless partial prefix = partial.to_s.split('/')[0..-2].join('/') [prefix, default_template].compact.join('/') end # The default name for the template when specified template can't be found def default_template 'default' end end |
Class Method Details
.presents(name) ⇒ Object (private)
Defines a method to access the presented entity
56 57 58 |
# File 'lib/lokap/presenter.rb', line 56 def self.presents(name) define_method(name) { @entity } end |
Instance Method Details
#class_path ⇒ String (private)
Removes _presenter
from the resulting string if present
Generates the name for the class segment of the path
87 88 89 |
# File 'lib/lokap/presenter.rb', line 87 def class_path self.class.name.underscore.gsub('_presenter', '').gsub('/presenter', '') end |
#default_path(partial = nil) ⇒ Object (private)
Generates the default partial path when a template is not found or specified
94 95 96 97 98 |
# File 'lib/lokap/presenter.rb', line 94 def default_path(partial=nil) return unless partial prefix = partial.to_s.split('/')[0..-2].join('/') [prefix, default_template].compact.join('/') end |
#default_template ⇒ Object (private)
The default name for the template when specified template can't be found
101 102 103 |
# File 'lib/lokap/presenter.rb', line 101 def default_template 'default' end |
#locals ⇒ Object (private)
Makes the presenter available to it's templates via p.<method>
or
presenter.<method>
62 63 64 |
# File 'lib/lokap/presenter.rb', line 62 def locals { presenter: self, p: self }.merge() end |
#partial_options(partial = nil) ⇒ Hash (private)
Generate Hash options to pass to the renderer
69 70 71 72 73 74 |
# File 'lib/lokap/presenter.rb', line 69 def (partial=nil) { partial: template_path(partial), locals: locals } end |
#render(partial = nil) ⇒ String
Renders the specified template for a presenter
46 47 48 49 50 |
# File 'lib/lokap/presenter.rb', line 46 def render(partial=nil) super (partial) rescue Lokap::ActionView::MissingTemplate super (default_path(partial)) end |
#template_path(partial = nil) ⇒ String (private)
The full path of the template to be rendered
79 80 81 82 |
# File 'lib/lokap/presenter.rb', line 79 def template_path(partial=nil) template = partial || default_template "#{class_path}/#{template}".downcase end |