public
Description: A Rails plugin to simplify the fetching and memoization of records for parameter-based finds.
Homepage:
Clone URL: git://github.com/mbleigh/fetches.git
mbleigh (author)
Tue Jul 29 07:35:05 -0700 2008
commit  2538ef18936047294b7d22543fa05d6c1c03934f
tree    a134e7ec8ea8be4a6c6c198eee3a4b03c3abb612
parent  fe18ae0f5c13db4b6a3672c1af90efd54b08b4aa
README
= Fetches

Fetches is a simple extension to ActionController that allows you to DRY up your
model fetching with a simple, intuitive syntax. Rather than creating helpers or
manually finding records each time an action is processed, simply add a call to
'fetches' in your controller and it will take care of the rest.

== Installation

Fetches is available as a gem as well as in traditional plugin format. To install
as a gem, add this to your environment.rb:

  config.gem 'mbleigh-fetches', :source => 'http://gems.github.com', :lib => "fetches"
  
To install it as a traditional plugin:

  script/plugin install git://github.com/mbleigh/fetches.git

== Example

  class UsersController < ApplicationController
    fetches :user
    
    def show
      user # equivalent to a memoizing call to User.find(params[:id])
    end
  end
  
It's very simple, and can also be extended to meet more complex demands, such as:
  
  # routes as a nested /users/:user_id/articles
  class ArticlesController < ApplicationController
    fetches :user, :as => :author, :from => :user_id, :using => :find_by_login
    fetches :article
    
    def index
      author # equivalent to User.find_by_login(params[:user_id])
    end
  end
  
You may also pass a Proc into the "from" option in order to fetch from
a more complex set of requirements:

  class UsersController < ApplicationController
    fetches :user, :from => Proc.new{ |c| c.params[:user_id] || c.params[:id] }
  end
  
Finally, you can use the :initialize option to initialize a new record using
parameters or Proc-based information:

  fetches :user, :initialize => true
  fetches :user, :initialize => :author # initialize from params[:author]
  fetches :user, :initialize => Proc.new{ |c| {:login => params[:login], :email => params[:email]} }

The helper method generated memoizes (initializes once then doesn't make additional
calls to the database) and is available both in the controller and the view.

Copyright (c) 2008 Michael Bleigh and Intridea, Inc., released under the MIT license