Skip to content

Instantly share code, notes, and snippets.

@hmans
Created March 15, 2013 14:58
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 hmans/5170438 to your computer and use it in GitHub Desktop.
Save hmans/5170438 to your computer and use it in GitHub Desktop.
class ResourceController < ApplicationController
helper_method :resource, :collection
before_filter do
params[resource_name.to_sym] &&= resource_params
end
def index(&blk)
respond_with *(resource_parents + [collection]), &blk
end
def show(&blk)
respond_with *(resource_parents + [resource]), &blk
end
def edit(&blk)
respond_with *(resource_parents + [resource]), &blk
end
def update(&blk)
resource.update_attributes(resource_params)
respond_with *(resource_parents + [resource]), &blk
end
def new(&blk)
respond_with *(resource_parents + [resource]), &blk
end
def create(&blk)
resource.save
respond_with *(resource_parents + [resource]), &blk
end
def destroy(&blk)
resource.destroy
respond_with(*(resource_parents + [resource])) do |format|
yield(format) if block_given?
format.html { redirect_to(resource.class) }
end
end
protected
def resource_parents
[]
end
def resource
instance_variable_get "@#{resource_name}"
end
def collection
instance_variable_get "@#{resource_name.pluralize}"
end
def resource_name
self.class.name.gsub(/Controller$/, '').underscore.singularize
end
def resource_params
params[resource_name]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment