From 4fcd924797d6afdbf9362f387edf6f72ed72402e Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 8 Feb 2013 00:55:04 +1100 Subject: [PATCH] I committed a regression around multi site today and site customization, added tests so it don't happen again --- app/models/site_customization.rb | 22 ++++++++++++------- spec/models/site_customization_spec.rb | 29 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index 3b455785c72..4dd77a299b8 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -50,30 +50,36 @@ footer:after{ content: '#{error}' }" self.remove_from_cache! end - def self.enabled_style + def self.enabled_key + ENABLED_KEY.dup << RailsMultisite::ConnectionManagement.current_db + end + + def self.enabled_style_key @cache ||= {} - preview_style = @cache[ENABLED_KEY] + preview_style = @cache[self.enabled_key] return nil if preview_style == :none return preview_style if preview_style @lock.synchronize do style = self.where(enabled: true).first if style - @cache[ENABLED_KEY] = style.key + @cache[self.enabled_key] = style.key + return style.key else - @cache[ENABLED_KEY] = :none + @cache[self.enabled_key] = :none + return nil end end end def self.custom_stylesheet(preview_style) - preview_style ||= enabled_style + preview_style ||= enabled_style_key style = lookup_style(preview_style) style.stylesheet_link_tag.html_safe if style end def self.custom_header(preview_style) - preview_style ||= enabled_style + preview_style ||= enabled_style_key style = lookup_style(preview_style) if style && style.header style.header.html_safe @@ -83,7 +89,7 @@ footer:after{ content: '#{error}' }" end def self.override_default_style(preview_style) - preview_style ||= enabled_style + preview_style ||= enabled_style_key style = lookup_style(preview_style) style.override_default_style if style end @@ -126,7 +132,7 @@ footer:after{ content: '#{error}' }" end def remove_from_cache! - self.class.remove_from_cache!(ENABLED_KEY) + self.class.remove_from_cache!(self.class.enabled_key) self.class.remove_from_cache!(self.key) end diff --git a/spec/models/site_customization_spec.rb b/spec/models/site_customization_spec.rb index 7be20ae75c3..68a554cf9f2 100644 --- a/spec/models/site_customization_spec.rb +++ b/spec/models/site_customization_spec.rb @@ -16,6 +16,35 @@ describe SiteCustomization do end context 'caching' do + + context 'enabled style' do + before do + @customization = customization + end + + it 'finds no style when none enabled' do + SiteCustomization.enabled_style_key.should be_nil + end + + it 'finds the enabled style' do + @customization.enabled = true + @customization.save + SiteCustomization.enabled_style_key.should == @customization.key + end + + it 'finds no enabled style on other sites' do + @customization.enabled = true + @customization.save + + RailsMultisite::ConnectionManagement.expects(:current_db).returns("foo").twice + # the mocking is tricky, lets remove the record so we can properly pretend we are on another db + # this bypasses the before / after stuff + SiteCustomization.exec_sql('delete from site_customizations') + + SiteCustomization.enabled_style_key.should be_nil + end + end + it 'should allow me to lookup a filename containing my preview stylesheet' do SiteCustomization.custom_stylesheet(customization.key).should == ""