Skip to content

Commit dd3cd8f

Browse files
committed
accept multiple asset roots as search paths for globbing for assets.
1 parent 8ebe4ca commit dd3cd8f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/jammit.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class << self
5757
:javascript_compressor, :compressor_options, :css_compressor,
5858
:css_compressor_options, :template_extension,
5959
:template_extension_matcher, :allow_debugging,
60-
:rewrite_relative_paths, :public_root
60+
:rewrite_relative_paths, :public_root, :asset_roots
6161
attr_accessor :javascript_compressors, :css_compressors
6262
end
6363

@@ -109,6 +109,7 @@ def self.load_configuration(config, soft=false)
109109
set_template_namespace(conf[:template_namespace])
110110
set_template_extension(conf[:template_extension])
111111
set_public_root(conf[:public_root]) if conf[:public_root]
112+
set_asset_roots(conf[:asset_roots]) if conf[:asset_roots]
112113
symbolize_keys(conf[:stylesheets]) if conf[:stylesheets]
113114
symbolize_keys(conf[:javascripts]) if conf[:javascripts]
114115
check_for_deprecations
@@ -163,6 +164,10 @@ def self.package!(options={})
163164
def self.set_public_root(public_root=nil)
164165
@public_root = public_root if public_root
165166
end
167+
168+
def self.set_asset_roots(*roots)
169+
@asset_roots = [ASSET_ROOT, roots].compact.flatten.uniq
170+
end
166171

167172
# Ensure that the JavaScript compressor is a valid choice.
168173
def self.set_javascript_compressor(value)

lib/jammit/packager.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,21 @@ def package_for(package, extension)
102102
# Absolute globs are absolute -- relative globs are relative to ASSET_ROOT.
103103
# Print a warning if no files were found that match the glob.
104104
def glob_files(glob)
105-
absolute = Pathname.new(glob).absolute?
106-
paths = Dir[absolute ? glob : File.join(ASSET_ROOT, glob)].sort
105+
absolute =
106+
paths = if Pathname.new(glob).absolute?
107+
Dir[glob].sort
108+
else
109+
search_paths = Jammit.asset_roots.map{ |path| File.join(path, glob) }
110+
Dir[*search_paths].sort
111+
end
107112
Jammit.warn("No assets match '#{glob}'") if paths.empty?
108113
paths
109114
end
110115

111116
# In Rails, the difference between a path and an asset URL is "public".
112117
def path_to_url
113-
@path_to_url ||= /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(Jammit.public_root.sub(ASSET_ROOT, ''))})?/
118+
prefix = Jammit.asset_roots.map{ |path| Regexp.escape(path) }.uniq.join("|")
119+
@path_to_url ||= /\A(#{prefix})(\/?#{Regexp.escape(Jammit.public_root.sub(/#{prefix}/, ''))})?/
114120
end
115121

116122
# Get the latest mtime of a list of files (plus the config path).

0 commit comments

Comments
 (0)