Module: Cadmus::PagesController
- Extended by:
- ActiveSupport::Concern
- Includes:
- Concerns::ControllerWithParent, Renderable
- Defined in:
- lib/cadmus/pages_controller.rb
Overview
A controller mixin that includes all the RESTful resource actions for viewing and administering Cadmus pages. This mixin provides a great deal of flexibility for customizing the behavior of the resulting controller.
Controllers that include this mixin are expected to define at least a page_class method, which returns the class to be used for pages in this context.
== Examples
A basic PagesController for a site:
class PagesController < ApplicationController include Cadmus::PagesController
# no action methods are necessary because PagesController defines them for us
protected
def page_class
Page
end
# Page must be defined as a model that includes Cadmus::Page
end
A PagesController using CanCan for authorization control:
class PagesController < ApplicationController include Cadmus::PagesController
# no need for load_resource because PagesController does that for us
:page
protected
def page_class
Page
end
end
A controller for pages inside a Book object. This controller uses URLs of the form +/books/:book_id/pages/...+ The book_id parameter is a slug, not an ID. First, here's the Book model:
class Book < ActiveRecord::Base # This association has to be called "pages" because that's what BookPagesController # will expect to use to find them. has_many :pages, :class_name => "BookPage"
validates_uniqueness_of :slug
end
class BookPagesController < ApplicationController include Cadmus::PagesController
self.page_parent_class = Book # pages must have a Book as their parent
self.page_parent_name = "book" # use params[:book_id] for finding Books
self.find_parent_by = "slug" # use the Book's slug field for finding Books
protected
def page_class
BookPage
end
end
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #liquid_registers ⇒ Object protected
- #load_parent_and_page ⇒ Object protected
- #new ⇒ Object
- #page_class ⇒ Object protected
- #page_params ⇒ Object protected
-
#page_scope ⇒ Object
protected
Returns the ActiveRecord::Relation that will be used for finding pages.
- #show ⇒ Object
- #update ⇒ Object
Methods included from Concerns::ControllerWithParent
#find_parent_by, #liquid_file_system, #parent_model, #parent_model_class, #parent_model_name
Methods included from Renderable
#cadmus_renderer, #cadmus_renderer_class, #setup_renderer
Instance Method Details
#create ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/cadmus/pages_controller.rb', line 107 def create @page = page_scope.new(page_params) respond_to do |format| if @page.save dest = { :action => 'show', :page_glob => @page.slug } format.html { redirect_to(dest, :notice => 'Page was successfully created.') } format.xml { render :xml => @page, :status => :created, :location => dest } format.json { render :json => @page, :status => :created, :location => dest } else format.html { render 'cadmus/pages/new' } format.xml { render :xml => @page.errors, :status => :unprocessable_entity } format.json { render :json => @page.errors, :status => :unprocessable_entity } end end end |
#destroy ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/cadmus/pages_controller.rb', line 139 def destroy @page.destroy respond_to do |format| format.html { redirect_to(:action => :index) } format.xml { head :ok } format.json { head :ok } end end |
#edit ⇒ Object
103 104 105 |
# File 'lib/cadmus/pages_controller.rb', line 103 def edit render 'cadmus/pages/edit' end |
#index ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/cadmus/pages_controller.rb', line 75 def index @pages = page_scope.order(:name).all respond_to do |format| format.html { render 'cadmus/pages/index' } format.xml { render :xml => @pages } format.json { render :json => @pages } end end |
#liquid_registers ⇒ Object (protected)
173 174 175 176 177 178 179 180 181 |
# File 'lib/cadmus/pages_controller.rb', line 173 def liquid_registers registers = { 'parent' => parent_model, :file_system => liquid_file_system } if defined?(super) registers.merge(super) else registers end end |
#load_parent_and_page ⇒ Object (protected)
166 167 168 169 170 171 |
# File 'lib/cadmus/pages_controller.rb', line 166 def load_parent_and_page if params[:page_glob] @page = page_scope.find_by_slug(params[:page_glob]) raise ActiveRecord::RecordNotFound.new("No page called #{params[:page_glob]}") unless @page end end |
#new ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/cadmus/pages_controller.rb', line 93 def new @page = page_scope.new respond_to do |format| format.html { render 'cadmus/pages/new' } format.xml { render :xml => @page } format.json { render :json => @page } end end |
#page_class ⇒ Object (protected)
158 159 160 |
# File 'lib/cadmus/pages_controller.rb', line 158 def page_class Cadmus.page_model end |
#page_params ⇒ Object (protected)
162 163 164 |
# File 'lib/cadmus/pages_controller.rb', line 162 def page_params params.require(:page).permit(:name, :slug, :content) end |
#page_scope ⇒ Object (protected)
Returns the ActiveRecord::Relation that will be used for finding pages. If there is a page parent for this request, this will be the "pages" scope defined by the parent object. If there isn't, this will be the "global" scope of the page class (i.e. pages with no parent object).
154 155 156 |
# File 'lib/cadmus/pages_controller.rb', line 154 def page_scope @page_scope ||= parent_model ? parent_model.pages : page_class.global end |
#show ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/cadmus/pages_controller.rb', line 85 def show respond_to do |format| format.html { render 'cadmus/pages/show' } format.xml { render :xml => @page } format.json { render :json => @page } end end |
#update ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/cadmus/pages_controller.rb', line 124 def update respond_to do |format| if @page.update_attributes(page_params) dest = { :action => 'show', :page_glob => @page.slug } format.html { redirect_to(dest, :notice => 'Page was successfully updated.') } format.xml { head :ok } format.json { head :ok } else format.html { render 'cadmus/pages/edit' } format.xml { render :xml => @page.errors, :status => :unprocessable_entity } format.json { render :json => @page.errors, :status => :unprocessable_entity } end end end |