Created
March 19, 2012 16:35
-
-
Save jamesalmond/2118420 to your computer and use it in GitHub Desktop.
CRUD controller
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 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