adding page caching for cms-css and cms-js

Oleg committed Nov 29, 2010
commit 7a0607f2a49166f3260a12f0e4a389dba745a952
Showing 4 changed files with 38 additions and 4 deletions
.gitignore +4 -1
@@ @@ -7,4 +7,7 @@ db/schema.rb
db/development_structure.sql
public/system/*
pkg
- rdoc
\ No newline at end of file
+ rdoc
+ /tmp
+ public/cms-css/
+ public/cms-js/
app/controllers/cms_content_controller.rb +3 -1
@@ @@ -3,7 +3,9 @@ class CmsContentController < ApplicationController
before_filter :load_cms_site
before_filter :load_cms_page, :only => :render_html
before_filter :load_cms_layout, :only => [:render_css, :render_js]
-
+
+ caches_page :render_css, :render_js
+
def render_html(status = 200)
layout = @cms_page.cms_layout.app_layout.blank?? false : @cms_page.cms_layout.app_layout
render :inline => @cms_page.content, :layout => layout, :status => status
app/models/cms_layout.rb +14 -2
@@ @@ -6,6 +6,10 @@ class CmsLayout < ActiveRecord::Base
belongs_to :cms_site
has_many :cms_pages, :dependent => :nullify
+ # -- Callbacks ------------------------------------------------------------
+ after_save :clear_cache
+ after_destroy :clear_cache
+
# -- Validations ----------------------------------------------------------
validates :cms_site_id,
:presence => true
@@ @@ -18,7 +22,7 @@ class CmsLayout < ActiveRecord::Base
validates :content,
:presence => true
- validate :content_tag_presence
+ validate :check_content_tag_presence
# -- Class Methods --------------------------------------------------------
# Tree-like structure for layouts
@@ @@ -98,11 +102,19 @@ class CmsLayout < ActiveRecord::Base
protected
- def content_tag_presence
+ def check_content_tag_presence
CmsTag.process_content((test_page = CmsPage.new), content)
if test_page.cms_tags.select{|t| t.class.superclass == CmsBlock}.blank?
self.errors.add(:content, 'No cms page tags defined')
end
end
+ # After saving need to make sure that cached pages for css and js for this
+ # layout and its children are gone. Good enough to avoid using cache sweepers.
+ def clear_cache
+ FileUtils.rm File.expand_path("cms-css/#{self.slug}.css", Rails.public_path), :force => true
+ FileUtils.rm File.expand_path("cms-js/#{self.slug}.js", Rails.public_path), :force => true
+ self.children.each{ |child| child.save! }
+ end
+
end
test/unit/cms_layout_test.rb +17 -0
@@ @@ -36,6 +36,23 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert layout.valid?
end
+ def test_creation
+ assert_difference 'CmsLayout.count' do
+ layout = cms_sites(:default).cms_layouts.create(
+ :label => 'New Layout',
+ :slug => 'new-layout',
+ :content => '{{cms:page:content}}',
+ :css => 'css',
+ :js => 'js'
+ )
+ assert_equal 'New Layout', layout.label
+ assert_equal 'new-layout', layout.slug
+ assert_equal '{{cms:page:content}}', layout.content
+ assert_equal 'css', layout.css
+ assert_equal 'js', layout.js
+ end
+ end
+
def test_options_for_select
assert_equal ['Default Layout', 'Nested Layout', '. . Child Layout'],
CmsLayout.options_for_select(cms_sites(:default)).collect{|t| t.first}