From bc7b7e3d27f955ea7a3149b2d05f769cbe1e7e88 Mon Sep 17 00:00:00 2001 From: Pritesh Date: Thu, 9 Aug 2012 13:15:24 +0100 Subject: [PATCH] Make use of the asset pipeline. refs #54 * Load and override action_view when asset digests are enabled. * don't load asset_controller routes when asset pipeline is in use. --- lib/themes_for_rails/digested_action_view.rb | 65 ++++++++++++++++++++ lib/themes_for_rails/railtie.rb | 4 ++ lib/themes_for_rails/routes.rb | 15 +++-- 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 lib/themes_for_rails/digested_action_view.rb diff --git a/lib/themes_for_rails/digested_action_view.rb b/lib/themes_for_rails/digested_action_view.rb new file mode 100644 index 0000000..880c750 --- /dev/null +++ b/lib/themes_for_rails/digested_action_view.rb @@ -0,0 +1,65 @@ +# encoding: utf-8 +module ThemesForRails + + module DigestedActionView + + extend ActiveSupport::Concern + + included do + include ThemesForRails::CommonMethods + end + + def current_theme_stylesheet_path(asset) + digest_for_stylesheet("#{asset}.css", self.theme_name) + end + + def current_theme_javascript_path(asset) + digest_for_javascript("#{asset}.js", self.theme_name) + end + + def current_theme_image_path(asset) + image, extension = name_ext(asset) + digest_for_image("#{image}.#{extension}", self.theme_name) + end + + def theme_stylesheet_path(asset, new_theme_name = self.theme_name) + digest_for_stylesheet("#{asset}.css", new_theme_name) + end + + def theme_javascript_path(asset, new_theme_name = self.theme_name) + digest_for_javascript("#{asset}.js", new_theme_name) + end + + def theme_image_path(asset, new_theme_name = self.theme_name) + image, extension = name_ext(asset) + digest_for_image("#{image}.#{extension}", new_theme_name) + end + + def theme_image_tag(source, options = {}) + image_tag(theme_image_path("#{source}", self.theme_name), options) + end + + def theme_image_submit_tag(source, options = {}) + image, extension = name_ext(source) + image_submit_tag(theme_image_path("#{image}.#{extension}", self.theme_name), options) + end + + def theme_javascript_include_tag(*files) + options = files.extract_options! + options.merge!({ :type => "text/javascript" }) + files_with_options = files.collect {|file| theme_javascript_path(file) } + files_with_options += [options] + + javascript_include_tag(*files_with_options) + end + + def theme_stylesheet_link_tag(*files) + options = files.extract_options! + options.merge!({ :type => "text/css" }) + files_with_options = files.collect {|file| theme_stylesheet_path(file) } + files_with_options += [options] + + stylesheet_link_tag(*files_with_options) + end + end +end diff --git a/lib/themes_for_rails/railtie.rb b/lib/themes_for_rails/railtie.rb index 717b64f..bc98bc7 100644 --- a/lib/themes_for_rails/railtie.rb +++ b/lib/themes_for_rails/railtie.rb @@ -20,6 +20,10 @@ class Railtie < ::Rails::Railtie ActiveSupport.on_load(:action_view) do include ThemesForRails::ActionView + if ThemesForRails.config.asset_digests_enabled? + require 'themes_for_rails/digested_action_view' + include ThemesForRails::DigestedActionView + end end ActiveSupport.on_load(:action_controller) do diff --git a/lib/themes_for_rails/routes.rb b/lib/themes_for_rails/routes.rb index d52301b..192848e 100644 --- a/lib/themes_for_rails/routes.rb +++ b/lib/themes_for_rails/routes.rb @@ -6,12 +6,15 @@ def themes_for_rails theme_dir = ThemesForRails.config.themes_routes_dir constraints = { :theme => /[\w\.]*/ } - match "#{theme_dir}/:theme/stylesheets/*asset" => 'themes_for_rails/assets#stylesheets', - :as => :base_theme_stylesheet, :constraints => constraints - match "#{theme_dir}/:theme/javascripts/*asset" => 'themes_for_rails/assets#javascripts', - :as => :base_theme_javascript, :constraints => constraints - match "#{theme_dir}/:theme/images/*asset" => 'themes_for_rails/assets#images', - :as => :base_theme_image, :constraints => constraints + # Lets not pollute the routes if they aren't being used. + unless ThemesForRails.config.asset_digests_enabled? + match "#{theme_dir}/:theme/stylesheets/*asset" => 'themes_for_rails/assets#stylesheets', + :as => :base_theme_stylesheet, :constraints => constraints + match "#{theme_dir}/:theme/javascripts/*asset" => 'themes_for_rails/assets#javascripts', + :as => :base_theme_javascript, :constraints => constraints + match "#{theme_dir}/:theme/images/*asset" => 'themes_for_rails/assets#images', + :as => :base_theme_image, :constraints => constraints + end end end