adding a wrapper around page/layout loading from db/file

Oleg committed Nov 04, 2010
commit 1170953bee35e6fada4583bf147f484970e0d08b
Showing 8 changed files with 98 additions and 16 deletions
app/controllers/cms_content_controller.rb +5 -9
@@ @@ -16,7 +16,7 @@ class CmsContentController < ApplicationController
def render_js
render :text => @cms_layout.merged_js, :content_type => 'text/javascript'
end
-
+
protected
def load_cms_site
@@ @@ -26,15 +26,11 @@ protected
end
def load_cms_page
- # Attempting to load seed page
- if ComfortableMexicanSofa.configuration.seed_data_path
- @cms_page = CmsPage.load_from_file(@cms_site, "/#{params[:cms_path]}")
- end
-
- @cms_page ||= @cms_site.cms_pages.find_by_full_path!("/#{params[:cms_path]}")
+ @cms_page = CmsPage.load_for_full_path!(@cms_site, "/#{params[:cms_path]}")
return redirect_to(@cms_page.target_page.full_path) if @cms_page.target_page
+
rescue ActiveRecord::RecordNotFound
- if @cms_page = @cms_site.cms_pages.find_by_full_path('/404')
+ if @cms_page = CmsPage.load_for_full_path(@cms_site, '/404')
render_html(404)
else
render :text => 'Page Not Found', :status => 404
@@ @@ -42,7 +38,7 @@ protected
end
def load_cms_layout
- @cms_layout = @cms_site.cms_layouts.find(params[:id])
+ @cms_layout = CmsLayout.load_for_slug!(@cms_site, params[:id])
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => 404
end
app/models/cms_layout.rb +19 -2
@@ @@ -44,9 +44,9 @@ class CmsLayout < ActiveRecord::Base
end
# Attempting to initialize layout object from yaml file that is found in config.seed_data_path
- def self.load_from_file(site, name)
+ def self.load_from_file(site, slug)
return nil if ComfortableMexicanSofa.config.seed_data_path.blank?
- file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/layouts/#{name}.yml"
+ file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/layouts/#{slug}.yml"
return nil unless File.exists?(file_path)
attributes = YAML.load_file(file_path).symbolize_keys!
attributes[:parent] = CmsLayout.load_from_file(site, attributes[:parent])
@@ @@ -54,6 +54,23 @@ class CmsLayout < ActiveRecord::Base
new(attributes)
end
+ # Wrapper around load_from_file and find_by_slug
+ # returns layout object if loaded / found
+ def self.load_for_slug!(site, slug)
+ if ComfortableMexicanSofa.configuration.seed_data_path
+ load_from_file(site, slug)
+ else
+ site.cms_layouts.find_by_slug(slug)
+ end || raise(ActiveRecord::RecordNotFound, "CmsLayout with slug: #{slug} cannot be found")
+ end
+
+ # Non-blowing-up version of the method above
+ def self.load_for_slug(site, slug)
+ load_for_slug!(site, slug)
+ rescue ActiveRecord::RecordNotFound
+ nil
+ end
+
# -- Instance Methods -----------------------------------------------------
# magical merging tag is {cms:page:content} If parent layout has this tag
# defined its content will be merged. If no such tag found, parent content
app/models/cms_page.rb +20 -3
@@ @@ -48,10 +48,10 @@ class CmsPage < ActiveRecord::Base
# Attempting to initialize page object from yaml file that is found in config.seed_data_path
# This file defines all attributes of the page plus all the block information
- def self.load_from_file(site, url)
+ def self.load_from_file(site, path)
return nil if ComfortableMexicanSofa.config.seed_data_path.blank?
- url = (url == '/')? '/index' : url.to_s.chomp('/')
- file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/pages#{url}.yml"
+ path = (path == '/')? '/index' : path.to_s.chomp('/')
+ file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/pages#{path}.yml"
return nil unless File.exists?(file_path)
attributes = YAML.load_file(file_path).symbolize_keys!
attributes[:cms_layout] = CmsLayout.load_from_file(site, attributes[:cms_layout])
@@ @@ -60,6 +60,23 @@ class CmsPage < ActiveRecord::Base
new(attributes)
end
+ # Wrapper around load_from_file and find_by_full_path
+ # returns page object if loaded / found
+ def self.load_for_full_path!(site, path)
+ if ComfortableMexicanSofa.configuration.seed_data_path
+ load_from_file(site, path)
+ else
+ site.cms_pages.find_by_full_path(path)
+ end || raise(ActiveRecord::RecordNotFound, "CmsPage with path: #{path} cannot be found")
+ end
+
+ # Non-blowing-up version of the method above
+ def self.load_for_full_path(site, path)
+ load_for_full_path!(site, path)
+ rescue ActiveRecord::RecordNotFound
+ nil
+ end
+
# -- Instance Methods -----------------------------------------------------
# Processing content will return rendered content and will populate
# self.cms_tags with instances of CmsTag
test/cms_seeds/test.host/layouts/default.yml +1 -0
@@ @@ -1,2 +1,3 @@
label: Default Layout
+ slug: default
content: <html>{{cms:page:content}}</html>
\ No newline at end of file
test/cms_seeds/test.host/layouts/nested.yml +1 -0
@@ @@ -1,3 +1,4 @@
label: Nested Layout
+ slug: nested
parent: default
content: <div>{{cms:page:content}}</div>
\ No newline at end of file
test/functional/cms_content_controller_test.rb +2 -2
@@ @@ -58,7 +58,7 @@ class CmsContentControllerTest < ActionController::TestCase
end
def test_render_css
- get :render_css, :id => cms_layouts(:default)
+ get :render_css, :id => cms_layouts(:default).slug
assert_response :success
assert_match %r{text\/css}, response.headers["Content-Type"]
assert_equal cms_layouts(:default).css, response.body
@@ @@ -70,7 +70,7 @@ class CmsContentControllerTest < ActionController::TestCase
end
def test_render_js
- get :render_js, :id => cms_layouts(:default)
+ get :render_js, :id => cms_layouts(:default).slug
assert_response :success
assert_match %r{text\/javascript}, response.headers["Content-Type"]
assert_equal cms_layouts(:default).js, response.body
test/unit/cms_layout_test.rb +25 -0
@@ @@ -85,4 +85,29 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert_equal '<html><div>{{cms:page:content}}</div></html>', layout.merged_content
end
+ def test_load_for_slug
+ assert layout = CmsLayout.load_for_slug!(cms_sites(:default), 'default')
+ assert !layout.new_record?
+ db_content = layout.content
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ assert layout = CmsLayout.load_for_slug!(cms_sites(:default), 'default')
+ assert layout.new_record?
+ file_content = layout.content
+ assert_not_equal db_content, file_content
+ end
+
+ def test_load_for_slug_exceptions
+ assert_exception_raised ActiveRecord::RecordNotFound, 'CmsLayout with slug: not_found cannot be found' do
+ CmsLayout.load_for_slug!(cms_sites(:default), 'not_found')
+ end
+ assert !CmsLayout.load_for_slug(cms_sites(:default), 'not_found')
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ assert_exception_raised ActiveRecord::RecordNotFound, 'CmsLayout with slug: not_found cannot be found' do
+ CmsLayout.load_for_slug!(cms_sites(:default), 'not_found')
+ end
+ assert !CmsLayout.load_for_slug(cms_sites(:default), 'not_found')
+ end
+
end
test/unit/cms_page_test.rb +25 -0
@@ @@ -149,6 +149,31 @@ class CmsPageTest < ActiveSupport::TestCase
assert_equal '<html><div>Sub Child Page Content Content for Default Snippet</div></html>', page.content
end
+ def test_load_for_full_path
+ assert page = CmsPage.load_for_full_path!(cms_sites(:default), '/')
+ assert !page.new_record?
+ db_content = page.content
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ assert page = CmsPage.load_for_full_path!(cms_sites(:default), '/')
+ assert page.new_record?
+ file_content = page.content
+ assert_not_equal db_content, file_content
+ end
+
+ def test_load_for_full_path_exceptions
+ assert_exception_raised ActiveRecord::RecordNotFound, 'CmsPage with path: /invalid_page cannot be found' do
+ CmsPage.load_for_full_path!(cms_sites(:default), '/invalid_page')
+ end
+ assert !CmsPage.load_for_full_path(cms_sites(:default), '/invalid_page')
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ assert_exception_raised ActiveRecord::RecordNotFound, 'CmsPage with path: /invalid_page cannot be found' do
+ CmsPage.load_for_full_path!(cms_sites(:default), '/invalid_page')
+ end
+ assert !CmsPage.load_for_full_path(cms_sites(:default), '/invalid_page')
+ end
+
protected
def new_params(options = {})