Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding possibility to configure sass generation directories #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/themes_for_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,9 +24,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
Expand Down
17 changes: 15 additions & 2 deletions lib/themes_for_rails/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,29 @@ 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?
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
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)
Expand Down
12 changes: 11 additions & 1 deletion lib/themes_for_rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions lib/themes_for_rails/interpolation.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down