This gem includes a base class,
DelegatePresenter::Base which all your presenter classes should inherit from.
So, creating a new presenter class:
class TodoPresenter < DelegatePresenter::Base def todo_name s("<br>", self.description) end end
You mean that little s hack? That's a way to keep yourself from going sane having to
This will look up the name of
object's class, then look for a class named (that name) presenter.
So, if object is a Todo instance, DelegatePresenter will look for
TodoPresenter. It will then instantiate an object of that class (
TodoPresenter), passing the parameter from the Present call into the constructor.
DelegatePresenter::Base subclasses are just
SimpleDelegators at heart, this means methods that
TodoPresenter does not know about will be passed on to (delegated to) the
DelegatePresenter does three things:
Inherits from Ruby Standard Library's SimpleDelegator. This simple class solves many of the problems I've seen with presenter APIs in the past. (calling object.method everywhere)
Makes Rails helpers available to you via the
helpersmethod, gives you
h(your old Rails 2 friend).
Gives you a
record_idmethod, which will return the ActiveRecord ID of the database object. Because I think it should have been this way in the first place :)
- Add me to your Gemfile
- In your ApplicationController
- In your
config/application.rb, add an autoload path for
- Create presenters for your classes. For example:
app/presenters/todo_presenter.rb. These classes should subclass
Most of this gem was inspired by (and the initial structure stolen from), Avdi Grimm's blog posts on Demeter and Nil objects and Falsiness. While these articles talk about other things, they give part of a very simple Presenter strategy.
The simplest presenter strategy that could possibly work.
So I used it on a few projects, and extended the abilities were I had to. It's still very simple, doing only one or two things more than that original pattern. I just built on the shoulders of giants.
I know there are a few other presenter frameworks out there for Rails. One hasn't seen any activity since 2008, and one is Draper.
I'm not a big fan of Draper's API, and it seemed too heavy to me. If Draper's your style, that's great: different strokes for different folks.
(The MIT License)
Copyright © 2011 Ryan Wilcox
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.