Skip to content

Instantly share code, notes, and snippets.

@jamesalmond
Created March 19, 2012 16:35
Show Gist options
  • Save jamesalmond/2118420 to your computer and use it in GitHub Desktop.
Save jamesalmond/2118420 to your computer and use it in GitHub Desktop.
CRUD controller
class NewController < SomeOtherController
include Cms::Controller
crud_for ArticleManager
add_rescue :create, SomeOtherError do |error|
redirect_to root_path
end
end
module Cms::Controller
def self.crud_for(manager)
@@model = manager
end
def new
@article = @@model.new_for(current_cms_site)
end
def create
@article = @@model.create_for!(current_cms_site, params[@@model.param_name])
rescue SaveError => e
@article = e.model
render :new
rescue StandardError => e
handle_error e
end
end
class ArticleManager
def self.new_for(parent)
parent.articles.new
end
def self.create_for!(parent, attributes)
article = parent.articles.new(attributes)
raise NotSaved, article unless article.save
article
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment