The Embedded Actions plugin for Rails
This is an extraction of some things we’ve been using at StreetEasy for about two years now. Here’s the README:
Just like the traditional render :partial, embedded actions allow you to
refactor your views and extract presentation logic and templates into separate
files.Unlike partials, embedded actions also let you define business logic to be
performed before the partial is included. That logic is encapsulated in the
already well understood metaphor of an action inside a controller.So a simple call like
<%= embed_action :controller => “songs”, :action => “top10″ %>
lets you include an html fragment containing the top 10 songs into any of your
pages, regardless of which controller, action or view wants to do the including.Additionally, embedded actions can provide caching of their results (allowing
for different parameters) just like page caching, but at the level of html
fragments. So your dynamic pages can still be rendered dynamically, but some of
the embedded actions can be cached (and expired) independently.Just declare an action as ‘cacheable’ in a way similar to page caching,
by invoking “caches_embedded” with the name of the action to cache.class TestController < ApplicationController
caches_embedded :user_listdef user_list
...
end
endCached fragments can be invalidated with calls to expires_embedded, but you must
remember to use the same set of parameters used to embed the cached action in
the first place.
The code is available at
http://dev.notso.net/svn/rails/plugins/embedded_actions/
and can be easily installed by running
script/plugin install http://dev.notso.net/svn/rails/plugins/embedded_actions/current
.
Enjoy, and please comment.
UPDATE: Version 1.1 has been released.
October 25th, 2007 at 3:45 am
So a simple call like ??? The code is swallowed by rails it seems…
Looks nice and interesting.
Herman
November 12th, 2007 at 6:18 pm
Love this plugin, it helps you keep DRY in the application.
There is some features that you didn’t show in the main post, that I may consider important… you can pass parameters to the embedded actions with the :params key
for example supuse that we want to embed an action, but this actions requires a user_id in the params, we could easily do this in the invocation of embed_action:
embed_action :controller => ‘post’, :action => ‘top_10′, :params => { :user_id => 10 }
or we could pass even the same params hash and it would take it
embed_action :controller => ‘post’, :action => ‘top_10′, :params => params
Could notice that you cant put even ruby objects and it takes it to the controller too
embed_action :controller => ‘post’, :action => ‘top_10′, :params => { :user => current_user }
This plugin is very cool indeed.
November 19th, 2007 at 6:22 am
What’s the difference between this and Rails components?
November 28th, 2007 at 6:46 pm
We have been using this for awhile too, on something not quite live yet… great work Sebastian, and thanks for the plugin!
December 4th, 2007 at 11:10 am
[…] just released an update to my Embedded Actions plugin for […]
December 12th, 2007 at 6:46 am
This thing needs to get into the Rails core. Its absurd that they let components suck so much until ripping it out without offering a replacement. People who think using before_filters and partials are a replacement for a real component structure are on crack.
Thanks for the plugin!