Created
March 3, 2011 14:35
-
-
Save rbxbx/852851 to your computer and use it in GitHub Desktop.
CRUD with decent_exposure
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
# 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