Skip to content

Commit

Permalink
fixed #61 + good amount of cache-related cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmh committed Oct 8, 2008
1 parent 53dd8da commit 8acbf3b
Show file tree
Hide file tree
Showing 23 changed files with 75 additions and 59 deletions.
2 changes: 1 addition & 1 deletion config/environments/development.rb
Expand Up @@ -11,7 +11,7 @@
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
config.action_controller.perform_caching = true

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
Expand Down
Empty file added public/forums/1.html
Empty file.
Empty file added public/forums/1/boards/1.html
Empty file.
Empty file.
Empty file added public/sections/1.html
Empty file.
1 change: 1 addition & 0 deletions public/sections/1/articles/an-article.atom
@@ -0,0 +1 @@
comments/comments
Empty file.
1 change: 1 addition & 0 deletions public/sections/1/comments.atom
@@ -0,0 +1 @@
comments/comments
1 change: 1 addition & 0 deletions public/users/1/roles.js
@@ -0,0 +1 @@
roles/index
Empty file added public/wikis/1.html
Empty file.
Empty file.
1 change: 1 addition & 0 deletions public/wikis/1/comments.atom
@@ -0,0 +1 @@
comments/comments
Empty file added public/wikis/1/pages.html
Empty file.
Empty file.
1 change: 1 addition & 0 deletions public/wikis/1/pages/a-wikipage/comments.atom
@@ -0,0 +1 @@
comments/comments
Empty file.
23 changes: 14 additions & 9 deletions spec/helpers/theme_helper_spec.rb
Expand Up @@ -5,26 +5,31 @@

before :each do
helper.stub!(:join_asset_file_contents).and_return "joined_asset_file_contents"
helper.stub!(:page_cache_subdirectory).and_return 'tmp/cache/site.host'
helper.stub!(:perma_host).and_return 'test.site'

ActionController::Base.perform_caching = true
@cache_dir = "#{RAILS_ROOT}/#{helper.page_cache_subdirectory}"
FileUtils.rm_r @cache_dir if File.exists? @cache_dir
end

after :each do
ActionController::Base.perform_caching = false
FileUtils.rm_r @cache_dir if File.exists? @cache_dir
end

it "scopes the stylesheet cache file path to the site's page_cache_subdirectory" do
it "caches the stylesheets in the site's stylesheet cache directory" do
cache_dir = File.join( Rails.root, 'public', 'stylesheets', 'cache', helper.perma_host )
FileUtils.rm_r cache_dir if File.exists? cache_dir
File.exists?("#{cache_dir}/default.css").should be_false
helper.stylesheet_link_tag('foo', 'bar', :cache => 'default') =~ /href="([^"\?]*)(\?|")/
File.exists?("#{@cache_dir}/stylesheets/default.css").should be_true
File.exists?("#{cache_dir}/default.css").should be_true
FileUtils.rm_r cache_dir if File.exists? cache_dir
end

it "scopes the javascript cache file path to the site's page_cache_subdirectory" do
it "caches the javascripts in the site's javascript cache directory" do
cache_dir = File.join( Rails.root, 'public', 'javascripts', 'cache', helper.perma_host )
FileUtils.rm_r cache_dir if File.exists? cache_dir
File.exists?("#{cache_dir}/default.js").should be_false
helper.javascript_include_tag('foo', 'bar', :cache => 'default') =~ /src="([^"\?]*)(\?|")/
File.exists?("#{@cache_dir}/javascripts/default.js").should be_true
File.exists?("#{cache_dir}/default.js").should be_true
FileUtils.rm_r cache_dir if File.exists? cache_dir
end
end

Expand Down Expand Up @@ -95,4 +100,4 @@ def controller
$1.should == '/stylesheets/themes/theme.id/asset.css'
end
end
end
end
3 changes: 2 additions & 1 deletion spec/stubs/site.rb
Expand Up @@ -41,7 +41,8 @@
:has_attribute? => true,
:spam_filter_active? => false,
:role_context => stub_site,
:page_cache_directory => RAILS_ROOT + '/tmp/cache'
:page_cache_directory => RAILS_ROOT + '/tmp/cache',
:multi_sites_enabled? => true

instance :default
end
Expand Up @@ -38,7 +38,7 @@ def admin_section_path_for(section)
# else admin_articles_path section.site, section
end
end

protected

def require_authentication
Expand Down Expand Up @@ -87,9 +87,4 @@ def set_section
def current_role_context
@section || @site || Site.new
end

def page_cache_directory
raise "@site not set" unless @site
@site.page_cache_directory
end
end
35 changes: 16 additions & 19 deletions vendor/engines/adva_cms/app/controllers/base_controller.rb
Expand Up @@ -2,8 +2,9 @@

class BaseController < ApplicationController
class SectionRoutingError < ActionController::RoutingError; end
helper :base, :content, :comments, :users, :roles
helper_method :page_cache_subdirectory
helper :base, :content, :comments, :users, :roles
helper_method :perma_host

include ContentHelper # WTF!

include CacheableFlash
Expand All @@ -20,9 +21,7 @@ class SectionRoutingError < ActionController::RoutingError; end
# :force_template_types => ['html.serb', 'liquid']
# :force_template_types => lambda {|c| ['html.serb', 'liquid'] unless c.class.name =~ /^Admin::/ }

def asset_cache_directory
"cache/#{@site.host}"
end
def perma_host; @site.perma_host end

# TODO move these to acts_as_commentable (?)
caches_page_with_references :comments, :track => ['@commentable']
Expand Down Expand Up @@ -59,10 +58,6 @@ def set_timezone
Time.zone = @site.timezone if @site
end

def set_cache_root
self.class.page_cache_directory = page_cache_directory.to_s
end

def current_page
@page ||= params[:page].blank? ? 1 : params[:page].to_i
end
Expand All @@ -71,16 +66,6 @@ def set_commentable
@commentable = @article || @section || @site
end

def page_cache_directory
raise "@site not set" unless @site
@site.page_cache_directory
end

def page_cache_subdirectory
raise "@site not set" unless @site
@site.page_cache_subdirectory
end

def rescue_action(exception)
if exception.is_a? ActionController::RoleRequired
redirect_to_login exception.message
Expand All @@ -98,4 +83,16 @@ def redirect_to_login(notice = nil)
def current_role_context
@section || @site
end

def page_cache_directory
if Rails.env == 'test'
@site.multi_sites_enabled? ? 'tmp/cache/' + perma_host : 'tmp/cache'
else
@site.multi_sites_enabled? ? 'public/cache/' + perma_host : 'public'
end
end

def set_cache_root
self.class.page_cache_directory = page_cache_directory.to_s
end
end
16 changes: 4 additions & 12 deletions vendor/engines/adva_cms/app/models/site.rb
Expand Up @@ -61,6 +61,10 @@ def find_users_and_superusers(id, options = {})
end
end

def multi_sites_enabled?
self.class.multi_sites_enabled
end

def owner
nil
end
Expand All @@ -81,18 +85,6 @@ def perma_host
host.sub(':', '.') # Needed to create valid directories in ms-win
end

def page_cache_directory
Pathname.new(File.expand_path(RAILS_ROOT)) + page_cache_subdirectory
end

def page_cache_subdirectory
if RAILS_ENV == 'test'
self.class.multi_sites_enabled ? 'tmp/cache/' + perma_host : 'tmp/cache'
else
self.class.multi_sites_enabled ? 'public/cache/' + perma_host : 'public'
end
end

def plugins
@plugins ||= Plugins.new self, Engines.plugins
end
Expand Down
39 changes: 30 additions & 9 deletions vendor/engines/theme_support/lib/theme_support/asset_tag_helper.rb
@@ -1,13 +1,34 @@
ActionView::Helpers::AssetTagHelper.module_eval do
# TODO wow, this sux. patch asset_tag_helpers to use an overwriteable
# mattr_accessor instead of constants for directories
def write_asset_file_contents_with_path_munging(joined_asset_path, asset_paths)
if respond_to?(:page_cache_subdirectory)
joined_asset_path.sub! %r(public/(stylesheets|javascripts)/), "#{page_cache_subdirectory}/#{'\1'}/"
module ActionView::Helpers::AssetTagHelper
# TODO might want to create our own custom asset caching plugin.
# probably better than monkey-patching rails. --JMH
def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
cache = options.delete("cache")

if ActionController::Base.perform_caching && cache
joined_javascript_name = File.join( 'cache', perma_host, (cache == true ? 'all' : cache) + '.js' )
joined_javascript_path = File.join(JAVASCRIPTS_DIR, joined_javascript_name)
write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources))
javascript_src_tag(joined_javascript_name, options)
else
expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n")
end
end

def stylesheet_link_tag(*sources)
options = sources.extract_options!.stringify_keys
cache = options.delete("cache")

if ActionController::Base.perform_caching && cache
joined_stylesheet_name = File.join( 'cache', perma_host, (cache == true ? 'all' : cache) + '.css' )
joined_stylesheet_path = File.join(STYLESHEETS_DIR, joined_stylesheet_name)

write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources))
stylesheet_tag(joined_stylesheet_name, options)
else
expand_stylesheet_sources(sources).collect { |source| stylesheet_tag(source, options) }.join("\n")
end
write_asset_file_contents_without_path_munging joined_asset_path, asset_paths
end
alias_method_chain :write_asset_file_contents, :path_munging
end

class ActionController::Base
Expand All @@ -20,4 +41,4 @@ class ActionView::Base
def self.reset_file_exist_cache!
@@file_exist_cache = nil
end
end
end
4 changes: 2 additions & 2 deletions vendor/plugins/page_cache_tagging/lib/page_cache_tagging.rb
Expand Up @@ -64,7 +64,7 @@ def expire_pages(pages)
pages.each { |page| expire_page(page.url) }
CachedPage.expire_pages(pages)
end

def expire_site_page_cache
cache_dir = page_cache_directory
if cache_dir =~ /\/public$/
Expand All @@ -81,4 +81,4 @@ def expire_site_page_cache
ActionController::Base.reset_file_exist_cache!
ActionView::Base.reset_file_exist_cache!
end
end
end

0 comments on commit 8acbf3b

Please sign in to comment.