class Resubject::Presenter

Attributes

context[R]

the HTML helpers context

template[R]

the HTML helpers context

Public Class Methods

all(collection, context = nil) click to toggle source

Builds a collection of presenters given an array of objects

@param [Array<Object>] collection @param context the HTML helpers context @return [Array<Presenter>] instances of a presenter for each item in the collection

@example

boxes = [box1, box2, box3]
BoxPresenter.all boxes
# => [<BoxPresenter>, <BoxPresenter>, <BoxPresenter>]

PostPresenter.all Post.all
# => [<PostPresenter>, ...]
# File lib/resubject/presenter.rb, line 43
def self.all(collection, context = nil)
  collection.map { |c| new(c, context) }
end
new(model, context = nil) click to toggle source

Create a new presenter

@param model any object that can be presented @param context a context of HTML helpers

@example

PostPresenter.new(post)
PostPresenter.new(post, view_context)
Calls superclass method
# File lib/resubject/presenter.rb, line 21
def initialize(model, context = nil)
  @context = context
  super(model)
end
presents(attribute, *presenters) click to toggle source

Generates a instance method with the attribute presented

@example

class BoxPresenter < Resubject::Presenter
  presents :name
  # or presents :name, CustomPresenter
end

BoxPresenter.new(box).name   # => <NamePresenter>

@example When the parent method is nil, it returns nil

box = BoxPresenter.new Box.new(name: nil)
box.name
# => nil
# File lib/resubject/presenter.rb, line 81
def self.presents(attribute, *presenters)
  define_method attribute do
    present to_model.send(attribute), *presenters
  end
end

Public Instance Methods

present(objects, *presenters) click to toggle source

Creates a presenter from object or collection of objects

@example

present box                          # => <BoxPresenter>
present [box, box]                   # => [<BoxPresenter>, <BoxPresenter>]
present [box, box], CustomPresenter  # => <CustomPresenter>

@param [Object, Array<Object>] objects objects to be instantiated with related presenter @param [Presenter] presenters one or multiple presenters @return [Presenter, Array<Presenter>] either the presenter or a collection of presenter

@see Builder.present

# File lib/resubject/presenter.rb, line 60
def present(objects, *presenters)
  Builder.present objects, context, *presenters
end

Private Instance Methods

h()
Alias for: helpers
helpers() click to toggle source
# File lib/resubject/presenter.rb, line 101
def helpers
  context
end
Also aliased as: h
l(*args, &block)
Alias for: localize
localize(*args, &block) click to toggle source
# File lib/resubject/presenter.rb, line 95
def localize(*args, &block)
  context.l(*args, &block)
end
Also aliased as: l
r()
Alias for: routes
routes() click to toggle source
# File lib/resubject/rails.rb, line 7
def routes
  ::Rails.application.routes.url_helpers
end
Also aliased as: r
t(*args, &block)
Alias for: translate
translate(*args, &block) click to toggle source
# File lib/resubject/presenter.rb, line 89
def translate(*args, &block)
  context.t(*args, &block)
end
Also aliased as: t