sprockets-helpers 0.7.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +41 -5
- data/lib/sprockets/helpers.rb +33 -1
- data/lib/sprockets/helpers/expanded_asset_paths.rb +17 -0
- data/lib/sprockets/helpers/version.rb +1 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/sprockets-helpers_spec.rb +89 -0
- metadata +15 -14
data/README.md
CHANGED
@@ -35,13 +35,13 @@ class App < Sinatra::Base
|
|
35
35
|
set :sprockets, Sprockets::Environment.new(root)
|
36
36
|
set :assets_prefix, '/assets'
|
37
37
|
set :digest_assets, false
|
38
|
-
|
38
|
+
|
39
39
|
configure do
|
40
40
|
# Setup Sprockets
|
41
41
|
sprockets.append_path File.join(root, 'assets', 'stylesheets')
|
42
42
|
sprockets.append_path File.join(root, 'assets', 'javascripts')
|
43
43
|
sprockets.append_path File.join(root, 'assets', 'images')
|
44
|
-
|
44
|
+
|
45
45
|
# Configure Sprockets::Helpers (if necessary)
|
46
46
|
Sprockets::Helpers.configure do |config|
|
47
47
|
config.environment = sprockets
|
@@ -50,17 +50,17 @@ class App < Sinatra::Base
|
|
50
50
|
config.public_path = public_folder
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
helpers do
|
55
55
|
include Sprockets::Helpers
|
56
|
-
|
56
|
+
|
57
57
|
# Alternative method for telling Sprockets::Helpers which
|
58
58
|
# Sprockets environment to use.
|
59
59
|
# def assets_environment
|
60
60
|
# settings.sprockets
|
61
61
|
# end
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
get '/' do
|
65
65
|
erb :index
|
66
66
|
end
|
@@ -123,6 +123,42 @@ Would become:
|
|
123
123
|
</html>
|
124
124
|
```
|
125
125
|
|
126
|
+
Even better, you can use #javascript_tag and #stylesheet_tag directly, which optionally handle the expansion of assets for debugging like Rails:
|
127
|
+
|
128
|
+
``` html+erb
|
129
|
+
<!doctype html>
|
130
|
+
<html lang="en">
|
131
|
+
<head>
|
132
|
+
<meta charset="utf-8">
|
133
|
+
<title>Sinatra with Sprockets 2 (Asset Pipeline)</title>
|
134
|
+
<%= stylesheet_tag 'application' %>
|
135
|
+
<%= javascript_tag 'application', :expand => true %>
|
136
|
+
</head>
|
137
|
+
<body>
|
138
|
+
<img src="<%= image_path 'rails.png' %>">
|
139
|
+
</body>
|
140
|
+
</html>
|
141
|
+
```
|
142
|
+
|
143
|
+
Would become:
|
144
|
+
|
145
|
+
``` html
|
146
|
+
<!doctype html>
|
147
|
+
<html lang="en">
|
148
|
+
<head>
|
149
|
+
<meta charset="utf-8">
|
150
|
+
<title>Sinatra with Sprockets 2 (Asset Pipeline)</title>
|
151
|
+
<link rel="stylesheet" href="/assets/application.css">
|
152
|
+
<script src="/assets/jquery.js?body=1"></script>
|
153
|
+
<script src="/assets/jquery.ui.js?body=1"></script>
|
154
|
+
<script src="/assets/application.js?body=1"></script>
|
155
|
+
</head>
|
156
|
+
<body>
|
157
|
+
<img src="/assets/rails.png">
|
158
|
+
</body>
|
159
|
+
</html>
|
160
|
+
```
|
161
|
+
|
126
162
|
|
127
163
|
Fallback to Public Directory
|
128
164
|
----------------------------
|
data/lib/sprockets/helpers.rb
CHANGED
@@ -2,6 +2,7 @@ require 'sprockets'
|
|
2
2
|
require 'sprockets/helpers/version'
|
3
3
|
require 'sprockets/helpers/base_path'
|
4
4
|
require 'sprockets/helpers/asset_path'
|
5
|
+
require 'sprockets/helpers/expanded_asset_paths'
|
5
6
|
require 'sprockets/helpers/file_path'
|
6
7
|
require 'sprockets/helpers/manifest_path'
|
7
8
|
require 'uri'
|
@@ -15,6 +16,9 @@ module Sprockets
|
|
15
16
|
# When true, the asset paths will return digest paths.
|
16
17
|
attr_accessor :digest
|
17
18
|
|
19
|
+
# When true, expand assets.
|
20
|
+
attr_accessor :expand
|
21
|
+
|
18
22
|
# Set the Sprockets environment to search for assets.
|
19
23
|
# This defaults to the context's #environment method.
|
20
24
|
attr_accessor :environment
|
@@ -120,8 +124,14 @@ module Sprockets
|
|
120
124
|
|
121
125
|
# If the source points to an asset in the Sprockets
|
122
126
|
# environment use AssetPath to generate the full path.
|
127
|
+
# With expand, return array of paths of assets required by asset.
|
123
128
|
assets_environment.resolve(uri.path) do |path|
|
124
|
-
|
129
|
+
asset = assets_environment[path]
|
130
|
+
if options[:expand]
|
131
|
+
return Helpers::ExpandedAssetPaths.new(uri, asset, options).to_a
|
132
|
+
else
|
133
|
+
return Helpers::AssetPath.new(uri, asset, options).to_s
|
134
|
+
end
|
125
135
|
end
|
126
136
|
|
127
137
|
# Use FilePath for normal files on the file system
|
@@ -129,6 +139,28 @@ module Sprockets
|
|
129
139
|
end
|
130
140
|
alias_method :path_to_asset, :asset_path
|
131
141
|
|
142
|
+
def asset_tag(source, options = {}, &block)
|
143
|
+
raise ::ArgumentError, 'block missing' unless block
|
144
|
+
path = asset_path source, { :expand => Helpers.expand }.merge(options)
|
145
|
+
if options[:expand] && path.kind_of?(Array)
|
146
|
+
return path.map(&block).join()
|
147
|
+
else
|
148
|
+
yield path
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def javascript_tag(source, options = {})
|
153
|
+
asset_tag(source, { :ext => 'js' }.merge(options)) do |path|
|
154
|
+
%Q(<script src="#{path}"></script>)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def stylesheet_tag(source, options = {})
|
159
|
+
asset_tag(source, { :ext => 'css' }.merge(options)) do |path|
|
160
|
+
%Q(<link rel="stylesheet" href="#{path}">)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
132
164
|
# Computes the path to a audio asset either in the Sprockets environment
|
133
165
|
# or the public directory. External URIs are untouched.
|
134
166
|
#
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Helpers
|
3
|
+
class ExpandedAssetPaths
|
4
|
+
def initialize(uri, asset, options = {})
|
5
|
+
@uri = uri
|
6
|
+
@asset = asset
|
7
|
+
@options = { :body => true }.merge options
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_a
|
11
|
+
@asset.to_a.map do |dependency|
|
12
|
+
AssetPath.new(@uri.clone, dependency, @options).to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,4 +23,26 @@ RSpec.configure do |config|
|
|
23
23
|
pathname ||= Pathname.new(File.join('assets', logical_path)).expand_path
|
24
24
|
env.context_class.new env, logical_path, pathname
|
25
25
|
end
|
26
|
+
|
27
|
+
# Exemplary file system layout for usage in test-construct
|
28
|
+
def assets_layout(construct)
|
29
|
+
lambda { |c|
|
30
|
+
c.file('assets/main.js') do |f|
|
31
|
+
f << "//= require a\n"
|
32
|
+
f << "//= require b\n"
|
33
|
+
end
|
34
|
+
c.file('assets/a.js')
|
35
|
+
c.file('assets/b.js')
|
36
|
+
|
37
|
+
c.file('assets/main.css') do |f|
|
38
|
+
f << "/*\n"
|
39
|
+
f << "*= require a\n"
|
40
|
+
f << "*= require b\n"
|
41
|
+
f << "*/\n"
|
42
|
+
end
|
43
|
+
c.file('assets/a.css')
|
44
|
+
c.file('assets/b.css')
|
45
|
+
}.call(construct)
|
46
|
+
end
|
47
|
+
|
26
48
|
end
|
@@ -371,4 +371,93 @@ describe Sprockets::Helpers do
|
|
371
371
|
end
|
372
372
|
end
|
373
373
|
end
|
374
|
+
|
375
|
+
describe '#asset_tag' do
|
376
|
+
it 'receives block to generate tag' do
|
377
|
+
actual = context.asset_tag('main.js') { |path| "<script src=#{path}></script>" }
|
378
|
+
actual.should == '<script src=/main.js></script>'
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'raises when called without block' do
|
382
|
+
expect { context.asset_tag('main.js') }.to raise_error(ArgumentError, "block missing")
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'expands when configured' do
|
386
|
+
within_construct do |construct|
|
387
|
+
assets_layout(construct)
|
388
|
+
Sprockets::Helpers.expand = true
|
389
|
+
c = context
|
390
|
+
c.stub(:asset_path).and_return(context.asset_path('main.js')) # Spy
|
391
|
+
c.should_receive(:asset_path).with('main.js', {:expand => true})
|
392
|
+
c.asset_tag('main.js') {}
|
393
|
+
Sprockets::Helpers.expand = false
|
394
|
+
c.should_receive(:asset_path).with('main.js', {:expand => false})
|
395
|
+
c.asset_tag('main.js') {}
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
describe 'when expanding' do
|
400
|
+
it 'passes uri that is no asset untouched' do
|
401
|
+
context.asset_tag('main.js', :expand => true) {}
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'generates tag for each asset' do
|
405
|
+
within_construct do |construct|
|
406
|
+
assets_layout(construct)
|
407
|
+
tags = context.asset_tag('main.js', :expand => true) do |path|
|
408
|
+
"<script src=\"#{path}\"></script>"
|
409
|
+
end
|
410
|
+
tags.split("</script>").should have(3).scripts
|
411
|
+
tags.should include('<script src="/assets/main.js?body=1"></script>')
|
412
|
+
tags.should include('<script src="/assets/a.js?body=1"></script>')
|
413
|
+
tags.should include('<script src="/assets/b.js?body=1"></script>')
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
describe '#javascript_tag' do
|
421
|
+
it 'generates script tag' do
|
422
|
+
context.javascript_tag('main.js').should == '<script src="/main.js"></script>'
|
423
|
+
end
|
424
|
+
|
425
|
+
it 'appends extension' do
|
426
|
+
context.javascript_tag('main').should == '<script src="/main.js"></script>'
|
427
|
+
end
|
428
|
+
|
429
|
+
describe 'when expanding' do
|
430
|
+
it 'generates script tag for each javascript asset' do
|
431
|
+
within_construct do |construct|
|
432
|
+
assets_layout(construct)
|
433
|
+
tags = context.javascript_tag('main.js', :expand => true)
|
434
|
+
tags.should include('<script src="/assets/main.js?body=1"></script>')
|
435
|
+
tags.should include('<script src="/assets/a.js?body=1"></script>')
|
436
|
+
tags.should include('<script src="/assets/b.js?body=1"></script>')
|
437
|
+
end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
describe '#stylesheet_tag' do
|
443
|
+
it 'generates stylesheet tag' do
|
444
|
+
context.stylesheet_tag('main.css').should == '<link rel="stylesheet" href="/main.css">'
|
445
|
+
end
|
446
|
+
|
447
|
+
it 'generates stylesheet tag' do
|
448
|
+
context.stylesheet_tag('main').should == '<link rel="stylesheet" href="/main.css">'
|
449
|
+
end
|
450
|
+
|
451
|
+
describe 'when expanding' do
|
452
|
+
it 'generates stylesheet tag for each stylesheet asset' do
|
453
|
+
within_construct do |construct|
|
454
|
+
assets_layout(construct)
|
455
|
+
tags = context.stylesheet_tag('main.css', :expand => true)
|
456
|
+
tags.should include('<link rel="stylesheet" href="/assets/main.css?body=1">')
|
457
|
+
tags.should include('<link rel="stylesheet" href="/assets/a.css?body=1">')
|
458
|
+
tags.should include('<link rel="stylesheet" href="/assets/b.css?body=1">')
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end
|
462
|
+
end
|
374
463
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sprockets
|
16
|
-
requirement: &
|
16
|
+
requirement: &70280862449120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70280862449120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: appraisal
|
27
|
-
requirement: &
|
27
|
+
requirement: &70280862448600 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0.4'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70280862448600
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70280862448100 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '2.6'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70280862448100
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: test-construct
|
49
|
-
requirement: &
|
49
|
+
requirement: &70280862447080 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '1.2'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70280862447080
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &70280862446380 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70280862446380
|
69
69
|
description: Asset path helpers for Sprockets 2.x applications
|
70
70
|
email:
|
71
71
|
- me@petebrowne.com
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/sprockets/helpers.rb
|
91
91
|
- lib/sprockets/helpers/asset_path.rb
|
92
92
|
- lib/sprockets/helpers/base_path.rb
|
93
|
+
- lib/sprockets/helpers/expanded_asset_paths.rb
|
93
94
|
- lib/sprockets/helpers/file_path.rb
|
94
95
|
- lib/sprockets/helpers/manifest_path.rb
|
95
96
|
- lib/sprockets/helpers/version.rb
|
@@ -110,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
111
|
version: '0'
|
111
112
|
segments:
|
112
113
|
- 0
|
113
|
-
hash: -
|
114
|
+
hash: -4303612460519246856
|
114
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
116
|
none: false
|
116
117
|
requirements:
|
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
segments:
|
121
122
|
- 0
|
122
|
-
hash: -
|
123
|
+
hash: -4303612460519246856
|
123
124
|
requirements: []
|
124
125
|
rubyforge_project: sprockets-helpers
|
125
126
|
rubygems_version: 1.8.11
|