better support for multi host in site customization

This commit is contained in:
Sam Saffron 2013-02-10 23:05:11 +11:00
parent 4b0eec334f
commit 6fb78809c2
2 changed files with 20 additions and 9 deletions

View File

@ -106,6 +106,7 @@ footer:after{ content: '#{error}' }"
@lock.synchronize do
style = self.where(key: key).first
style.ensure_stylesheet_on_disk!
@cache[key] = style
end
end
@ -140,9 +141,13 @@ footer:after{ content: '#{error}' }"
Digest::MD5.hexdigest(self.stylesheet)
end
def cache_fullpath
"#{Rails.root}/public/#{CACHE_PATH}"
end
def ensure_stylesheet_on_disk!
path = stylesheet_fullpath
dir = "#{Rails.root}/public/#{CACHE_PATH}"
dir = cache_fullpath
FileUtils.mkdir_p(dir)
unless File.exists?(path)
File.open(path, "w") do |f|
@ -152,23 +157,18 @@ footer:after{ content: '#{error}' }"
end
def stylesheet_filename
file = ""
dir = "#{Rails.root}/public/#{CACHE_PATH}"
path = dir + file
"/#{CACHE_PATH}/#{self.key}.css"
"/#{self.key}.css"
end
def stylesheet_fullpath
"#{Rails.root}/public#{self.stylesheet_filename}"
"#{self.cache_fullpath}#{self.stylesheet_filename}"
end
def stylesheet_link_tag
return "" unless self.stylesheet.present?
return @stylesheet_link_tag if @stylesheet_link_tag
ensure_stylesheet_on_disk!
@stylesheet_link_tag = "<link class=\"custom-css\" rel=\"stylesheet\" href=\"#{self.stylesheet_filename}?#{self.stylesheet_hash}\" type=\"text/css\" media=\"screen\">"
@stylesheet_link_tag = "<link class=\"custom-css\" rel=\"stylesheet\" href=\"/#{CACHE_PATH}#{self.stylesheet_filename}?#{self.stylesheet_hash}\" type=\"text/css\" media=\"screen\">"
end
end

View File

@ -26,6 +26,7 @@ describe SiteCustomization do
SiteCustomization.enabled_style_key.should be_nil
end
it 'finds the enabled style' do
@customization.enabled = true
@customization.save
@ -45,6 +46,16 @@ describe SiteCustomization do
end
end
it 'ensure stylesheet is on disk on first fetch' do
c = customization
c.remove_from_cache!
File.delete(c.stylesheet_fullpath)
SiteCustomization.custom_stylesheet(c.key)
File.exists?(c.stylesheet_fullpath).should == true
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\">"