Skip to content

Commit

Permalink
added create_root_page
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakjo committed Sep 22, 2009
1 parent 33d232e commit fd0d6b5
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/comatose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def self.load_extensions
end
end

def self.create_root_page(options ={})
unless ComatosePage.root
page = ComatosePage.create({:title=>'root page', :body=>"Welcome to comatose", :author=>'System'}.merge(options), :parent_id=>nil)
page.save(false)
else
raise "There exists a comatose root page, cant create another one"
end
end
end

require 'acts_as_versioned'
Expand Down
138 changes: 138 additions & 0 deletions lib/comatose_controller.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# The controller for serving cms content...
class ComatoseController < ActionController::Base
unloadable

before_filter :handle_authorization, :set_content_type
after_filter :cache_cms_page

# Render a specific page
def show
page_name, page_ext = get_page_path
page = ComatosePage.find_by_path( page_name )
status = nil
if page.nil?
page = ComatosePage.find_by_path( '404' )
status = 404
end
# if it's still nil, well, send a 404 status
if page.nil?
render :nothing=>true, :status=>status
#raise ActiveRecord::RecordNotFound.new("Comatose page not found ")
else
# Make the page access 'safe'
@page = Comatose::PageWrapper.new(page)
# For accurate uri creation, tell the page class which is the active mount point...
ComatosePage.active_mount_info = get_active_mount_point(params[:index])
render :text=>page.to_html({'params'=>params.stringify_keys}), :layout=>get_page_layout, :status=>status
end
end

protected

def handle_authorization
if Comatose.config.authorization.is_a? Proc
instance_eval &Comatose.config.authorization
elsif Comatose.config.authorization.is_a? Symbol
send(Comatose.config.authorization)
elsif defined? authorize
authorize
else
true
end
end

def allow_page_cache?
# You should have access to the @page being rendered
true
end

# For use in the #show method... determines the current mount point
def get_active_mount_point( index )
Comatose.mount_points.each do |path_info|
if path_info[:index] == index
return path_info
end
end
{:root=>"", :index=>index}
end

# For use in the #show method... determines the current page path
def get_page_path

#in rails 2.0, params[:page] comes back as just an Array, so to_s doesn't do join('/')
if params[:page].is_a? Array
page_name = params[:page].join("/")
#in rails 1.x, params[:page] comes back as ActionController::Routing::PathSegment::Result
elsif params[:page].is_a? ActionController::Routing::PathSegment::Result
page_name = params[:page].to_s
else
logger.debug "get_page_path - params[:page] is an unrecognized type, may cause problems: #{params[:page].class}"
page_name = params[:page].to_s
end

page_ext = page_name.split('.')[1] unless page_name.empty?
# TODO: Automatic support for page RSS feeds... ????
if page_name.nil? or page_name.empty?
page_name = params[:index]
params[:cache_path] = "#{request.request_uri}/index"
elsif !params[:index].empty?
page_name = "#{params[:index]}/#{page_name}"
end
return page_name, page_ext
end

# Returns a path to plugin layout, if it's unspecified, otherwise
# a path to an application layout...
def get_page_layout
params[:layout]
end

# An after_filter implementing page caching if it's enabled, globally,
# and is allowed by #allow_page_cache?
def cache_cms_page
unless Comatose.config.disable_caching or response.headers['Status'] == '404 Not Found'
return unless params[:use_cache].to_s == 'true' and allow_page_cache?
path = params[:cache_path] || request.request_uri
begin
# TODO: Don't cache pages rendering '404' content...
self.class.cache_page( response.body, path )
rescue
logger.error "Comatose CMS Page Cache Exception: #{$!}"
end
end
end

# An after_filter that sets the HTTP header for Content-Type to
# what's defined in Comatose.config.content_type. Defaults to utf-8.
def set_content_type
response.headers["Content-Type"] = "text/html; charset=#{Comatose.config.content_type}" unless Comatose.config.content_type.nil? or response.headers['Status'] == '404 Not Found'
end

COMATOSE_VIEW_PATH = File.join(RAILS_ROOT, 'vendor', 'plugins', 'comatose', 'views')
ActionController::Base.append_view_path(COMATOSE_VIEW_PATH) unless ActionController::Base.view_paths.include?(COMATOSE_VIEW_PATH)

# Include any, well, includes...
Comatose.config.includes.each do |mod|
mod_klass = if mod.is_a? String
mod.constantize
elsif mod.is_a? Symbol
mod.to_s.classify.constantize
else
mod
end
include mod_klass
end

# Include any helpers...
Comatose.config.helpers.each do |mod|
mod_klass = if mod.is_a? String
mod.constantize
elsif mod.is_a? Symbol
mod.to_s.classify.constantize
else
mod
end
helper mod_klass
end
end

1 change: 0 additions & 1 deletion test/functional/comatose_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ def test_truth
assert_response :success
assert_tag :tag=>'title', :child=>/Question/
end

end
4 changes: 3 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ENV["RAILS_ENV"] = 'test'

require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
require File.expand_path(File.join(File.dirname(__FILE__), "/../../comatose_harness/config/environment.rb"))

require 'test/unit'
require 'test_help'
Expand Down Expand Up @@ -42,6 +42,8 @@ def assert_no_difference(object, method, &block)
assert_difference object, method, 0, &block
end



class << self
def should(behave,&block)
method_name = "test_should_#{behave.gsub(' ', '_')}"
Expand Down
11 changes: 11 additions & 0 deletions test/unit/comatose_root_page_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require File.dirname(__FILE__) + '/../test_helper'

class ComatoseRootPageTest < Test::Unit::TestCase

should "create root page" do
Comatose.create_root_page

assert_equal 1, Comatose.find(:all).length
end
end

0 comments on commit fd0d6b5

Please sign in to comment.