sprockets-helpers 0.8.0 → 0.9.1

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.
@@ -2,7 +2,6 @@ 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'
6
5
  require 'sprockets/helpers/file_path'
7
6
  require 'sprockets/helpers/manifest_path'
8
7
  require 'uri'
@@ -49,6 +48,20 @@ module Sprockets
49
48
  end
50
49
  attr_writer :public_path
51
50
 
51
+ # The default options for each asset path method. This is where you
52
+ # can change your default directories for the fallback directory.
53
+ def default_path_options
54
+ @default_path_options ||= {
55
+ :audio_path => { :dir => 'audios' },
56
+ :font_path => { :dir => 'fonts' },
57
+ :image_path => { :dir => 'images' },
58
+ :javascript_path => { :dir => 'javascripts', :ext => 'js' },
59
+ :stylesheet_path => { :dir => 'stylesheets', :ext => 'css' },
60
+ :video_path => { :dir => 'videos' }
61
+ }
62
+ end
63
+ attr_writer :default_path_options
64
+
52
65
  # Convience method for configuring Sprockets::Helpers.
53
66
  def configure
54
67
  yield self
@@ -99,50 +112,33 @@ module Sprockets
99
112
  #
100
113
  def asset_path(source, options = {})
101
114
  uri = URI.parse(source)
102
-
103
- # Return fast if the URI is absolute
104
115
  return source if uri.absolute?
105
116
 
106
- # When debugging return paths without digests, asset_hosts, etc.
107
117
  if options[:debug]
108
- options[:manifest] = false
109
- options[:digest] = false
118
+ options[:manifest] = false
119
+ options[:digest] = false
110
120
  options[:asset_host] = false
111
121
  end
112
122
 
113
- # Append extension if necessary
114
123
  source_ext = File.extname(source)
115
124
  if options[:ext] && source_ext != ".#{options[:ext]}"
116
125
  uri.path << ".#{options[:ext]}"
117
126
  end
118
127
 
119
- # If a manifest is present, try to grab the path from the manifest first
120
- # Don't try looking in manifest in the first place if we're not looking for digest version
121
- if options[:manifest] != false && Helpers.manifest && Helpers.manifest.assets[uri.path]
122
- return Helpers::ManifestPath.new(uri, Helpers.manifest.assets[uri.path], options).to_s
123
- end
124
-
125
- # If the source points to an asset in the Sprockets
126
- # environment use AssetPath to generate the full path.
127
- # With expand, return array of paths of assets required by asset.
128
- assets_environment.resolve(uri.path) do |path|
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
128
+ path = find_asset_path(uri, options)
129
+ if options[:expand] && path.respond_to?(:to_a)
130
+ path.to_a
131
+ else
132
+ path.to_s
135
133
  end
136
-
137
- # Use FilePath for normal files on the file system
138
- return Helpers::FilePath.new(uri, options).to_s
139
134
  end
140
135
  alias_method :path_to_asset, :asset_path
141
136
 
142
137
  def asset_tag(source, options = {}, &block)
143
138
  raise ::ArgumentError, 'block missing' unless block
144
- path = asset_path source, { :expand => Helpers.expand }.merge(options)
145
- if options[:expand] && path.kind_of?(Array)
139
+ options = { :expand => Helpers.expand }.merge(options)
140
+ path = asset_path source, options
141
+ if options[:expand] && path.respond_to?(:map)
146
142
  return path.map(&block).join()
147
143
  else
148
144
  yield path
@@ -150,13 +146,15 @@ module Sprockets
150
146
  end
151
147
 
152
148
  def javascript_tag(source, options = {})
153
- asset_tag(source, { :ext => 'js' }.merge(options)) do |path|
149
+ options = Helpers.default_path_options[:javascript_path].merge(options)
150
+ asset_tag(source, options) do |path|
154
151
  %Q(<script src="#{path}"></script>)
155
152
  end
156
153
  end
157
154
 
158
155
  def stylesheet_tag(source, options = {})
159
- asset_tag(source, { :ext => 'css' }.merge(options)) do |path|
156
+ options = Helpers.default_path_options[:stylesheet_path].merge(options)
157
+ asset_tag(source, options) do |path|
160
158
  %Q(<link rel="stylesheet" href="#{path}">)
161
159
  end
162
160
  end
@@ -181,7 +179,7 @@ module Sprockets
181
179
  # audio_path 'http://www.example.com/img/audio.mp3' # => 'http://www.example.com/img/audio.mp3'
182
180
  #
183
181
  def audio_path(source, options = {})
184
- asset_path source, { :dir => 'audios' }.merge(options)
182
+ asset_path source, Helpers.default_path_options[:audio_path].merge(options)
185
183
  end
186
184
  alias_method :path_to_audio, :audio_path
187
185
 
@@ -205,7 +203,7 @@ module Sprockets
205
203
  # font_path 'http://www.example.com/img/font.ttf' # => 'http://www.example.com/img/font.ttf'
206
204
  #
207
205
  def font_path(source, options = {})
208
- asset_path source, { :dir => 'fonts' }.merge(options)
206
+ asset_path source, Helpers.default_path_options[:font_path].merge(options)
209
207
  end
210
208
  alias_method :path_to_font, :font_path
211
209
 
@@ -229,7 +227,7 @@ module Sprockets
229
227
  # image_path 'http://www.example.com/img/edit.png' # => 'http://www.example.com/img/edit.png'
230
228
  #
231
229
  def image_path(source, options = {})
232
- asset_path source, { :dir => 'images' }.merge(options)
230
+ asset_path source, Helpers.default_path_options[:image_path].merge(options)
233
231
  end
234
232
  alias_method :path_to_image, :image_path
235
233
 
@@ -254,7 +252,7 @@ module Sprockets
254
252
  # javascript_path 'http://www.example.com/js/xmlhr.js' # => 'http://www.example.com/js/xmlhr.js'
255
253
  #
256
254
  def javascript_path(source, options = {})
257
- asset_path source, { :dir => 'javascripts', :ext => 'js' }.merge(options)
255
+ asset_path source, Helpers.default_path_options[:javascript_path].merge(options)
258
256
  end
259
257
  alias_method :path_to_javascript, :javascript_path
260
258
 
@@ -279,7 +277,7 @@ module Sprockets
279
277
  # stylesheet_path 'http://www.example.com/css/style.css' # => 'http://www.example.com/css/style.css'
280
278
  #
281
279
  def stylesheet_path(source, options = {})
282
- asset_path source, { :dir => 'stylesheets', :ext => 'css' }.merge(options)
280
+ asset_path source, Helpers.default_path_options[:stylesheet_path].merge(options)
283
281
  end
284
282
  alias_method :path_to_stylesheet, :stylesheet_path
285
283
 
@@ -303,7 +301,7 @@ module Sprockets
303
301
  # video_path 'http://www.example.com/img/video.mp4' # => 'http://www.example.com/img/video.mp4'
304
302
  #
305
303
  def video_path(source, options = {})
306
- asset_path source, { :dir => 'videos' }.merge(options)
304
+ asset_path source, Helpers.default_path_options[:video_path].merge(options)
307
305
  end
308
306
  alias_method :path_to_video, :video_path
309
307
 
@@ -316,6 +314,19 @@ module Sprockets
316
314
  def assets_environment
317
315
  Helpers.environment || environment
318
316
  end
317
+
318
+ def find_asset_path(uri, options = {})
319
+ if Helpers.manifest && options[:manifest] != false
320
+ manifest_path = Helpers.manifest.assets[uri.path]
321
+ return Helpers::ManifestPath.new(uri, manifest_path, options) if manifest_path
322
+ end
323
+
324
+ assets_environment.resolve(uri.path) do |path|
325
+ return Helpers::AssetPath.new(uri, assets_environment[path], options)
326
+ end
327
+
328
+ return Helpers::FilePath.new(uri, options)
329
+ end
319
330
  end
320
331
 
321
332
  class Context
@@ -4,18 +4,25 @@ module Sprockets
4
4
  # that exists in Sprockets environment.
5
5
  class AssetPath < BasePath
6
6
  def initialize(uri, asset, options = {})
7
- @uri = uri
7
+ @uri = uri
8
+ @asset = asset
8
9
  @options = {
9
- :body => false,
10
+ :body => false,
10
11
  :digest => Helpers.digest,
11
12
  :prefix => Helpers.prefix
12
13
  }.merge options
13
-
14
+
14
15
  @uri.path = @options[:digest] ? asset.digest_path : asset.logical_path
15
16
  end
16
-
17
+
18
+ def to_a
19
+ @asset.to_a.map do |dependency|
20
+ AssetPath.new(@uri.clone, dependency, @options.merge(:body => true)).to_s
21
+ end
22
+ end
23
+
17
24
  protected
18
-
25
+
19
26
  def rewrite_path
20
27
  prefix = if options[:prefix].respond_to? :call
21
28
  warn 'DEPRECATION WARNING: Using a Proc for Sprockets::Helpers.prefix is deprecated and will be removed in 1.0. Please use Sprockets::Helpers.asset_host instead.'
@@ -23,10 +30,10 @@ module Sprockets
23
30
  else
24
31
  options[:prefix].to_s
25
32
  end
26
-
33
+
27
34
  prepend_path(prefix)
28
35
  end
29
-
36
+
30
37
  def rewrite_query
31
38
  append_query('body=1') if options[:body]
32
39
  end
@@ -13,7 +13,7 @@ module Sprockets
13
13
 
14
14
  #
15
15
  def initialize(uri, options = {})
16
- @uri = uri
16
+ @uri = uri
17
17
  @options = options
18
18
  end
19
19
 
@@ -40,7 +40,7 @@ module Sprockets
40
40
  # Hook for rewriting the host.
41
41
  def rewrite_host # :nodoc:
42
42
  if host = compute_asset_host
43
- uri.host = host
43
+ uri.host = host
44
44
  uri.scheme = compute_scheme
45
45
  end
46
46
  end
@@ -4,12 +4,12 @@ module Sprockets
4
4
  # prepends the prefix.
5
5
  class ManifestPath < AssetPath
6
6
  def initialize(uri, path, options = {})
7
- @uri = uri
7
+ @uri = uri
8
8
  @options = {
9
- :body => false,
9
+ :body => false,
10
10
  :prefix => Helpers.prefix
11
11
  }.merge options
12
-
12
+
13
13
  @uri.path = path.to_s
14
14
  end
15
15
  end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Helpers
3
- VERSION = '0.8.0'
3
+ VERSION = '0.9.1'
4
4
  end
5
5
  end
@@ -379,7 +379,7 @@ describe Sprockets::Helpers do
379
379
  end
380
380
 
381
381
  it 'raises when called without block' do
382
- expect { context.asset_tag('main.js') }.to raise_error(ArgumentError, "block missing")
382
+ expect { context.asset_tag('main.js') }.to raise_error(ArgumentError, 'block missing')
383
383
  end
384
384
 
385
385
  it 'expands when configured' do
@@ -419,11 +419,15 @@ describe Sprockets::Helpers do
419
419
 
420
420
  describe '#javascript_tag' do
421
421
  it 'generates script tag' do
422
- context.javascript_tag('main.js').should == '<script src="/main.js"></script>'
422
+ context.javascript_tag('/main.js').should == '<script src="/main.js"></script>'
423
423
  end
424
424
 
425
425
  it 'appends extension' do
426
- context.javascript_tag('main').should == '<script src="/main.js"></script>'
426
+ context.javascript_tag('/main').should == '<script src="/main.js"></script>'
427
+ end
428
+
429
+ it 'prepends the javascript dir' do
430
+ context.javascript_tag('main').should == '<script src="/javascripts/main.js"></script>'
427
431
  end
428
432
 
429
433
  describe 'when expanding' do
@@ -441,11 +445,15 @@ describe Sprockets::Helpers do
441
445
 
442
446
  describe '#stylesheet_tag' do
443
447
  it 'generates stylesheet tag' do
444
- context.stylesheet_tag('main.css').should == '<link rel="stylesheet" href="/main.css">'
448
+ context.stylesheet_tag('/main.css').should == '<link rel="stylesheet" href="/main.css">'
445
449
  end
446
450
 
447
- it 'generates stylesheet tag' do
448
- context.stylesheet_tag('main').should == '<link rel="stylesheet" href="/main.css">'
451
+ it 'appends extension' do
452
+ context.stylesheet_tag('/main').should == '<link rel="stylesheet" href="/main.css">'
453
+ end
454
+
455
+ it 'prepends the stylesheets dir' do
456
+ context.stylesheet_tag('main').should == '<link rel="stylesheet" href="/stylesheets/main.css">'
449
457
  end
450
458
 
451
459
  describe 'when expanding' do
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.8.0
4
+ version: 0.9.1
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: 2013-01-16 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sprockets
16
- requirement: &70280862449120 !ruby/object:Gem::Requirement
16
+ requirement: &70117526611900 !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: *70280862449120
24
+ version_requirements: *70117526611900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: appraisal
27
- requirement: &70280862448600 !ruby/object:Gem::Requirement
27
+ requirement: &70117526611360 !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: *70280862448600
35
+ version_requirements: *70117526611360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70280862448100 !ruby/object:Gem::Requirement
38
+ requirement: &70117526610900 !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: *70280862448100
46
+ version_requirements: *70117526610900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: test-construct
49
- requirement: &70280862447080 !ruby/object:Gem::Requirement
49
+ requirement: &70117526610340 !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: *70280862447080
57
+ version_requirements: *70117526610340
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70280862446380 !ruby/object:Gem::Requirement
60
+ requirement: &70117526609960 !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: *70280862446380
68
+ version_requirements: *70117526609960
69
69
  description: Asset path helpers for Sprockets 2.x applications
70
70
  email:
71
71
  - me@petebrowne.com
@@ -90,7 +90,6 @@ 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
94
93
  - lib/sprockets/helpers/file_path.rb
95
94
  - lib/sprockets/helpers/manifest_path.rb
96
95
  - lib/sprockets/helpers/version.rb
@@ -111,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
110
  version: '0'
112
111
  segments:
113
112
  - 0
114
- hash: -4303612460519246856
113
+ hash: 2044833446584008479
115
114
  required_rubygems_version: !ruby/object:Gem::Requirement
116
115
  none: false
117
116
  requirements:
@@ -120,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
119
  version: '0'
121
120
  segments:
122
121
  - 0
123
- hash: -4303612460519246856
122
+ hash: 2044833446584008479
124
123
  requirements: []
125
124
  rubyforge_project: sprockets-helpers
126
125
  rubygems_version: 1.8.11
@@ -1,17 +0,0 @@
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