sprockets-rails 2.3.3 → 3.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/{LICENSE → MIT-LICENSE} +1 -1
- data/README.md +70 -37
- data/lib/sprockets/rails/asset_url_processor.rb +17 -0
- data/lib/sprockets/rails/context.rb +48 -0
- data/lib/sprockets/rails/helper.rb +271 -128
- data/lib/sprockets/rails/quiet_assets.rb +18 -0
- data/lib/sprockets/rails/route_wrapper.rb +23 -0
- data/lib/sprockets/rails/sourcemapping_url_processor.rb +54 -0
- data/lib/sprockets/rails/task.rb +10 -15
- data/lib/sprockets/rails/utils.rb +11 -0
- data/lib/sprockets/rails/version.rb +1 -1
- data/lib/sprockets/railtie.rb +185 -75
- metadata +23 -27
- data/lib/sprockets/rails/legacy_asset_tag_helper.rb +0 -32
- data/lib/sprockets/rails/legacy_asset_url_helper.rb +0 -133
data/lib/sprockets/railtie.rb
CHANGED
@@ -2,9 +2,17 @@ require 'rails'
|
|
2
2
|
require 'rails/railtie'
|
3
3
|
require 'action_controller/railtie'
|
4
4
|
require 'active_support/core_ext/module/remove_method'
|
5
|
+
require 'active_support/core_ext/numeric/bytes'
|
5
6
|
require 'sprockets'
|
7
|
+
|
8
|
+
require 'sprockets/rails/asset_url_processor'
|
9
|
+
require 'sprockets/rails/sourcemapping_url_processor'
|
10
|
+
require 'sprockets/rails/context'
|
6
11
|
require 'sprockets/rails/helper'
|
12
|
+
require 'sprockets/rails/quiet_assets'
|
13
|
+
require 'sprockets/rails/route_wrapper'
|
7
14
|
require 'sprockets/rails/version'
|
15
|
+
require 'set'
|
8
16
|
|
9
17
|
module Rails
|
10
18
|
class Application
|
@@ -19,37 +27,40 @@ module Rails
|
|
19
27
|
remove_possible_method :assets=
|
20
28
|
|
21
29
|
# Returns Sprockets::Environment for app config.
|
22
|
-
|
23
|
-
@assets ||= Sprockets::Environment.new(root.to_s) do |env|
|
24
|
-
env.version = ::Rails.env
|
30
|
+
attr_accessor :assets
|
25
31
|
|
26
|
-
|
27
|
-
|
32
|
+
# Returns Sprockets::Manifest for app config.
|
33
|
+
attr_accessor :assets_manifest
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
35
|
+
# Called from asset helpers to alert you if you reference an asset URL that
|
36
|
+
# isn't precompiled and hence won't be available in production.
|
37
|
+
def asset_precompiled?(logical_path)
|
38
|
+
if precompiled_assets.include?(logical_path)
|
39
|
+
true
|
40
|
+
elsif !config.cache_classes
|
41
|
+
# Check to see if precompile list has been updated
|
42
|
+
precompiled_assets(true).include?(logical_path)
|
43
|
+
else
|
44
|
+
false
|
32
45
|
end
|
33
46
|
end
|
34
|
-
attr_writer :assets
|
35
47
|
|
36
|
-
#
|
37
|
-
|
48
|
+
# Lazy-load the precompile list so we don't cause asset compilation at app
|
49
|
+
# boot time, but ensure we cache the list so we don't recompute it for each
|
50
|
+
# request or test case.
|
51
|
+
def precompiled_assets(clear_cache = false)
|
52
|
+
@precompiled_assets = nil if clear_cache
|
53
|
+
@precompiled_assets ||= assets_manifest.find(config.assets.precompile).map(&:logical_path).to_set
|
54
|
+
end
|
38
55
|
end
|
39
56
|
|
40
57
|
class Engine < Railtie
|
41
58
|
# Skip defining append_assets_path on Rails <= 4.2
|
42
59
|
unless initializers.find { |init| init.name == :append_assets_path }
|
43
60
|
initializer :append_assets_path, :group => :all do |app|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
|
48
|
-
else
|
49
|
-
app.config.assets.paths.unshift(*paths["vendor/assets"].paths.select { |d| File.directory?(d) })
|
50
|
-
app.config.assets.paths.unshift(*paths["lib/assets"].paths.select { |d| File.directory?(d) })
|
51
|
-
app.config.assets.paths.unshift(*paths["app/assets"].paths.select { |d| File.directory?(d) })
|
52
|
-
end
|
61
|
+
app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
|
62
|
+
app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
|
63
|
+
app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
|
53
64
|
end
|
54
65
|
end
|
55
66
|
end
|
@@ -57,8 +68,26 @@ end
|
|
57
68
|
|
58
69
|
module Sprockets
|
59
70
|
class Railtie < ::Rails::Railtie
|
60
|
-
|
61
|
-
|
71
|
+
include Sprockets::Rails::Utils
|
72
|
+
|
73
|
+
class ManifestNeededError < StandardError
|
74
|
+
def initialize
|
75
|
+
msg = "Expected to find a manifest file in `app/assets/config/manifest.js`\n" +
|
76
|
+
"But did not, please create this file and use it to link any assets that need\n" +
|
77
|
+
"to be rendered by your app:\n\n" +
|
78
|
+
"Example:\n" +
|
79
|
+
" //= link_tree ../images\n" +
|
80
|
+
" //= link_directory ../javascripts .js\n" +
|
81
|
+
" //= link_directory ../stylesheets .css\n" +
|
82
|
+
"and restart your server\n\n" +
|
83
|
+
"For more information see: https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs"
|
84
|
+
super msg
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
LOOSE_APP_ASSETS = lambda do |logical_path, filename|
|
89
|
+
filename.start_with?(::Rails.root.join("app/assets").to_s) &&
|
90
|
+
!['.js', '.css', ''].include?(File.extname(logical_path))
|
62
91
|
end
|
63
92
|
|
64
93
|
class OrderedOptions < ActiveSupport::OrderedOptions
|
@@ -68,92 +97,173 @@ module Sprockets
|
|
68
97
|
end
|
69
98
|
|
70
99
|
config.assets = OrderedOptions.new
|
71
|
-
config.assets._blocks
|
72
|
-
config.assets.paths
|
73
|
-
config.assets.
|
74
|
-
config.assets.
|
75
|
-
config.assets.
|
76
|
-
config.assets.
|
77
|
-
config.assets.
|
78
|
-
|
79
|
-
|
100
|
+
config.assets._blocks = []
|
101
|
+
config.assets.paths = []
|
102
|
+
config.assets.precompile = []
|
103
|
+
config.assets.prefix = "/assets"
|
104
|
+
config.assets.manifest = nil
|
105
|
+
config.assets.quiet = false
|
106
|
+
config.assets.resolve_assets_in_css_urls = true
|
107
|
+
|
108
|
+
initializer :set_default_precompile do |app|
|
109
|
+
if using_sprockets4?
|
110
|
+
raise ManifestNeededError unless ::Rails.root.join("app/assets/config/manifest.js").exist?
|
111
|
+
app.config.assets.precompile += %w( manifest.js )
|
112
|
+
else
|
113
|
+
app.config.assets.precompile += [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
initializer :quiet_assets do |app|
|
118
|
+
if app.config.assets.quiet
|
119
|
+
app.middleware.insert_before ::Rails::Rack::Logger, ::Sprockets::Rails::QuietAssets
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
initializer :asset_url_processor do |app|
|
124
|
+
if app.config.assets.resolve_assets_in_css_urls
|
125
|
+
Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
initializer :asset_sourcemap_url_processor do |app|
|
130
|
+
Sprockets.register_postprocessor "application/javascript", ::Sprockets::Rails::SourcemappingUrlProcessor
|
131
|
+
end
|
132
|
+
|
133
|
+
config.assets.version = ""
|
134
|
+
config.assets.debug = false
|
135
|
+
config.assets.compile = true
|
136
|
+
config.assets.digest = true
|
137
|
+
config.assets.cache_limit = 50.megabytes
|
138
|
+
config.assets.gzip = true
|
139
|
+
config.assets.check_precompiled_asset = true
|
140
|
+
config.assets.unknown_asset_fallback = true
|
141
|
+
|
142
|
+
config.assets.configure do |env|
|
143
|
+
config.assets.paths.each { |path| env.append_path(path) }
|
144
|
+
end
|
145
|
+
|
146
|
+
config.assets.configure do |env|
|
147
|
+
env.context_class.send :include, ::Sprockets::Rails::Context
|
148
|
+
env.context_class.assets_prefix = config.assets.prefix
|
149
|
+
env.context_class.digest_assets = config.assets.digest
|
150
|
+
env.context_class.config = config.action_controller
|
151
|
+
end
|
152
|
+
|
153
|
+
config.assets.configure do |env|
|
154
|
+
env.cache = Sprockets::Cache::FileStore.new(
|
155
|
+
"#{env.root}/tmp/cache/assets",
|
156
|
+
config.assets.cache_limit,
|
157
|
+
env.logger
|
158
|
+
)
|
159
|
+
end
|
160
|
+
|
161
|
+
Sprockets.register_dependency_resolver 'rails-env' do
|
162
|
+
::Rails.env.to_s
|
163
|
+
end
|
164
|
+
|
165
|
+
config.assets.configure do |env|
|
166
|
+
env.depend_on 'rails-env'
|
167
|
+
end
|
168
|
+
|
169
|
+
config.assets.configure do |env|
|
170
|
+
env.version = config.assets.version
|
171
|
+
end
|
172
|
+
|
173
|
+
config.assets.configure do |env|
|
174
|
+
env.gzip = config.assets.gzip if env.respond_to?(:gzip=)
|
175
|
+
end
|
80
176
|
|
81
177
|
rake_tasks do |app|
|
82
178
|
require 'sprockets/rails/task'
|
83
179
|
Sprockets::Rails::Task.new(app)
|
84
180
|
end
|
85
181
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
# the Sprockets cache when changed.
|
91
|
-
app.assets.version = [
|
92
|
-
app.assets.version,
|
93
|
-
config.assets.version,
|
94
|
-
config.action_controller.relative_url_root,
|
95
|
-
(config.action_controller.asset_host unless config.action_controller.asset_host.respond_to?(:call)),
|
96
|
-
Sprockets::Rails::VERSION
|
97
|
-
].compact.join('-')
|
98
|
-
|
99
|
-
# Copy config.assets.paths to Sprockets
|
100
|
-
config.assets.paths.each do |path|
|
101
|
-
app.assets.append_path path
|
182
|
+
def build_environment(app, initialized = nil)
|
183
|
+
initialized = app.initialized? if initialized.nil?
|
184
|
+
unless initialized
|
185
|
+
::Rails.logger.warn "Application uninitialized: Try calling YourApp::Application.initialize!"
|
102
186
|
end
|
103
187
|
|
188
|
+
env = Sprockets::Environment.new(app.root.to_s)
|
189
|
+
|
190
|
+
config = app.config
|
191
|
+
|
104
192
|
# Run app.assets.configure blocks
|
105
193
|
config.assets._blocks.each do |block|
|
106
|
-
block.call
|
194
|
+
block.call(env)
|
107
195
|
end
|
108
196
|
|
109
197
|
# Set compressors after the configure blocks since they can
|
110
198
|
# define new compressors and we only accept existent compressors.
|
111
|
-
|
112
|
-
|
199
|
+
env.js_compressor = config.assets.js_compressor
|
200
|
+
env.css_compressor = config.assets.css_compressor
|
113
201
|
|
114
202
|
# No more configuration changes at this point.
|
115
203
|
# With cache classes on, Sprockets won't check the FS when files
|
116
204
|
# change. Preferable in production when the FS only changes on
|
117
205
|
# deploys when the app restarts.
|
118
206
|
if config.cache_classes
|
119
|
-
|
207
|
+
env = env.cached
|
120
208
|
end
|
121
209
|
|
122
|
-
|
210
|
+
env
|
211
|
+
end
|
212
|
+
|
213
|
+
def self.build_manifest(app)
|
214
|
+
config = app.config
|
215
|
+
path = File.join(config.paths['public'].first, config.assets.prefix)
|
216
|
+
Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
|
217
|
+
end
|
218
|
+
|
219
|
+
config.after_initialize do |app|
|
220
|
+
config = app.config
|
221
|
+
|
123
222
|
if config.assets.compile
|
124
|
-
app.
|
125
|
-
|
126
|
-
|
223
|
+
app.assets = self.build_environment(app, true)
|
224
|
+
app.routes.prepend do
|
225
|
+
mount app.assets => config.assets.prefix
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
app.assets_manifest = build_manifest(app)
|
230
|
+
|
231
|
+
if config.assets.resolve_with.nil?
|
232
|
+
config.assets.resolve_with = []
|
233
|
+
config.assets.resolve_with << :manifest if config.assets.digest && !config.assets.debug
|
234
|
+
config.assets.resolve_with << :environment if config.assets.compile
|
235
|
+
end
|
236
|
+
|
237
|
+
ActionDispatch::Routing::RouteWrapper.class_eval do
|
238
|
+
class_attribute :assets_prefix
|
239
|
+
|
240
|
+
if defined?(prepend) && ::Rails.version >= '4'
|
241
|
+
prepend Sprockets::Rails::RouteWrapper
|
242
|
+
else
|
243
|
+
include Sprockets::Rails::RouteWrapper
|
244
|
+
end
|
245
|
+
|
246
|
+
self.assets_prefix = config.assets.prefix
|
127
247
|
end
|
128
248
|
|
129
249
|
ActiveSupport.on_load(:action_view) do
|
130
250
|
include Sprockets::Rails::Helper
|
131
251
|
|
132
252
|
# Copy relevant config to AV context
|
133
|
-
self.debug_assets
|
134
|
-
self.digest_assets
|
135
|
-
self.assets_prefix
|
253
|
+
self.debug_assets = config.assets.debug
|
254
|
+
self.digest_assets = config.assets.digest
|
255
|
+
self.assets_prefix = config.assets.prefix
|
256
|
+
self.assets_precompile = config.assets.precompile
|
136
257
|
|
137
|
-
|
138
|
-
context = app.assets.context_class
|
139
|
-
context.assets_prefix = config.assets.prefix
|
140
|
-
context.digest_assets = config.assets.digest
|
141
|
-
context.config = config.action_controller
|
142
|
-
|
143
|
-
self.assets_environment = app.assets if config.assets.compile
|
258
|
+
self.assets_environment = app.assets
|
144
259
|
self.assets_manifest = app.assets_manifest
|
145
|
-
end
|
146
260
|
|
147
|
-
|
148
|
-
Sprockets::Rails::Helper.assets ||= app.assets
|
149
|
-
Sprockets::Rails::Helper.raise_runtime_errors = app.config.assets.raise_runtime_errors
|
261
|
+
self.resolve_assets_with = config.assets.resolve_with
|
150
262
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
156
|
-
end
|
263
|
+
self.check_precompiled_asset = config.assets.check_precompiled_asset
|
264
|
+
self.unknown_asset_fallback = config.assets.unknown_asset_fallback
|
265
|
+
# Expose the app precompiled asset check to the view
|
266
|
+
self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
|
157
267
|
end
|
158
268
|
end
|
159
269
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Peek
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sprockets
|
@@ -16,62 +16,56 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '4.0'
|
19
|
+
version: 3.0.0
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '4.0'
|
26
|
+
version: 3.0.0
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: actionpack
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
31
|
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
33
|
+
version: '5.2'
|
40
34
|
type: :runtime
|
41
35
|
prerelease: false
|
42
36
|
version_requirements: !ruby/object:Gem::Requirement
|
43
37
|
requirements:
|
44
38
|
- - ">="
|
45
39
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
40
|
+
version: '5.2'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: activesupport
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
50
44
|
requirements:
|
51
45
|
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
47
|
+
version: '5.2'
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
51
|
requirements:
|
58
52
|
- - ">="
|
59
53
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
54
|
+
version: '5.2'
|
61
55
|
- !ruby/object:Gem::Dependency
|
62
56
|
name: railties
|
63
57
|
requirement: !ruby/object:Gem::Requirement
|
64
58
|
requirements:
|
65
59
|
- - ">="
|
66
60
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
61
|
+
version: '5.2'
|
68
62
|
type: :development
|
69
63
|
prerelease: false
|
70
64
|
version_requirements: !ruby/object:Gem::Requirement
|
71
65
|
requirements:
|
72
66
|
- - ">="
|
73
67
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
68
|
+
version: '5.2'
|
75
69
|
- !ruby/object:Gem::Dependency
|
76
70
|
name: rake
|
77
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,26 +108,30 @@ dependencies:
|
|
114
108
|
- - ">="
|
115
109
|
- !ruby/object:Gem::Version
|
116
110
|
version: '0'
|
117
|
-
description:
|
111
|
+
description:
|
118
112
|
email: josh@joshpeek.com
|
119
113
|
executables: []
|
120
114
|
extensions: []
|
121
115
|
extra_rdoc_files: []
|
122
116
|
files:
|
123
|
-
- LICENSE
|
117
|
+
- MIT-LICENSE
|
124
118
|
- README.md
|
125
119
|
- lib/sprockets/rails.rb
|
120
|
+
- lib/sprockets/rails/asset_url_processor.rb
|
121
|
+
- lib/sprockets/rails/context.rb
|
126
122
|
- lib/sprockets/rails/helper.rb
|
127
|
-
- lib/sprockets/rails/
|
128
|
-
- lib/sprockets/rails/
|
123
|
+
- lib/sprockets/rails/quiet_assets.rb
|
124
|
+
- lib/sprockets/rails/route_wrapper.rb
|
125
|
+
- lib/sprockets/rails/sourcemapping_url_processor.rb
|
129
126
|
- lib/sprockets/rails/task.rb
|
127
|
+
- lib/sprockets/rails/utils.rb
|
130
128
|
- lib/sprockets/rails/version.rb
|
131
129
|
- lib/sprockets/railtie.rb
|
132
130
|
homepage: https://github.com/rails/sprockets-rails
|
133
131
|
licenses:
|
134
132
|
- MIT
|
135
133
|
metadata: {}
|
136
|
-
post_install_message:
|
134
|
+
post_install_message:
|
137
135
|
rdoc_options: []
|
138
136
|
require_paths:
|
139
137
|
- lib
|
@@ -141,17 +139,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
141
139
|
requirements:
|
142
140
|
- - ">="
|
143
141
|
- !ruby/object:Gem::Version
|
144
|
-
version: '
|
142
|
+
version: '2.5'
|
145
143
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
144
|
requirements:
|
147
145
|
- - ">="
|
148
146
|
- !ruby/object:Gem::Version
|
149
147
|
version: '0'
|
150
148
|
requirements: []
|
151
|
-
|
152
|
-
|
153
|
-
signing_key:
|
149
|
+
rubygems_version: 3.2.32
|
150
|
+
signing_key:
|
154
151
|
specification_version: 4
|
155
152
|
summary: Sprockets Rails integration
|
156
153
|
test_files: []
|
157
|
-
has_rdoc:
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'sprockets'
|
2
|
-
|
3
|
-
module Sprockets
|
4
|
-
module Rails
|
5
|
-
# Backports of AssetTagHelper methods for Rails 2.x and 3.x.
|
6
|
-
module LegacyAssetTagHelper
|
7
|
-
include ActionView::Helpers::TagHelper
|
8
|
-
|
9
|
-
def javascript_include_tag(*sources)
|
10
|
-
options = sources.extract_options!.stringify_keys
|
11
|
-
sources.uniq.map { |source|
|
12
|
-
tag_options = {
|
13
|
-
"src" => path_to_javascript(source)
|
14
|
-
}.merge(options)
|
15
|
-
content_tag(:script, "", tag_options)
|
16
|
-
}.join("\n").html_safe
|
17
|
-
end
|
18
|
-
|
19
|
-
def stylesheet_link_tag(*sources)
|
20
|
-
options = sources.extract_options!.stringify_keys
|
21
|
-
sources.uniq.map { |source|
|
22
|
-
tag_options = {
|
23
|
-
"rel" => "stylesheet",
|
24
|
-
"media" => "screen",
|
25
|
-
"href" => path_to_stylesheet(source)
|
26
|
-
}.merge(options)
|
27
|
-
tag(:link, tag_options)
|
28
|
-
}.join("\n").html_safe
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,133 +0,0 @@
|
|
1
|
-
require 'sprockets'
|
2
|
-
|
3
|
-
module Sprockets
|
4
|
-
module Rails
|
5
|
-
# Backports of AssetUrlHelper methods for Rails 2.x and 3.x.
|
6
|
-
module LegacyAssetUrlHelper
|
7
|
-
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
|
8
|
-
|
9
|
-
def asset_path(source, options = {})
|
10
|
-
source = source.to_s
|
11
|
-
return "" unless source.present?
|
12
|
-
return source if source =~ URI_REGEXP
|
13
|
-
|
14
|
-
tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')
|
15
|
-
|
16
|
-
if extname = compute_asset_extname(source, options)
|
17
|
-
source = "#{source}#{extname}"
|
18
|
-
end
|
19
|
-
|
20
|
-
if source[0] != ?/
|
21
|
-
source = compute_asset_path(source, options)
|
22
|
-
end
|
23
|
-
|
24
|
-
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
|
25
|
-
if relative_url_root
|
26
|
-
source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/")
|
27
|
-
end
|
28
|
-
|
29
|
-
if host = compute_asset_host(source, options)
|
30
|
-
source = "#{host}#{source}"
|
31
|
-
end
|
32
|
-
|
33
|
-
"#{source}#{tail}"
|
34
|
-
end
|
35
|
-
alias_method :path_to_asset, :asset_path
|
36
|
-
|
37
|
-
def asset_url(source, options = {})
|
38
|
-
path_to_asset(source, options.merge(:protocol => :request))
|
39
|
-
end
|
40
|
-
|
41
|
-
ASSET_EXTENSIONS = {
|
42
|
-
:javascript => '.js',
|
43
|
-
:stylesheet => '.css'
|
44
|
-
}
|
45
|
-
|
46
|
-
def compute_asset_extname(source, options = {})
|
47
|
-
return if options[:extname] == false
|
48
|
-
extname = options[:extname] || ASSET_EXTENSIONS[options[:type]]
|
49
|
-
extname if extname && File.extname(source) != extname
|
50
|
-
end
|
51
|
-
|
52
|
-
ASSET_PUBLIC_DIRECTORIES = {
|
53
|
-
:audio => '/audios',
|
54
|
-
:font => '/fonts',
|
55
|
-
:image => '/images',
|
56
|
-
:javascript => '/javascripts',
|
57
|
-
:stylesheet => '/stylesheets',
|
58
|
-
:video => '/videos'
|
59
|
-
}
|
60
|
-
|
61
|
-
def compute_asset_path(source, options = {})
|
62
|
-
dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
|
63
|
-
File.join(dir, source)
|
64
|
-
end
|
65
|
-
|
66
|
-
def compute_asset_host(source = "", options = {})
|
67
|
-
request = self.request if respond_to?(:request)
|
68
|
-
|
69
|
-
if defined? config
|
70
|
-
host = config.asset_host
|
71
|
-
elsif defined? ActionController::Base.asset_host
|
72
|
-
host = ActionController::Base.asset_host
|
73
|
-
end
|
74
|
-
|
75
|
-
host ||= request.base_url if request && options[:protocol] == :request
|
76
|
-
return unless host
|
77
|
-
|
78
|
-
if host.respond_to?(:call)
|
79
|
-
arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity
|
80
|
-
args = [source]
|
81
|
-
args << request if request && (arity > 1 || arity < 0)
|
82
|
-
host = host.call(*args)
|
83
|
-
elsif host =~ /%d/
|
84
|
-
host = host % (Zlib.crc32(source) % 4)
|
85
|
-
end
|
86
|
-
|
87
|
-
if host =~ URI_REGEXP
|
88
|
-
host
|
89
|
-
else
|
90
|
-
protocol = options[:protocol] || (request ? :request : :relative)
|
91
|
-
case protocol
|
92
|
-
when :relative
|
93
|
-
"//#{host}"
|
94
|
-
when :request
|
95
|
-
"#{request.protocol}#{host}"
|
96
|
-
else
|
97
|
-
"#{protocol}://#{host}"
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def javascript_path(source, options = {})
|
103
|
-
path_to_asset(source, {:type => :javascript}.merge(options))
|
104
|
-
end
|
105
|
-
alias_method :path_to_javascript, :javascript_path
|
106
|
-
|
107
|
-
def stylesheet_path(source, options = {})
|
108
|
-
path_to_asset(source, {:type => :stylesheet}.merge(options))
|
109
|
-
end
|
110
|
-
alias_method :path_to_stylesheet, :stylesheet_path
|
111
|
-
|
112
|
-
def image_path(source, options = {})
|
113
|
-
path_to_asset(source, {:type => :image}.merge(options))
|
114
|
-
end
|
115
|
-
alias_method :path_to_image, :image_path
|
116
|
-
|
117
|
-
def video_path(source, options = {})
|
118
|
-
path_to_asset(source, {:type => :video}.merge(options))
|
119
|
-
end
|
120
|
-
alias_method :path_to_video, :video_path
|
121
|
-
|
122
|
-
def audio_path(source, options = {})
|
123
|
-
path_to_asset(source, {:type => :audio}.merge(options))
|
124
|
-
end
|
125
|
-
alias_method :path_to_audio, :audio_path
|
126
|
-
|
127
|
-
def font_path(source, options = {})
|
128
|
-
path_to_asset(source, {:type => :font}.merge(options))
|
129
|
-
end
|
130
|
-
alias_method :path_to_font, :font_path
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|