Sha256: e0734834671fc325ad89e363bf647739a503c41562a0f9074aca599f8d02b2c8

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

class PagesController < InheritedResources::Base
  respond_to :js, :html
  ajax_loading
  belongs_to :parent, :optional=>true, :collection_name => :children,
             :parent_class=>Page, :param=>:page_id, :instance_name=>:parent_page
  before_filter :set_collection_name

  def set_collection_name
    if params[:page_id]
      self.resources_configuration[:self][:collection_name] = :children
    end
  end


  before_filter :authenticate_user!, :except=>[:index, :show]
  before_filter :maybe_authenticate, :only=>[:index, :show]
  before_filter :resource, :only=>[:update, :show, :destroy, :edit]

  has_scope :tagged_with, :as => :tag
  has_scope :accessible_by, :type=>:boolean, :default=>true do |c,s|
    s.accessible_by(c.current_ability)
  end

  authorize_resource

  def resource
    page_id = params[:id]
    if page_id.is_numeric?
      @page ||= Page.find(page_id)
    else
      @page ||= Page.where(["lower(pages.title) = ?", page_id.downcase.gsub(/[_]/, ' ')]).first
    end
    @attachments ||= @page.attachments
    @page
  end

  def maybe_authenticate
    authenticate_user! if params[:a]
  end

  def show
    show! do |format|
      set_children
    end
  end

  def edit
    edit! do |format|
      set_children
      @attachment = Attachment.new(:page=>@page)
    end
  end

  def create
    create! { (@parent_page ? page_child_path(@parent_page, @page) : @page) }
  end

  def new
    new! do |format|
      set_children
    end
  end

  def update
    update! do |format|
      if params[:child_id]
        @parent_page = @page
        @page = Page.find(params[:child_id])
        @page.parent = @parent_page
        @page.save
      end
    end
  end

  def set_children
    if @parent_page
      @children = @parent_page.children
    else
      @children = @page.children
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wheels-0.0.25 app/controllers/pages_controller.rb
wheels-0.0.24 app/controllers/pages_controller.rb
wheels-0.0.23 app/controllers/pages_controller.rb