public
Description: Rails Plugin - a RailsEngines-based CMS extension for any Rails project
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/simple_pages.git
studioda (author)
Tue Feb 20 13:13:18 -0800 2007
commit  1aa1cfb1b026aebd5fe58b59809a0643ddc1a23c
tree    b52a4cc92cace5ccd1de37c90253447b67e4d71c
simple_pages / app / controllers / simple_pages_controller.rb
100644 70 lines (55 sloc) 1.795 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class SimplePagesController < ApplicationController
 
  before_filter :admin_required, :only => [:edit,
                                           :create,
                                           :update,
                                           :destroy]
  before_filter :find_or_initialize, :except => :index
  before_filter :set_title
  
  def index
    @pages = Page.find(:all)
  end
  
  # render show
  
  def edit
    @page.revert_to(params[:version]) if params[:version] && @page.respond_to(:revert_to)
  end
  
  def create
    @page.update_attributes!(params[:page])
    flash[:success] = "Page successfully created"
    redirect_to :action => 'show', :id => @page
  end
  
  def update
    @page.update_attributes!(params[:page])
    flash[:success] = "Page successfully modified"
    redirect_to :action => 'show', :id => @page
  end
  
  def destroy
    @page.destroy
    flash[:success] = "Page successfully deleted"
    redirect_to :action => 'index'
  end
  
  protected
  
    def find_or_initialize
      @page = params[:id] ? Page.find_by_filename(params[:id]) : Page.new
    end
    
    def set_title
      @title = @page.title if @page.respond_to(:title)
    end
    
    def rescue_action(exception)
      case exception.class.name
      when 'ActiveRecord::RecordInvalid'
        render_invalid_record(exception.record)
      when 'ActionController::RoutingError',
           'ActionController::UnknownAction',
           'ActiveRecord::RecordNotFound'
        render_404
      else
        super
      end
    end
 
    def render_invalid_record(record)
      render :action => (record.new_record? ? 'new' : 'edit')
    end
    
    def render_404
      render :text => "<h1>Whoops! That's a bad link.</h1>", :layout => true, :status => "404 Not Found"
    end
 
end