File: app/controllers/admin/pages_controller.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Admin
  class: PagesController#1
inherits from
  ResourceController ( Admin )
has properties
method: index #21
method: new #26
method: preview #32
method: assign_page_attributes #39
method: model_class #46
method: render_preview #56
method: process_with_exception / 1 #73
method: count_deleted_pages #79
method: initialize_meta_rows_and_buttons #83
  class: PreviewStop#5
inherits from
  Rollback ( ActiveRecord )
has properties
method: message #6

Code

   1  class Admin::PagesController < Admin::ResourceController
   2    before_filter :initialize_meta_rows_and_buttons, :only => [:new, :edit, :create, :update]
   3    before_filter :count_deleted_pages, :only => [:destroy]
   4    
   5    class PreviewStop < ActiveRecord::Rollback
   6      def message
   7        'Changes not saved!'
   8      end
   9    end
  10 
  11    responses do |r|
  12      r.plural.js do
  13        @level = params[:level].to_i
  14        @template_name = 'index'
  15        self.models = Page.find(params[:page_id]).children.all
  16        response.headers['Content-Type'] = 'text/html;charset=utf-8'
  17        render :action => 'children.html.haml', :layout => false
  18      end
  19    end
  20 
  21    def index
  22      @homepage = Page.find_by_parent_id(nil)
  23      response_for :plural
  24    end
  25 
  26    def new
  27      @page = self.model = model_class.new_with_defaults(config)
  28      assign_page_attributes
  29      response_for :new
  30    end
  31    
  32    def preview
  33      render_preview
  34    rescue PreviewStop => exception
  35      render :text => exception.message unless @performed_render
  36    end
  37 
  38    private
  39      def assign_page_attributes
  40        if params[:page_id].blank?
  41          self.model.slug = '/'
  42        end
  43        self.model.parent_id = params[:page_id]
  44      end
  45 
  46      def model_class
  47        if Page.descendants.any? { |d| d.to_s == params[:page_class] }
  48          params[:page_class].constantize
  49        elsif params[:page_id]
  50          Page.find(params[:page_id]).children
  51        else
  52          Page
  53        end
  54      end
  55        
  56      def render_preview
  57        Page.transaction do
  58          page_class = Page.descendants.include?(model_class) ? model_class : Page
  59          if request.referer =~ %r{/admin/pages/(\d+)/edit}
  60            page = Page.find($1).becomes(page_class)
  61            page.update_attributes(params[:page])
  62            page.published_at ||= Time.now
  63          else
  64            page = page_class.new(params[:page])
  65            page.published_at = page.updated_at = page.created_at = Time.now
  66            page.parent = Page.find($1) if request.referer =~ %r{/admin/pages/(\d+)/children/new}
  67          end
  68          page.pagination_parameters = pagination_parameters
  69          process_with_exception(page)
  70        end
  71      end
  72      
  73      def process_with_exception(page)
  74        page.process(request, response)
  75        @performed_render = true
  76        raise PreviewStop
  77      end
  78 
  79      def count_deleted_pages
  80        @count = model.children.count + 1
  81      end
  82 
  83      def initialize_meta_rows_and_buttons
  84        @buttons_partials ||= []
  85        @meta ||= []
  86        @meta << {:field => "slug", :type => "text_field", :args => [{:class => 'textbox', :maxlength => 100}]}
  87        @meta << {:field => "breadcrumb", :type => "text_field", :args => [{:class => 'textbox', :maxlength => 160}]}
  88      end
  89  end