adding ability to use cms seed data instead of relying on database

Oleg committed Oct 22, 2010
commit fa312d0a7277663b944584f8dbfd87d8cea3c74d
Showing 19 changed files with 198 additions and 64 deletions
Gemfile.lock +30 -31
@@ @@ -2,12 +2,12 @@ GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
- actionmailer (3.0.0)
- actionpack (= 3.0.0)
+ actionmailer (3.0.1)
+ actionpack (= 3.0.1)
mail (~> 2.2.5)
- actionpack (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
+ actionpack (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
@@ @@ -16,26 +16,25 @@ GEM
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
active_link_to (0.0.6)
- activemodel (3.0.0)
- activesupport (= 3.0.0)
+ activemodel (3.0.1)
+ activesupport (= 3.0.1)
builder (~> 2.1.2)
i18n (~> 0.4.1)
- activerecord (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
+ activerecord (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
- activeresource (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
- activesupport (3.0.0)
+ activeresource (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
+ activesupport (3.0.1)
arel (1.0.1)
activesupport (~> 3.0.0)
builder (2.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
- gemcutter (0.5.0)
- json_pure
+ gemcutter (0.6.1)
git (1.2.5)
i18n (0.4.1)
jeweler (1.4.0)
@@ @@ -43,38 +42,38 @@ GEM
git (>= 1.2.5)
rubyforge (>= 2.0.0)
json_pure (1.4.6)
- mail (2.2.5)
+ mail (2.2.7)
activesupport (>= 2.3.6)
mime-types
treetop (>= 1.4.5)
mime-types (1.16)
- paperclip (2.3.3)
+ paperclip (2.3.4)
activerecord
activesupport
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
- rack-test (0.5.4)
+ rack-test (0.5.6)
rack (>= 1.0)
- rails (3.0.0)
- actionmailer (= 3.0.0)
- actionpack (= 3.0.0)
- activerecord (= 3.0.0)
- activeresource (= 3.0.0)
- activesupport (= 3.0.0)
+ rails (3.0.1)
+ actionmailer (= 3.0.1)
+ actionpack (= 3.0.1)
+ activerecord (= 3.0.1)
+ activeresource (= 3.0.1)
+ activesupport (= 3.0.1)
bundler (~> 1.0.0)
- railties (= 3.0.0)
- railties (3.0.0)
- actionpack (= 3.0.0)
- activesupport (= 3.0.0)
+ railties (= 3.0.1)
+ railties (3.0.1)
+ actionpack (= 3.0.1)
+ activesupport (= 3.0.1)
rake (>= 0.8.4)
thor (~> 0.14.0)
rake (0.8.7)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
sqlite3-ruby (1.3.1)
- thor (0.14.0)
+ thor (0.14.3)
treetop (1.4.8)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
@@ @@ -87,5 +86,5 @@ DEPENDENCIES
jeweler (>= 1.4.0)
mime-types
paperclip (>= 2.3.3)
- rails (>= 3.0.0)
+ rails (>= 3.0.1)
sqlite3-ruby
app/controllers/cms_content_controller.rb +6 -1
@@ @@ -26,7 +26,12 @@ protected
end
def load_cms_page
- @cms_page = @cms_site.cms_pages.find_by_full_path!("/#{params[:cms_path]}")
+ # 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]}")
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')
app/models/cms_layout.rb +11 -0
@@ @@ -33,6 +33,17 @@ class CmsLayout < ActiveRecord::Base
end.compact
end
+ # Attempting to initialize layout object from yaml file that is found in config.seed_data_path
+ def self.load_from_file(site, name)
+ return nil if ComfortableMexicanSofa.config.seed_data_path.blank?
+ file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/layouts/#{name}.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])
+ attributes[:cms_site] = site
+ new(attributes)
+ 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 +14 -0
@@ @@ -45,6 +45,20 @@ class CmsPage < ActiveRecord::Base
return out.compact
end
+ # 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)
+ 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"
+ 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])
+ attributes[:parent] = CmsPage.load_from_file(site, attributes[:parent])
+ attributes[:cms_site] = site
+ new(attributes)
+ end
+
# -- Instance Methods -----------------------------------------------------
# Processing content will return rendered content and will populate
# self.cms_tags with instances of CmsTag
app/models/cms_snippet.rb +13 -1
@@ @@ -19,7 +19,19 @@ class CmsSnippet < ActiveRecord::Base
end
def self.initialize_or_find(cms_page, slug)
- find_by_slug(slug) || new(:slug => slug)
+ if ComfortableMexicanSofa.configuration.seed_data_path
+ s = CmsTag::Snippet.load_from_file(cms_page.cms_site, slug)
+ end
+ s || find_by_slug(slug) || new(:slug => slug)
+ end
+
+ # Attempting to initialize snippet object from yaml file that is found in config.seed_data_path
+ def self.load_from_file(site, name)
+ return nil if ComfortableMexicanSofa.config.seed_data_path.blank?
+ file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/snippets/#{name}.yml"
+ return nil unless File.exists?(file_path)
+ attributes = YAML.load_file(file_path).symbolize_keys!
+ new(attributes)
end
end
comfortable_mexican_sofa/configuration.rb b/lib/comfortable_mexican_sofa/configuration.rb +6 -5
@@ @@ -6,14 +6,15 @@ class ComfortableMexicanSofa::Configuration
# Module that will handle authentication to access cms-admin area
attr_accessor :authentication
- # Enable cms to manage multiple sites
- attr_accessor :multiple_sites
+ # Location of YAML files that can be used to render pages instead of pulling
+ # data from the database. Not active if not specified.
+ attr_accessor :seed_data_path
# Configuration defaults
def initialize
- @cms_title = 'ComfortableMexicanSofa'
- @authentication = 'CmsHttpAuthentication'
- @multiple_sites = false
+ @cms_title = 'ComfortableMexicanSofa'
+ @authentication = 'ComfortableMexicanSofa::HttpAuth'
+ @seed_data_path = nil
end
end
\ No newline at end of file
test/cms_seeds/test.host/layouts/default.yml +2 -0
@@ @@ -0,0 +1,2 @@
+ label: Default Layout
+ content: <html><cms:page:content/></html>
\ No newline at end of file
test/cms_seeds/test.host/layouts/nested.yml +3 -0
@@ @@ -0,0 +1,3 @@
+ label: Nested Layout
+ parent: default
+ content: <div><cms:page:content/></div>
\ No newline at end of file
test/cms_seeds/test.host/pages/child.yml +10 -0
@@ @@ -0,0 +1,10 @@
+ label: Child Page
+ parent: /
+ cms_layout: default
+ slug: child
+ cms_blocks_attributes:
+ -
+ label: content
+ type: CmsTag::PageText
+ content: |-
+ Child Page Content
\ No newline at end of file
test/cms_seeds/test.host/pages/child/subchild.yml +10 -0
@@ @@ -0,0 +1,10 @@
+ label: Child Page
+ parent: /child
+ cms_layout: nested
+ slug: subchild
+ cms_blocks_attributes:
+ -
+ label: content
+ type: CmsTag::PageText
+ content: |-
+ Sub Child Page Content <cms:snippet:default/>
\ No newline at end of file
test/cms_seeds/test.host/pages/index.yml +10 -0
@@ @@ -0,0 +1,10 @@
+ label: Default Page
+ parent:
+ cms_layout: default
+ slug:
+ cms_blocks_attributes:
+ -
+ label: content
+ type: CmsTag::PageText
+ content: |-
+ Default Page Content
\ No newline at end of file
test/cms_seeds/test.host/snippets/default.yml +3 -0
@@ @@ -0,0 +1,3 @@
+ label: Default Snippet
+ slug: default
+ content: Content for Default Snippet
\ No newline at end of file
test/fixtures/README.md +0 -22
@@ @@ -1,22 +0,0 @@
- Default Test Application Data Structure
- =======================================
- Layout
- ------
- <cms:field:default_field:text>
- layout_content_a
- <cms:page:default_page:text>
- layout_content_b
- <cms:snippet:default_snippet>
- layout_content_c
- Page
- ----
- ### CmsBlock::FieldText
- default_field_content
- ### CmsBlock::PageText
- default_page_content_a
- <cms:snippet:default_snippet>
- default_page_content_a
- Snippets
- -------
- ### Snippet
- default_snippet_content
\ No newline at end of file
test/integration/render_cms_seed_test.rb +16 -0
@@ @@ -0,0 +1,16 @@
+ require File.dirname(__FILE__) + '/../test_helper'
+
+ class RenderCmsSeedTest < ActionDispatch::IntegrationTest
+
+ def test_render_with_seed_data_enabled
+ get '/child/subchild'
+ assert_response 404
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+
+ get '/child/subchild'
+ assert_response :success
+ assert_equal '<html><div>Sub Child Page Content Content for Default Snippet</div></html>', response.body
+ end
+
+ end
\ No newline at end of file
test/test_helper.rb +1 -0
@@ @@ -11,6 +11,7 @@ class ActiveSupport::TestCase
ComfortableMexicanSofa.configure do |config|
config.cms_title = 'ComfortableMexicanSofa'
config.authentication = 'ComfortableMexicanSofa::HttpAuth'
+ config.seed_data_path = nil
end
ComfortableMexicanSofa::HttpAuth.username = 'username'
ComfortableMexicanSofa::HttpAuth.password = 'password'
test/unit/cms_configuration_test.rb +1 -0
@@ @@ -6,6 +6,7 @@ class CmsConfigurationTest < ActiveSupport::TestCase
assert config = ComfortableMexicanSofa.configuration
assert_equal 'ComfortableMexicanSofa', config.cms_title
assert_equal 'ComfortableMexicanSofa::HttpAuth', config.authentication
+ assert_equal nil, config.seed_data_path
end
def test_initialization_overrides
test/unit/cms_layout_test.rb +17 -0
@@ @@ -45,4 +45,21 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert_equal merged_js, cms_layouts(:child).merged_js
end
+ def test_load_from_file
+ assert !CmsLayout.load_from_file(cms_sites(:default), 'default')
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+
+ assert !CmsLayout.load_from_file(cms_sites(:default), 'bogus')
+
+ assert layout = CmsLayout.load_from_file(cms_sites(:default), 'default')
+ assert_equal 'Default Layout', layout.label
+ assert_equal '<html><cms:page:content/></html>', layout.content
+
+ assert layout = CmsLayout.load_from_file(cms_sites(:default), 'nested')
+ assert_equal 'Nested Layout', layout.label
+ assert_equal '<div><cms:page:content/></div>', layout.content
+ assert_equal '<html><div><cms:page:content/></div></html>', layout.merged_content
+ end
+
end
test/unit/cms_page_test.rb +33 -4
@@ @@ -112,12 +112,41 @@ class CmsPageTest < ActiveSupport::TestCase
end
def test_options_for_select
- assert_equal ['Default Page', '. . Child Page'], CmsPage.options_for_select(cms_sites(:default)).collect{|t| t.first }
- assert_equal ['Default Page'], CmsPage.options_for_select(cms_sites(:default), cms_pages(:child)).collect{|t| t.first }
- assert_equal [], CmsPage.options_for_select(cms_sites(:default), cms_pages(:default))
+ assert_equal ['Default Page', '. . Child Page'],
+ CmsPage.options_for_select(cms_sites(:default)).collect{|t| t.first }
+ assert_equal ['Default Page'],
+ CmsPage.options_for_select(cms_sites(:default), cms_pages(:child)).collect{|t| t.first }
+ assert_equal [],
+ CmsPage.options_for_select(cms_sites(:default), cms_pages(:default))
page = CmsPage.new(new_params(:parent => cms_pages(:default)))
- assert_equal ['Default Page', '. . Child Page'], CmsPage.options_for_select(cms_sites(:default), page).collect{|t| t.first }
+ assert_equal ['Default Page', '. . Child Page'],
+ CmsPage.options_for_select(cms_sites(:default), page).collect{|t| t.first }
+ end
+
+ def test_load_from_file
+ assert !CmsPage.load_from_file(cms_sites(:default), '/')
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+
+ assert !CmsPage.load_from_file(cms_sites(:default), '/bogus')
+
+ assert page = CmsPage.load_from_file(cms_sites(:default), '/')
+ assert_equal 'Default Page', page.label
+ assert_equal 1, page.cms_blocks.size
+ assert page.cms_layout
+ assert_equal '<html>Default Page Content</html>', page.content
+
+ assert page = CmsPage.load_from_file(cms_sites(:default), '/child')
+ assert_equal 1, page.cms_blocks.size
+ assert page.cms_layout
+ assert_equal '<html>Child Page Content</html>', page.content
+
+ assert page = CmsPage.load_from_file(cms_sites(:default), '/child/subchild')
+ assert_equal 1, page.cms_blocks.size
+ assert page.cms_layout
+ assert_equal 'Nested Layout', page.cms_layout.label
+ assert_equal '<html><div>Sub Child Page Content Content for Default Snippet</div></html>', page.content
end
protected
test/unit/cms_snippet_test.rb +12 -0
@@ @@ -20,4 +20,16 @@ class CmsSnippetTest < ActiveSupport::TestCase
assert_equal '', CmsSnippet.content_for('nonexistent_snippet')
end
+ def test_load_from_file
+ assert !CmsSnippet.load_from_file(cms_sites(:default), 'default')
+
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+
+ assert !CmsSnippet.load_from_file(cms_sites(:default), 'bogus')
+
+ assert snippet = CmsSnippet.load_from_file(cms_sites(:default), 'default')
+ assert_equal 'Default Snippet', snippet.label
+ assert_equal 'Content for Default Snippet', snippet.content
+ end
+
end