Sha256: 4baa3ffc28de830cc6b4c3f30850a5e4f21b6544f5e42a5646fa2ff078ef4ee3
Contents?: true
Size: 1.99 KB
Versions: 1
Compression:
Stored size: 1.99 KB
Contents
= 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
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mbleigh-fetches-0.0.4 | README |