Skip to content

Instantly share code, notes, and snippets.

@rbxbx
Created March 3, 2011 14:35
Show Gist options
  • Save rbxbx/852851 to your computer and use it in GitHub Desktop.
Save rbxbx/852851 to your computer and use it in GitHub Desktop.
CRUD with decent_exposure
# pulled out of a project authored by tpope && rbxbx
# not generic enough for general use, but a decent example of
# an application specific restful crud abstraction
module Incrudable
extend ActiveSupport::Concern
included do
expose(controller_name) { controller_name.classify.constantize.scoped }
expose(controller_name.singularize)
end
def resource
send(controller_name.singularize)
end
def create
flash[:notice] = "#{resource.class.model_name.human} was successfully created" if resource.save
respond_with resource, :location => (default_redirect || resource.admin_hierarchy(:edit))
end
def update
flash[:notice] = "#{resource.class.model_name.human} was successfully updated" if resource.save
respond_with resource, :location => (default_redirect || resource.admin_hierarchy(:edit))
end
def destroy
resource.destroy
flash[:notice] = "#{resource.class.model_name.human} has been deleted"
redirect_to default_redirect || [:admin] + resource.ancestors + [controller_name]
end
def default_redirect
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment