doqq6g
Last Updated: February 25, 2016
·
804
· felipeelias
F6faa607e4f541da71fbf8aea6f99a7a

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

Say Thanks
Respond
Awesome Job

E20ce6a2 ca1f 11e7 82be 7f617c8310c9
SENIOR DEVELOPER (m/w)
·
Berlin, Köln, Hamburg, München
·
Full Time