Embedded Actions plugin

Plugin details

A way to invoke an action from a view and insert the results in place, including the possibility of caching the results of the embedded action.

Websitehttp://www.notsostupid.com/blog/2007/10/23/the-embedded-actions-plugin-for-rails/ Repositoryhttp://dev.notso.net/svn/rails/plugins/embedded_actions/current/ Author Sebastian Delmont Tags embedded, Cache, fragment, reuse, views, render, partial LicenseBSD

Documentation

Install the plugin:
ruby script/plugin install http://dev.notso.net/svn/rails/plugins/embedded_actions/current/

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 SongsController < ApplicationController

  caches_embedded :top10

  def top10
    @top10 = Songs.find(:all, :order => "times_played", :limit => 10)

    respond_to do |format|
      format.embedded { render :layout => false }  # embedded version uses no layout
      format.all      { render }
    end
  end
end



Cached 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.

Caching is optional, and so is respond_to. Just remember to set your layout to
false when rendering for embedded use (will be automatic in a future release).

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | Back in time (4 older versions) | Last edited by: sdelmont, 6 months ago