From 4dc317241177bf231c0339d5ee160fc8b2fbbb54 Mon Sep 17 00:00:00 2001 From: Pritesh Date: Thu, 9 Aug 2012 12:57:35 +0100 Subject: [PATCH] Prepare code for asset pipeline. refs #54 * check for asset digest and store in TFR config * If asset pipeline, use theme_assets_dir config and PREPEND to config.asset.paths. This requires theme assets to be in a folder under the theme_name so as not to clobber other themes. * When gathering digested assets, fallback to the original asset if no digested one is available --- lib/themes_for_rails.rb | 17 ++++++++++++++++- lib/themes_for_rails/common_methods.rb | 12 ++++++------ lib/themes_for_rails/config.rb | 9 +++++++-- lib/themes_for_rails/railtie.rb | 10 ++++++++-- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/themes_for_rails.rb b/lib/themes_for_rails.rb index 71c1790..f72d26a 100644 --- a/lib/themes_for_rails.rb +++ b/lib/themes_for_rails.rb @@ -35,7 +35,22 @@ def add_themes_path_to_sass raise "Sass is not available. What are you trying to do?" end end - + + def check_asset_pipeline + config.asset_digests_enabled ||= Rails.application.config.respond_to?(:assets) && Rails.application.config.assets.digest == true + end + + def add_themes_assets_to_asset_pipeline + Rails.logger.info "Start adding themes to assets [#{ThemesForRails.config.asset_digests_enabled?}]" + if ThemesForRails.config.asset_digests_enabled? + available_theme_names.each do |theme_name| + theme_asset_path = ThemesForRails.config.assets_dir.gsub(":root", ThemesForRails.config.base_dir).gsub(":name", theme_name.to_s) + Rails.logger.info "== Adding theme [#{theme_name}] asset dir [#{theme_asset_path}] to asset pipeline" + Rails.application.config.assets.paths.prepend(theme_asset_path) + end + end + end + def already_configured_in_sass?(sass_dir) Sass::Plugin.template_location_array.map(&:first).include?(sass_dir) end diff --git a/lib/themes_for_rails/common_methods.rb b/lib/themes_for_rails/common_methods.rb index 492b671..f91b60e 100644 --- a/lib/themes_for_rails/common_methods.rb +++ b/lib/themes_for_rails/common_methods.rb @@ -44,24 +44,24 @@ def add_theme_view_path_for(name) end def digest_for_image(asset, theme_context) - if Rails.application.config.respond_to?('assets') && Rails.application.config.assets.digests - File.basename(Rails.application.config.assets.digests["#{theme_context}/images/#{asset}"]) + if ThemesForRails.config.asset_digests_enabled? + Rails.application.config.assets.digests["#{theme_context}/images/#{asset}"] || asset else asset end end def digest_for_javascript(asset, theme_context) - if Rails.application.config.respond_to?('assets') && Rails.application.config.assets.digests - File.basename(Rails.application.config.assets.digests["#{theme_context}/javascripts/#{asset}"]) + if ThemesForRails.config.asset_digests_enabled? + Rails.application.config.assets.digests["#{theme_context}/javascripts/#{asset}"] || asset else asset end end def digest_for_stylesheet(asset, theme_context) - if Rails.application.config.respond_to?('assets') && Rails.application.config.assets.digests - File.basename(Rails.application.config.assets.digests["#{theme_context}/stylesheets/#{asset}"]) + if ThemesForRails.config.asset_digests_enabled? + Rails.application.config.assets.digests["#{theme_context}/stylesheets/#{asset}"] || asset else asset end diff --git a/lib/themes_for_rails/config.rb b/lib/themes_for_rails/config.rb index ad0360d..9a6064f 100644 --- a/lib/themes_for_rails/config.rb +++ b/lib/themes_for_rails/config.rb @@ -3,13 +3,14 @@ module ThemesForRails class Config attr_writer :base_dir, :themes_dir, :assets_dir, :views_dir, :themes_routes_dir - attr_accessor :use_sass, :default_theme + attr_accessor :use_sass, :default_theme, :asset_digests_enabled include Interpolation def initialize(&block) @use_sass = true @default_theme = 'default' + @asset_digests_enabled = nil yield if block_given? end @@ -70,5 +71,9 @@ def use_sass? def sass_is_available? !!defined?Sass::Plugin end + + def asset_digests_enabled? + @asset_digests_enabled + end end -end \ No newline at end of file +end diff --git a/lib/themes_for_rails/railtie.rb b/lib/themes_for_rails/railtie.rb index 276446d..717b64f 100644 --- a/lib/themes_for_rails/railtie.rb +++ b/lib/themes_for_rails/railtie.rb @@ -9,9 +9,15 @@ class Railtie < ::Rails::Railtie ThemesForRails.config.send "#{key}=".to_sym, value end - # Adding theme stylesheets path to sass, automatically. + # Adding theme stylesheets path to sass, automatically. ThemesForRails.add_themes_path_to_sass if ThemesForRails.config.use_sass? - + + # Check if asset pipeline enabled + ThemesForRails.check_asset_pipeline + + # Adding theme assets to the asset pipeline, automatically. + ThemesForRails.add_themes_assets_to_asset_pipeline if ThemesForRails.config.asset_digests_enabled? + ActiveSupport.on_load(:action_view) do include ThemesForRails::ActionView end