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

Asset Pipeline support refs #54 #63

Open
wants to merge 12 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
16 changes: 9 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ GEM
activesupport (3.0.11)
arel (2.0.10)
builder (2.1.2)
contest (0.1.2)
contest (0.1.3)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
json (1.6.5)
json (1.7.4)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
mocha (0.9.12)
metaclass (0.0.1)
mime-types (1.19)
mocha (0.12.2)
metaclass (~> 0.0.1)
polyglot (0.3.3)
rack (1.2.5)
rack-mount (0.6.14)
Expand All @@ -71,13 +73,13 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
sqlite3 (1.3.5)
test-unit (2.2.0)
sqlite3 (1.3.6)
test-unit (2.5.1)
thor (0.14.6)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.31)
tzinfo (0.3.33)

PLATFORMS
ruby
Expand Down
45 changes: 31 additions & 14 deletions doc/README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ h3. Additional Instructions for using the Rails Asset Pipeline

In order to use themes_for_rails with the asset pipeline, you will need to configure a few settings and place your themes in the asset pipeline directory.

You will need to enable asset digests in your rails app. This is normally enabled in production mode but you can add it to your development environment to
test locally.

<pre>
# config/environments/development.rb

config.assets.digest = true

</pre>

h4. PLEASE ADD themes_for_rails TO THE ASSETS GROUP IN YOUR GEMFILE!
In order to automatically add the themes to your asset pipeline you need to add the themes_for_rails gem to the assets group in your Gemfile.


First, move your assets into the asset pipeline. For example:

<pre>
Expand All @@ -50,9 +64,11 @@ $app_root
stylesheets/ <-- default asset pipeline folder
themes/ <-- your themes root
[theme_name]
images/
stylesheets/
javascripts/
assets/ <-- this is your theme_assets_dir
[theme_name] <-- this ensures that the assets are namespaced and don't clobber eachother.
images/
stylesheets/
javascripts/
views/ <- you can override application views
layouts/ <- layout .rhtml or .liquid templates
</pre>
Expand All @@ -64,8 +80,7 @@ Create an initializer for themes in your {Rails.root}/config/initializers direct
ThemesForRails.config do |config|
#
# If you have placed your themes like the example path above within the asset pipeline:
config.themes_dir = 'assets'
config.assets_dir = 'app/assets/themes'
config.assets_dir = 'app/assets/themes/:name/assets'
# ...
end
</pre>
Expand Down Expand Up @@ -94,9 +109,11 @@ $app_root
stylesheets/ <-- default asset pipeline folder
themes/ <-- your themes root
[theme_name]
images/
stylesheets/
javascripts/
assets/ <-- this is your theme_assets_dir
[theme_name] <-- this ensures that the assets are namespaced and don't clobber eachother.
images/
stylesheets/
javascripts/
views/
themes/ <-- note themes folder lives under views in this scenario
[theme_name]
Expand All @@ -111,7 +128,7 @@ ThemesForRails.config do |config|
#
# If you have placed your themes like the example path above within the asset pipeline:
config.themes_dir = 'assets'
config.assets_dir = 'app/assets/themes'
config.assets_dir = 'app/assets/themes/:name/assets'
config.views_dir = 'app/views/themes'
# ...
end
Expand Down Expand Up @@ -207,15 +224,15 @@ h3. Url Helpers
In your views you should be able to access your assets like this (given the theme 'default' is set):

<pre>
current_theme_image_path('logo.png') # => /themes/default/images/logo.png
current_theme_stylesheet_path('style') # => /themes/default/stylesheets/logo.css
current_theme_javascript_path('app') # => /themes/default/stylesheets/app.js
current_theme_image_path('logo.png') # => /assets/default/images/logo.png
current_theme_stylesheet_path('style') # => /assets/default/stylesheets/logo.css
current_theme_javascript_path('app') # => /assets/default/stylesheets/app.js
</pre>

Or a given theme:

<pre>
current_theme_image_path('logo.png', 'purple') # => /themes/purple/images/logo.png
current_theme_image_path('logo.png', 'purple') # => /assets/purple/images/logo.png
</pre>

In your application views, there are theme specific helper tags
Expand Down Expand Up @@ -360,4 +377,4 @@ h2. Last but not least

If you are using this gem, please, take a minute to recommend me at Working With Rails.

<a href="http://www.workingwithrails.com/recommendation/new/person/7277-lucas-florio"><img alt="Recommend Me" src="http://workingwithrails.com/images/tools/compact-small.jpg" /></a>
<a href="http://www.workingwithrails.com/recommendation/new/person/7277-lucas-florio"><img alt="Recommend Me" src="http://workingwithrails.com/images/tools/compact-small.jpg" /></a>
18 changes: 18 additions & 0 deletions lib/tasks/themes_for_rails.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ namespace :themes do
end
desc "Updates the cached (public) theme folders"
task :update_cache => [:remove_cache, :create_cache]

desc "Add theme view paths to ActionView PathSet"
task :add_theme_to_view_paths => :environment do
raise 'you must provide THEME for theme support to work' unless theme = ENV['THEME']
puts "adding theme [#{theme}] to view path"
require 'themes_for_rails/common_methods'
include ThemesForRails::CommonMethods

theme_view_path=theme_view_path_for(theme)
ActionController::Base.view_paths.paths.prepend(theme_view_path) if theme
end
end

namespace :cache_digests do
puts "patching cache_digests with themes_for_rails support"
# Add themes support to cache_digest gem rake tasks
task :nested_dependencies => ['themes:add_theme_to_view_paths']
task :dependencies => ['themes:add_theme_to_view_paths']
end
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) unless Rails.application.config.assets.paths.include?(theme_asset_path)
end unless ThemesForRails.config.base_dir =~ %r!/app/assets/!
end
end

def already_configured_in_sass?(sass_dir)
Sass::Plugin.template_location_array.map(&:first).include?(sass_dir)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/themes_for_rails/action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ActionMailer
end

def mail_with_theme(headers = {}, &block)
theme_opts = headers[:theme] || self.class.default[:theme]
theme_opts = headers[:theme] || headers['X-theme'] || self.class.default[:theme]
theme(theme_opts) if theme_opts

mail_without_theme(headers, &block)
Expand Down
26 changes: 15 additions & 11 deletions lib/themes_for_rails/action_view.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
module ThemesForRails

module ActionView

extend ActiveSupport::Concern
Expand All @@ -10,35 +10,39 @@ module ActionView
end

def current_theme_stylesheet_path(asset)
base_theme_stylesheet_path(:theme => self.theme_name, :asset => "#{asset}.css")
base_theme_stylesheet_path(:theme => self.theme_name, :asset => digest_for_stylesheet("#{asset}.css", self.theme_name))
end

def current_theme_javascript_path(asset)
base_theme_javascript_path(:theme => self.theme_name, :asset => "#{asset}.js")
base_theme_javascript_path(:theme => self.theme_name, :asset => digest_for_javascript("#{asset}.js", self.theme_name))
end

def current_theme_image_path(asset)
base_theme_image_path(:theme => self.theme_name, :asset => asset)
image, extension = name_ext(asset)
base_theme_image_path(:theme => self.theme_name, :asset => digest_for_image("#{image}.#{extension}", self.theme_name))
end

def theme_stylesheet_path(asset, new_theme_name = self.theme_name)
base_theme_stylesheet_path(:theme => new_theme_name, :asset => "#{asset}.css")
base_theme_stylesheet_path(:theme => new_theme_name, :asset => digest_for_stylesheet("#{asset}.css", new_theme_name))
end

def theme_javascript_path(asset, new_theme_name = self.theme_name)
base_theme_javascript_path(:theme => new_theme_name, :asset => "#{asset}.js")
base_theme_javascript_path(:theme => new_theme_name, :asset => digest_for_javascript("#{asset}.js", new_theme_name))
end

def theme_image_path(asset, new_theme_name = self.theme_name)
base_theme_image_path(:theme => new_theme_name, :asset => asset)
image, extension = name_ext(asset)
base_theme_image_path(:theme => new_theme_name, :asset => digest_for_image("#{image}.#{extension}", new_theme_name))
end

def theme_image_tag(source, options = {})
image_tag(theme_image_path(source), options)
image, extension = name_ext(source)
image_tag(theme_image_path(digest_for_image("#{image}.#{extension}", self.theme_name)), options)
end

def theme_image_submit_tag(source, options = {})
image_submit_tag(theme_image_path(source), options)
image, extension = name_ext(source)
image_submit_tag(theme_image_path(digest_for_image("#{image}.#{extension}", self.theme_name)), options)
end

def theme_javascript_include_tag(*files)
Expand Down
46 changes: 39 additions & 7 deletions lib/themes_for_rails/common_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ module CommonMethods
def theme_name
@cached_theme_name ||= begin
case @theme_name
when Symbol then
when Symbol then
self.respond_to?(@theme_name, true) ? self.send(@theme_name) : @theme_name.to_s
when String then @theme_name
else
nil
end
end
end

def theme_name=(name)
@theme_name = name
end
Expand All @@ -26,23 +26,55 @@ def set_theme(name)
add_theme_view_path
end
end

public

def valid_theme?
!self.theme_name.nil?
end

# will add the view path for the current theme
def add_theme_view_path
add_theme_view_path_for(self.theme_name)
end

# will add the view path for a given theme name
def add_theme_view_path_for(name)
self.view_paths.insert 0, ::ActionView::FileSystemResolver.new(theme_view_path_for(name))
end

def digest_for_image(asset, theme_context)
if ThemesForRails.config.asset_digests_enabled?
asset_paths.digest_for("#{theme_context}/images/#{asset}") || asset
else
asset
end
end

def digest_for_javascript(asset, theme_context)
if ThemesForRails.config.asset_digests_enabled?
asset_paths.digest_for("#{theme_context}/javascripts/#{asset}") || asset
else
asset
end
end

def digest_for_stylesheet(asset, theme_context)
if ThemesForRails.config.asset_digests_enabled?
#Rails.application.config.assets.digests["#{theme_context}/stylesheets/#{asset}"] || asset
asset_paths.digest_for("#{theme_context}/stylesheets/#{asset}") || asset
else
asset
end
end

def name_ext(file_name)
ext = File.extname(file_name)
name = File.basename(file_name, ext)
ext.slice!(0) if ext.length > 0
return name, ext
end

def public_theme_path
theme_view_path("/")
end
Expand All @@ -63,4 +95,4 @@ def theme_asset_path_for(theme_name)
interpolate(ThemesForRails.config.assets_dir, theme_name)
end
end
end
end
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
Loading