Class: Kadmin::Presenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/kadmin/presenter.rb,
lib/kadmin/presenter/test_case.rb

Overview

Base presenter class

Defined Under Namespace

Classes: NoViewContext, TestCase

Instance Method Summary collapse

Constructor Details

#initialize(object, view: nil) ⇒ Presenter

Returns a new instance of Presenter.

Parameters:

  • object (Object)

    the object to present

  • view (ActiveView::Base) (defaults to: nil)

    the view to present in; can be provided later on



6
7
8
9
# File 'lib/kadmin/presenter.rb', line 6

def initialize(object, view: nil)
  super(object)
  @view = view
end

Instance Method Details

#present(view) ⇒ self

Updates the context of the presenter with the given view This is mostly to provide a consistent interface between Presentable and Presenter, so you don’t have to check if you should present something or not.

Parameters:

  • view (ActiveView::Base)

    render in a different view

Returns:

  • (self)

    returns itself, as it is already presented



39
40
41
42
# File 'lib/kadmin/presenter.rb', line 39

def present(view)
  @view = view
  return self
end

#render(view: nil, **options, &block) ⇒ Object

Renders the wrapped object into the given view

Parameters:

  • view (ActiveView::Base) (defaults to: nil)

    optionally render in a different view

  • options (Hash)

    additional options passed to the render method

  • block (Proc)

    optional block to render additional stuff within the template

Returns:

  • (Object)

    rendered representation of the wrapped object, typically a string



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kadmin/presenter.rb', line 16

def render(view: nil, **options, &block)
  previous_view = @view
  rendered = nil
  captured = ''

  begin
    @view = view unless view.nil?
    raise Kadmin::Presenter::NoViewContext if @view.nil?
    captured = capture(&block) if block_given?
    rendered = generate(captured, **options)
  ensure
    @view = previous_view
  end

  return rendered
end