From 3f9a540ce6693bff8f4853a100c89c56fe543bc5 Mon Sep 17 00:00:00 2001 From: matthias Date: Sat, 13 Apr 2013 11:09:21 +0200 Subject: [PATCH 1/3] Adding possibility to configure sass generation directories By default "sass" files should be stored in a "sass" directory in stylesheet's directory and generated "css" files are saved in stylesheet's directory. The idea is not to have generated "css" stored in same directory as the others and to allow "sass" to be stored in the root stylesheet's directory of themes. Both directory can be set in config as "css_cache_dir" and "sass_dir" eg: all "css" can be saved in a specific cache directory "config.css_cache_dir = :root/tmp/cache/themes_for_rails/:name/stylesheets" --- lib/themes_for_rails.rb | 9 ++++++--- lib/themes_for_rails/assets_controller.rb | 15 ++++++++++++++- lib/themes_for_rails/config.rb | 12 +++++++++++- lib/themes_for_rails/interpolation.rb | 8 ++++++-- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/themes_for_rails.rb b/lib/themes_for_rails.rb index 71c1790..8c11c2f 100644 --- a/lib/themes_for_rails.rb +++ b/lib/themes_for_rails.rb @@ -22,9 +22,10 @@ def add_themes_path_to_sass if ThemesForRails.config.sass_is_available? each_theme_dir do |dir| if File.directory?(dir) # Need to get rid of the '.' and '..' - - sass_dir = "#{dir}/stylesheets/sass" - css_dir = "#{dir}/stylesheets" + asset_theme = File.basename(dir) + + sass_dir = ThemesForRails::Interpolation.interpolate(config.sass_dir, asset_theme) + css_dir = ThemesForRails::Interpolation.interpolate(config.css_cache_dir, asset_theme) unless already_configured_in_sass?(sass_dir) Sass::Plugin.add_template_location sass_dir, css_dir @@ -44,6 +45,8 @@ def already_configured_in_sass?(sass_dir) end require 'active_support/dependencies' +# Make sure that sass/plugin is loaded because Sass load only plugin if Merb::Plugin is loaded +require 'sass/plugin' if defined?(Sass::Engine) require 'themes_for_rails/interpolation' require 'themes_for_rails/config' require 'themes_for_rails/common_methods' diff --git a/lib/themes_for_rails/assets_controller.rb b/lib/themes_for_rails/assets_controller.rb index c4b92c1..7bce3fe 100644 --- a/lib/themes_for_rails/assets_controller.rb +++ b/lib/themes_for_rails/assets_controller.rb @@ -28,7 +28,11 @@ def handle_asset(prefix) end def find_themed_asset(asset_name, asset_theme, asset_type, &block) - path = asset_path(asset_name, asset_theme, asset_type) + if asset_type == 'stylesheets' + path = asset_stylesheets_path(asset_name, asset_theme, asset_type) + else + path = asset_path(asset_name, asset_theme, asset_type) + end if File.exists?(path) yield path, mime_type_for(request) elsif File.extname(path).blank? @@ -38,6 +42,15 @@ def find_themed_asset(asset_name, asset_theme, asset_type, &block) render_not_found end end + + def asset_stylesheets_path(asset_name, asset_theme, asset_type) + path = asset_path(asset_name, asset_theme, asset_type) + if !File.exists?(path) + # Search for css generated by Sass + path = File.join(ThemesForRails::Interpolation.interpolate(ThemesForRails.config.css_cache_dir, asset_theme), asset_name) + end + return path + end def asset_path(asset_name, asset_theme, asset_type) File.join(theme_asset_path_for(asset_theme), asset_type, asset_name) diff --git a/lib/themes_for_rails/config.rb b/lib/themes_for_rails/config.rb index ad0360d..ce2fd54 100644 --- a/lib/themes_for_rails/config.rb +++ b/lib/themes_for_rails/config.rb @@ -2,7 +2,7 @@ module ThemesForRails class Config - attr_writer :base_dir, :themes_dir, :assets_dir, :views_dir, :themes_routes_dir + attr_writer :base_dir, :themes_dir, :assets_dir, :views_dir, :themes_routes_dir, :css_cache_dir, :sass_dir attr_accessor :use_sass, :default_theme include Interpolation @@ -39,6 +39,16 @@ def themes_dir @themes_dir ||= ":root/themes" end + # Default dir where css file are store on Sass generation + def css_cache_dir + @css_cache_dir ||= ":root/themes/:name/stylesheets" + end + + # Default sass dir path + def sass_dir + @sass_dir ||= ":root/themes/:name/stylesheets/sass" + end + # Full path to themes def themes_path interpolate(themes_dir) diff --git a/lib/themes_for_rails/interpolation.rb b/lib/themes_for_rails/interpolation.rb index 395b1a4..05d108a 100644 --- a/lib/themes_for_rails/interpolation.rb +++ b/lib/themes_for_rails/interpolation.rb @@ -1,9 +1,13 @@ module ThemesForRails module Interpolation - + def interpolate(pattern, name = nil) - pattern.gsub(":root", ThemesForRails.config.base_dir.to_s).gsub(":name", name.to_s) + ThemesForRails::Interpolation.interpolate(pattern, name) + end + + def self.interpolate(pattern, name = nil) + pattern.gsub(":root", ThemesForRails.config.base_dir).gsub(":name", name.to_s) end end From 859d5c1e59dd9f8db7e47106056fae073b737880 Mon Sep 17 00:00:00 2001 From: matthias Date: Sun, 14 Apr 2013 12:02:07 +0200 Subject: [PATCH 2/3] Fix requirement bug for sass/plugin --- lib/themes_for_rails.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/themes_for_rails.rb b/lib/themes_for_rails.rb index 8c11c2f..09a97a9 100644 --- a/lib/themes_for_rails.rb +++ b/lib/themes_for_rails.rb @@ -4,6 +4,8 @@ module ThemesForRails class << self def config + # Make sure that sass/plugin is loaded because Sass load only plugin if Merb::Plugin is loaded + require 'sass/plugin' if defined?(Sass::Engine) @config ||= ThemesForRails::Config.new yield(@config) if block_given? @config @@ -45,8 +47,6 @@ def already_configured_in_sass?(sass_dir) end require 'active_support/dependencies' -# Make sure that sass/plugin is loaded because Sass load only plugin if Merb::Plugin is loaded -require 'sass/plugin' if defined?(Sass::Engine) require 'themes_for_rails/interpolation' require 'themes_for_rails/config' require 'themes_for_rails/common_methods' From 6741130bccc7738d64d56d9d5a41441b0273ab77 Mon Sep 17 00:00:00 2001 From: matthias Date: Sun, 14 Apr 2013 16:31:02 +0200 Subject: [PATCH 3/3] Fix asset extension detection To prevent doted filename to return a 404 error, extension comparison is now done with mime type extension and not "blank?" --- lib/themes_for_rails/assets_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/themes_for_rails/assets_controller.rb b/lib/themes_for_rails/assets_controller.rb index 7bce3fe..efbd38c 100644 --- a/lib/themes_for_rails/assets_controller.rb +++ b/lib/themes_for_rails/assets_controller.rb @@ -35,7 +35,7 @@ def find_themed_asset(asset_name, asset_theme, asset_type, &block) end if File.exists?(path) yield path, mime_type_for(request) - elsif File.extname(path).blank? + elsif File.extname(path) != extension_from(request.path_info) asset_name = "#{asset_name}.#{extension_from(request.path_info)}" return find_themed_asset(asset_name, asset_theme, asset_type, &block) else