Skip to content

Commit

Permalink
Prepare code for asset pipeline. refs lucasefe#54
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jasherai committed Aug 9, 2012
1 parent 828a46f commit 2e56895
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
17 changes: 16 additions & 1 deletion lib/themes_for_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/themes_for_rails/common_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@ksheurs

ksheurs Aug 28, 2012

hi there. im getting errors w/ Rails.application.config.assets.digests being nil. any ideas? i have asset_digests_enabled set to true and am including the gem within the assets group, thanks!

This comment has been minimized.

Copy link
@jasherai

jasherai Aug 28, 2012

Author Owner

The normal reason for this is that you haven't run precompile on your assets yet.
when you precompile the assets a manifest.yml file in public/assets is created which is loaded into Rails.application.config.assets.digests on server startup.

bundle exec rake assets:precompile

http://edgeguides.rubyonrails.org/asset_pipeline.html#precompiling-assets will give you some better background on how this fits together.

Let me know if you are still having issues.... it would be good to get more testing on this code.

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
Expand Down
9 changes: 7 additions & 2 deletions lib/themes_for_rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
end
10 changes: 8 additions & 2 deletions lib/themes_for_rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e56895

Please sign in to comment.