Created
March 15, 2013 14:58
-
-
Save hmans/5170438 to your computer and use it in GitHub Desktop.
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
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