Skip to content
This repository was archived by the owner on Jun 10, 2018. It is now read-only.

Commit 93e5103

Browse files
committed
Pass absolute path to each_logical_path filter block
1 parent 5bef8bb commit 93e5103

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/sprockets/base.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,31 +400,35 @@ def circular_call_protection(path)
400400
def logical_path_for_filename(filename, filters)
401401
logical_path = attributes_for(filename).logical_path.to_s
402402

403-
if matches_filter(filters, logical_path)
403+
if matches_filter(filters, logical_path, filename)
404404
return logical_path
405405
end
406406

407407
# If filename is an index file, retest with alias
408408
if File.basename(logical_path)[/[^\.]+/, 0] == 'index'
409409
path = logical_path.sub(/\/index\./, '.')
410-
if matches_filter(filters, path)
410+
if matches_filter(filters, path, filename)
411411
return path
412412
end
413413
end
414414

415415
nil
416416
end
417417

418-
def matches_filter(filters, filename)
418+
def matches_filter(filters, logical_path, filename)
419419
return true if filters.empty?
420420

421421
filters.any? do |filter|
422422
if filter.is_a?(Regexp)
423-
filter.match(filename)
423+
filter.match(logical_path)
424424
elsif filter.respond_to?(:call)
425-
filter.call(filename)
425+
if filter.arity == 1
426+
filter.call(logical_path)
427+
else
428+
filter.call(logical_path, filename.to_s)
429+
end
426430
else
427-
File.fnmatch(filter.to_s, filename)
431+
File.fnmatch(filter.to_s, logical_path)
428432
end
429433
end
430434
end

test/test_environment.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,17 @@ def self.test(name, &block)
311311
assert !paths.include?("gallery.css")
312312
end
313313

314+
test "iterate over each logical path matching proc filters with full path arg" do
315+
paths = []
316+
@env.each_logical_path(proc { |_, fn| fn.match(fixture_path('default/mobile')) }) do |logical_path|
317+
paths << logical_path
318+
end
319+
320+
assert paths.include?("mobile/a.js")
321+
assert paths.include?("mobile/b.js")
322+
assert !paths.include?("application.js")
323+
end
324+
314325
test "CoffeeScript files are compiled in a closure" do
315326
script = @env["coffee"].to_s
316327
assert_equal "undefined", ExecJS.exec(script)

0 commit comments

Comments
 (0)