Class: Lokap::Presenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/lokap/presenter.rb

Overview

Lokap::Presenter is the base class for which all presenters inherit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, context, options = {}) ⇒ Lokap::Presenter

Creates a new Lokap::Presenter object

Examples:

present(@person, :share).render
present(@person, :share).render(:twitter)
present(@person, :share).render('social/twitter')

Parameters:

  • entity (Object)

    Object/entity to present

  • context (ActionView::Context)

    Rails view context for rendering

  • options (Hash) (defaults to: {})

    Options passed to the presenter and template

See Also:



35
36
37
38
39
40
41
# File 'lib/lokap/presenter.rb', line 35

def initialize(entity, context, options={})
  @entity  = entity
  @context = context
  @options = options

  super(@context)
end

Instance Attribute Details

#contextActionView

Returns Rails view context.

Returns:



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, options={})
    @entity  = entity
    @context = context
    @options = 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_options(partial)
  rescue Lokap::ActionView::MissingTemplate
    super partial_options(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(options)
  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_options(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

#optionsHash

Returns Options passed to the presenter and the template.

Returns:

  • (Hash)

    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, options={})
    @entity  = entity
    @context = context
    @options = 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_options(partial)
  rescue Lokap::ActionView::MissingTemplate
    super partial_options(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(options)
  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_options(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

Parameters:

  • name (String|Symbol)

    Name of the entity



56
57
58
# File 'lib/lokap/presenter.rb', line 56

def self.presents(name)
  define_method(name) { @entity }
end

Instance Method Details

#class_pathString (private)

Note:

Removes _presenter from the resulting string if present

Generates the name for the class segment of the path

Returns:

  • (String)

    Path for the subclassed presenter



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

Parameters:

  • partial (String) (defaults to: nil)

    full path to the template's partial



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_templateObject (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

#localsObject (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(options)
end

#partial_options(partial = nil) ⇒ Hash (private)

Generate Hash options to pass to the renderer

Parameters:

  • partial (String) (defaults to: nil)

    path to the template's partial

Returns:

  • (Hash)

    These options are passed to render :partial, <options>



69
70
71
72
73
74
# File 'lib/lokap/presenter.rb', line 69

def partial_options(partial=nil)
  {
    partial: template_path(partial),
    locals:  locals
  }
end

#render(partial = nil) ⇒ String

Renders the specified template for a presenter

Parameters:

  • partial (String|Symbol) (defaults to: nil)

    The name of the partial in app/views/presenters

Returns:

  • (String)

    HTML output



46
47
48
49
50
# File 'lib/lokap/presenter.rb', line 46

def render(partial=nil)
  super partial_options(partial)
rescue Lokap::ActionView::MissingTemplate
  super partial_options(default_path(partial))
end

#template_path(partial = nil) ⇒ String (private)

The full path of the template to be rendered

Parameters:

  • partial (String) (defaults to: nil)

    The name of the partial to render

Returns:

  • (String)

    Full path to the template's partial



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