class Resubject::Presenter
Attributes
the HTML helpers context
the HTML helpers context
Public Class Methods
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
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)
# File lib/resubject/presenter.rb, line 21 def initialize(model, context = nil) @context = context super(model) end
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
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
# File lib/resubject/presenter.rb, line 101 def helpers context end
# File lib/resubject/presenter.rb, line 95 def localize(*args, &block) context.l(*args, &block) end
# File lib/resubject/rails.rb, line 7 def routes ::Rails.application.routes.url_helpers end
# File lib/resubject/presenter.rb, line 89 def translate(*args, &block) context.t(*args, &block) end