Created
July 23, 2008 17:57
-
-
Save briandoll/1833 to your computer and use it in GitHub Desktop.
marcel's rest/crud conventions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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