Last Updated: February 25, 2016
·
975
· felipeelias

Creating presenters with Resubject

Resubject is a (yet another) presenter gem based on Ruby's SimpleDelegator class. You don't need to explicitly expose any methods in your presenter, and only define those that require customization:

Example:

class Post < Struct.new(:title, :comments)
end

class PostPresenter < Resubject::Presenter
  # create comments method with 
  # instances of CommentPresenter
  presents :comments

  def title
    title.uppercase
  end
end

Then you can use:

PostPresenter.new(post).title
# => OMG!

PostPresenter.new(post).comments
# => [<CommentPresenter>, ...]

Rails

If you are using Rails, it automatically include some helpers for you:

class BoxesController < ActionController::Base
  def index
    @boxes = present Boxes.all
  end
end

And in your views:

<%= present(@box).contents %>

Check out the project and much more on github


endorse