public
Clone URL: git://github.com/nmerouze/action_presenter.git
Nicolas Mérouze (author)
Mon May 19 00:48:49 -0700 2008
commit  f9582b5c4d15f97236698bf1c331840a0c637826
tree    aef2fbcd931f68400c73074172c2ab9a5078daba
parent  fd1def3f0b56db6bb8ce4d3c40f1819f704dbdae
name age message
file CHANGELOG Mon May 19 00:48:49 -0700 2008 Remove Engines dependency [Nicolas Mérouze]
file MIT-LICENSE Sun May 04 10:59:14 -0700 2008 Initial import [Nicolas Mérouze]
file README.rdoc Sun May 04 10:59:14 -0700 2008 Initial import [Nicolas Mérouze]
file init.rb Mon May 19 00:48:49 -0700 2008 Remove Engines dependency [Nicolas Mérouze]
directory lib/ Sun May 04 10:59:14 -0700 2008 Initial import [Nicolas Mérouze]
README.rdoc

Action Presenter

Inspired by : http://www.caboo.se/articles/2007/8/23/simple-presenters

Warning

Works only with Edge Rails.

Install

        ruby script/plugin install git://github.com/nmerouze/action_presenter.git

Usage

Here’s a presenter :

        class ArticlePresenter < ActionPresenter::Base
                def title
                        h @source.title
                end
        end

And to call it in the views, there are 2 helpers :

        <%= present(@article).title %> or <%= p(@article).title %>

Render files

You can also render files for a presenter method :

        class ArticlePresenter < ActionPresenter::Base
                def title
                        render :title
                end

                def body
                        render :body, :msg => 'Hello World!'
                end
        end

The partial for title will be located in app/presenters/article/_title.html.erb :

        <%=h article.title %>

The partial for body will be located in app/presenters/article/_body.html.erb :

        <%= article.body + msg %>

Presenters in your plugins

You can have presenters in plugins. Just create a directory app/presenters and you’re ready to go. It’s great if you have Engines too.

Contact

nicolas.merouze (at) gmail (dot) com