I committed a regression around multi site today and site customization, added tests so it don't happen again

This commit is contained in:
Sam Saffron 2013-02-08 00:55:04 +11:00
parent 168bb01439
commit 4fcd924797
2 changed files with 43 additions and 8 deletions

View File

@ -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

View File

@ -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 ==
"<link class=\"custom-css\" rel=\"stylesheet\" href=\"/stylesheet-cache/#{customization.key}.css?#{customization.stylesheet_hash}\" type=\"text/css\" media=\"screen\">"