Skip to content

Instantly share code, notes, and snippets.

@chatgris
Forked from rbxbx/incrudable.rb
Created July 30, 2011 09:45
Show Gist options
  • Save chatgris/1115368 to your computer and use it in GitHub Desktop.
Save chatgris/1115368 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