Module: Marilyne::Helper

Defined in:
lib/marilyne/helper.rb

Overview

Presenter Helper

Instance Method Summary collapse

Instance Method Details

#presenter_for(template, options = {}) ⇒ Object

Render a partial and build a new presenter object which is associate to the parial

Examples

presenter_for('base')
# => render partial: 'base', object: BasePresenter.new(Base.new)

You can specify the object you want present:

presenter_for('base', object: @object)
# => render partial: 'base', object: BasePresenter.new(@object)

or multiple object

presenter_for('base', objects: [@object, @other_object])
# => render partial: 'base', object: BasePresenter.new(@object)

A block can also be passed and the the render will be call only block is true

presenter_for('base', object: @object) { |o| o.ok? }

Options:

  • :object - Specifies the object you want present

  • :objects - You can pass multiple objects in your presenter

  • :presenter - Specifies the presenter you want use



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/marilyne/helper.rb', line 28

def presenter_for(template, options = {})
  presenter_string = options[:presenter] || "#{template.camelize}Presenter"

  object           = extract_objects template, options[:object], options[:objects]
  presenter        = presenter_string.to_s.constantize.new self, *object

  if block_given?
    return unless yield object
  end

  render partial: template, object: presenter
end