Created
October 29, 2009 16:47
-
-
Save boof/221588 to your computer and use it in GitHub Desktop.
Make controller code more readable...
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 PagesController < Protected::Base | |
before_filter :authorized?, :except => [:index, :show] | |
before_filter :assign_searcher, :except => :index | |
before_filter :assign_navigation, :except => [:show, :new, :edit] | |
def index | |
@pages = Page.find :all | |
end | |
def show id = fetch(:id), version = fetch(:version, 'HEAD'), | |
page = find_and_set(id, :version => version) | |
@page = page | |
return :show | |
end | |
def publish id = fetch(:id), version = fetch(:version, 'HEAD'), | |
page = find_and_set(id, :version => version) | |
if page.publish | |
redirect_to protected_page_path(page) | |
else | |
render show(id, version, page) | |
end | |
end | |
def new attributes = fetch(:page, {}), | |
page = Page.new(attributes) | |
@page = page | |
return :new | |
end | |
def create attributes = fetch(:page), | |
page = Page.new(attributes) | |
# TODO: submit location in tree | |
if page.save | |
navigation.insert page | |
navigation.write visitor, "updated link to #{ page.href }" | |
redirect_to protected_page_path(page) | |
else | |
render new(attributes, page) | |
end | |
end | |
def edit id = fetch(:id), | |
page = find_and_set(id) | |
@page = page | |
return :edit | |
end | |
def update id = fetch(:id), attributes = fetch(:page), | |
page = find_and_set(id, attributes) | |
if page.save | |
navigation.update page | |
navigation.write visitor, "updated link to #{ page.href }" | |
redirect_to protected_page_path(id) | |
else | |
render edit(id, page) | |
end | |
end | |
def destroy id = fetch(:id), | |
page = find_and_set(id) | |
page.destroy | |
navigation.delete page | |
navigation.write visitor, "removed link to #{ page.href }" | |
redirect_to protected_page_path(id) | |
end | |
protected | |
def fetch(key, *default) | |
param.fetch key.to_s, *default | |
end | |
def find_and_set(id, attributes = nil) | |
page = Page.find id | |
page.attributes = attributes if attributes | |
block_given?? yield(page) : page | |
end | |
def assign_navigation | |
@navigation = Navigation.local | |
end | |
attr_reader :navigation | |
def authorized? | |
visitor.is_a? 'Author' | |
end | |
def assign_searcher | |
raise NotImplementedError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment