Skip to content

Commit

Permalink
fixes for asset pipeline support
Browse files Browse the repository at this point in the history
  • Loading branch information
JediFreeman committed Feb 16, 2012
1 parent f3d1754 commit cea54b1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ tmp
.DS_Store
spec/dummy/db/*.sqlite3
spec/dummy/log/test.log
.svn
6 changes: 3 additions & 3 deletions lib/themes_for_rails/action_view.rb
Expand Up @@ -43,14 +43,14 @@ def theme_image_submit_tag(source, options = {})

def theme_javascript_include_tag(*files)
files.collect! {|file| theme_javascript_path(file) }
javascript_include_tag *files
javascript_include_tag(*files, :type => "text/javascript")
end

def theme_stylesheet_link_tag(*files)
options = files.extract_options!
files.collect! {|file| theme_stylesheet_path(file) }
files << options
stylesheet_link_tag(*files)
options.merge!({ :type => "text/css" })
stylesheet_link_tag(*files, options)
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions lib/themes_for_rails/assets_controller.rb
Expand Up @@ -5,39 +5,40 @@ module ThemesForRails
class AssetsController < ActionController::Base

def stylesheets
handle_asset(params[:asset], params[:theme], "stylesheets")
handle_asset("stylesheets")
end

def javascripts
handle_asset(params[:asset], params[:theme], "javascripts")
handle_asset("javascripts")
end

def images
handle_asset(params[:asset], params[:theme], "images")
handle_asset("images")
end

private

def handle_asset(asset, theme, prefix)
def handle_asset(prefix)
asset, theme = params[:asset], params[:theme]
find_themed_asset(asset, theme, prefix) do |path, mime_type|
send_file path, :type => mime_type, :disposition => "inline"
end
end

def find_themed_asset(asset_name, asset_theme, asset_prefix, &block)
path = asset_path(asset_name, asset_theme, asset_prefix)
def find_themed_asset(asset_name, asset_theme, asset_type, &block)
path = asset_path(asset_name, asset_theme, asset_type)
if File.exists?(path)
yield path, mime_type_for(request)
elsif File.extname(path).blank?
asset_name = "#{asset_name}.#{extension_from(request.path_info)}"
return find_themed_asset(asset_name, asset_theme, asset_prefix, &block)
return find_themed_asset(asset_name, asset_theme, asset_type, &block)
else
render_not_found
end
end

def asset_path(asset_name, asset_theme, asset_prefix)
File.join(theme_path_for(asset_theme), asset_prefix, asset_name)
def asset_path(asset_name, asset_theme, asset_type)
File.join(theme_path_for(asset_theme), asset_type, asset_name)
end

def render_not_found
Expand Down
4 changes: 2 additions & 2 deletions lib/themes_for_rails/common_methods.rb
Expand Up @@ -53,8 +53,8 @@ def theme_path(base = ThemesForRails.config.base_dir)
theme_path_for(theme_name, base)
end

def theme_path_for(name, base = ThemesForRails.config.base_dir, theme_dir = ThemesForRails.config.themes_dir)
File.join(base, theme_dir, name)
def theme_path_for(name, base = ThemesForRails.config.base_dir, asset_dir = ThemesForRails.config.assets_dir)
File.join(base, asset_dir, name)
end
end
end
16 changes: 13 additions & 3 deletions lib/themes_for_rails/config.rb
Expand Up @@ -2,29 +2,39 @@
module ThemesForRails
class Config

attr_writer :base_dir, :themes_dir
attr_accessor :use_sass
attr_writer :base_dir, :themes_dir, :assets_dir
attr_accessor :use_sass, :default_theme

def initialize(&block)
@use_sass = true
@default_theme = 'default'
yield if block_given?
end

# Rails Root: /vagrant/web
def base_dir
@base_dir ||= Rails.root
end

# relative assets dir for asset pipeline: /vagrant/web/app/ {assets} /themes/ {theme}
def assets_dir
@assets_dir ||= "assets"
end

# Themes Dir (relative path to themes from base_dir): app/assets/themes
def themes_dir
@themes_dir ||= "themes"
@themes_dir ||= "app/assets/themes"
end

# Full path to themes
def themes_path
File.join(base_dir, themes_dir)
end

def clear
@base_dir = nil
@themes_dir = nil
@assets_dir = nil
end

def use_sass?
Expand Down
8 changes: 5 additions & 3 deletions lib/themes_for_rails/routes.rb
Expand Up @@ -4,12 +4,14 @@ module Routes

def themes_for_rails
theme_dir = ThemesForRails.config.themes_dir
constraints = { :theme => /[\w\.]*/ }

match "#{theme_dir}/:theme/stylesheets/*asset" => 'themes_for_rails/assets#stylesheets',
:as => :base_theme_stylesheet, :constraints => { :theme => /.*/ }
:as => :base_theme_stylesheet, :constraints => constraints
match "#{theme_dir}/:theme/javascripts/*asset" => 'themes_for_rails/assets#javascripts',
:as => :base_theme_javascript, :constraints => { :theme => /.*/ }
:as => :base_theme_javascript, :constraints => constraints
match "#{theme_dir}/:theme/images/*asset" => 'themes_for_rails/assets#images',
:as => :base_theme_image, :constraints => { :theme => /.*/ }
:as => :base_theme_image, :constraints => constraints
end

end
Expand Down

0 comments on commit cea54b1

Please sign in to comment.