Make Resourceful plugin
Plugin details
Documentation
ruby script/plugin install http://svn.hamptoncatlin.com/make_resourceful/trunk
The easiest way to start with make_resourceful is to run the resource_scaffold generator. It uses the same syntax as the Rails scaffold_resource generator:
$ script/generate resource_scaffold post title:string body:text
If you want to try make_resourceful on one of your current controllers, just replace the mess of repetition with this:
class FooController < ApplicationController make_resourceful do actions :all end end
Those three lines will replace the entire default controller that comes out of the scaffold_resource generator.
=== Can I do nested resources?
make_resourceful do actions :all belongs_to :post end
=== What if I want to use fancy permalinks?
def current_object @current_object ||= current_model.find_by_permalink(params[:id]) end
=== What about paging?
def current_objects @current_object ||= current_model.find(:all, :order => "created_at DESC", :page => {:current => params[:page], :size => 10 } ) end
=== What if I want to do something in the middle of an action?
before :show, :index do @page_title = "Awesome!" end after :create_fails do @page_title = "Not So Awesome!" end
=== What about all of my awesome respond_to blocks for my XML APIs and RJS responses?
response_for :show do |format| format.html format.js format.xml end response_for :update_fails do |format| format.html { render :action => 'edit' } format.json { render :json => false.to_json, :status => 422 } end
=== So I guess I have to write responses for all my actions?
Nope! make_resourceful makes them do the right thing by default. You only need to customize them if you want to do something special.
=== make_resourceful the Method
The make_resourceful block is where most of the action happens. Here you specify which actions you want to auto-generate, what code you want to run for given callbacks, and so forth.
You also use the block to declare various bits of information about your controller. For instance, if the controller is nested, you'd call belongs_to. If you wanted to expose your models as some sort of text format, you'd call publish.
Check out the documentation of Resourceful::Builder for more information on the methods you can call here.
=== Helper Methods
make_resourceful provides lots of useful methods that can be used in your callbacks and in your views. They range from accessing the records you're looking up to easily generating URLs for a record to getting information about the action itself.
Two of the most useful methods are +current_object+ and +current_objects+ (note the subtle plurality difference). +current_objects+ only works for +index+, and returns all the records in the current model. current_object works for all actions other than index, and returns the record that's currently being dealt with.
The full documentation of the helper methods is in Resourceful::Default::Accessors and Resourceful::Default::URLs.
=== Overriding Methods
Not only are helper methods useful to the developer to use, they're used internally by the actions created by make_resourceful. Thus one of the main ways make_resourceful can be customized is by overriding accessors.
For instance, if you want to only look up the 10 most recent records for +index+, you're override +current_objects+. If you wanted to use a different model than that suggested by the name of the controller, you'd override +current_model+.
When you're overriding methods that do SQL lookups, though, be a little cautious. By default, these methods cache their values in instance variables so that multiple SQL queries aren't run on multiple calls. When overriding them, it's wise for you to do the same.
For instance,
def current_object @current_object ||= current_model.find_by_name(params[:name]) end
Further Documentation
There is currently no advanced documentation for this plugin.
New documentationEdit plugin | (0 older versions) | Last edited by: Guest, 8 months ago

