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

Add assets.cache_package_paths and unit test. #133

Open
wants to merge 1 commit 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.rvmrc
/.sass-cache
/doc/
*.gem
*.gem
.DS_Store
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ class App < Sinatra::Base
end
```

### assets.cache_package_paths
Cache the list of files matching each package definition.

Defaults to `false` in development, `true` in other environments.

## Helpers

These are helpers you can use in your views.
Expand Down
Binary file added lib/sinatra/.DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions lib/sinatra/assetpack/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ def js_or_css(type, name, *args)
attrib :prebuild # Bool
attrib :cache_dynamic_assets # Bool

def cache_package_paths(*args)
if args.empty?
@cache_package_paths = (app.settings.environment != :development) if @cache_package_paths.nil?
@cache_package_paths
else
@cache_package_paths = args.first
end
end

def expires(*args)
if args.empty?
@expires
Expand Down Expand Up @@ -277,6 +286,9 @@ def files(match=nil)
# glob(['spec1', 'spec2' ...])
#
def glob(match)

@reload_files_cache = true unless cache_package_paths

paths = Array.new(match) # Force array-ness

paths.map! do |spec|
Expand Down
41 changes: 41 additions & 0 deletions test/cache_package_paths_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require File.expand_path('../test_helper', __FILE__)

class CachePackagePathsTest < UnitTest
class App < Main
assets {
serve '/css', :from => 'app/css'
css :a, [ '/css/**/style*.css' ]
}

get('/pkg') { css :a }
end

def app
App
end

test "package contents are not cached in development" do
app.settings.stubs(:environment).returns(:development)
get '/pkg'
assert_equal 200, last_response.status
assert_match %r{css/style\.\w{32}\.css}, body

begin
file = Tempfile.new(['style', '.scss'], File.join(File.dirname(__FILE__), "app", "app", "css"))
file.write("body{color: red;}")
file.flush

get '/pkg'
assert_equal 200, last_response.status
assert_match %r{css/style\.\w{32}\.css}, body
assert_match %r{css/#{File.basename(file.path).chomp(".scss")}\.\w{32}\.css}, body

ensure
unless file == nil
file.close
file.delete
end
end
end

end