Permalink
Browse files

Push index cache upstream to environment

  • Loading branch information...
1 parent ba9e113 commit 262380ca9c095bd4307c2babf9283b3a874e9847 @josh josh committed Nov 2, 2011
Showing with 33 additions and 13 deletions.
  1. +1 −10 lib/sprockets/environment.rb
  2. +13 −2 lib/sprockets/index.rb
  3. +1 −1 test/test_asset.rb
  4. +18 −0 test/test_caching.rb
@@ -72,21 +72,12 @@ def find_asset(path, options = {})
if (asset = @assets[cache_key_for(path, options)]) && asset.fresh?(self)
asset
elsif asset = index.find_asset(path, options)
- @assets[cache_key_for(path, options)] = @assets[cache_key_for(asset.pathname, options)] = asset
+ # Cache is pushed upstream by Index#find_asset
asset
end
end
protected
- # TODO: Review Environment caching. This method doesn't seem to be called.
- # Cache asset building in persisted cache.
- def build_asset(path, pathname, options)
- # Persisted cache
- cache_asset(cache_key_for(pathname, options)) do
- super
- end
- end
-
def expire_index!
# Clear digest to be recomputed
@digest = nil
View
@@ -12,6 +12,8 @@ module Sprockets
# `Environment#index`.
class Index < Base
def initialize(environment)
+ @environment = environment
+
# Copy environment attributes
@logger = environment.logger
@context_class = environment.context_class
@@ -52,8 +54,17 @@ def find_asset(path, options = {})
if asset = @assets[cache_key_for(path, options)]
asset
elsif asset = super
- # Cache at logical path
- @assets[cache_key_for(path, options)] = asset
+ logical_path_cache_key = cache_key_for(path, options)
+ full_path_cache_key = cache_key_for(asset.pathname, options)
+
+ # Cache on Index
+ @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
+
+ # Push cache upstream to Environment
+ @environment.instance_eval do
+ @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
+ end
+
asset
end
end
View
@@ -654,7 +654,7 @@ def setup
test "can't require absolute files outside the load path" do
assert_raise Sprockets::FileOutsidePaths do
- warn asset("absolute/require_outside_path.js").to_s
+ asset("absolute/require_outside_path.js").to_s
end
end
View
@@ -83,6 +83,24 @@ def setup
assert !asset1.equal?(asset2)
end
+ test "depedencies are cached" do
+ env = @env1
+
+ parent = env['application.js']
+ assert parent
+
+ child1 = parent.to_a[0]
+
+ assert child1
+ assert_equal 'project.js', child1.logical_path
+
+ child2 = env.find_asset(child1.pathname, :bundle => false)
+ assert child2
+
+ assert child1.equal?(child2)
+ assert child2.equal?(child1)
+ end
+
test "proccessed and bundled assets are cached separately" do
env = @env1
assert_kind_of Sprockets::ProcessedAsset, env.find_asset('gallery.js', :bundle => false)

0 comments on commit 262380c

Please sign in to comment.