Skip to content

Instantly share code, notes, and snippets.

@briandoll
Created July 23, 2008 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandoll/1833 to your computer and use it in GitHub Desktop.
Save briandoll/1833 to your computer and use it in GitHub Desktop.
marcel's rest/crud conventions
# Marcel Molina's REST/CRUD conventions
# http://project.ioni.st/post/2283#snippet_2283
class ApplicationController < ActionController::Base
def self.set_resource(resource, &custom_setter)
filter_name = :"set_#{resource}"
before_filter filter_name, :only => [:show, :edit, :update, :destroy]
if custom_setter
define_method(filter_name) do
custom_setter.call(resource)
end
else
define_method(filter_name) do
instance_variable_set("@#{resource}", resource.to_s.classify.find(params[:id]) )
end
end
end
def self.default_formats_for(data)
respond_to do |format|
format.html
format.xml { render :xml => data }
end
end
end
class AccountsController < ApplicationController
set_resource :account
def index
@accounts = Account.all
default_formats_for @accounts
end
def show
default_formats_for @account
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment