-
Notifications
You must be signed in to change notification settings - Fork 147
/
engine.rb
70 lines (61 loc) · 2.06 KB
/
engine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require "rails/engine"
module JsRoutes
class SprocketsExtension
def initialize(filename, &block)
@filename = filename
@source = block.call
end
def render(context, empty_hash_wtf)
self.class.run(@filename, @source, context)
end
def self.run(filename, source, context)
if context.logical_path == 'js-routes'
routes = Rails.root.join('config', 'routes.rb').to_s
context.depend_on(routes)
end
source
end
def self.call(input)
filename = input[:filename]
source = input[:data]
context = input[:environment].context_class.new(input)
result = run(filename, source, context)
context.metadata.merge(data: result)
end
end
class Engine < ::Rails::Engine
def self.install_sprockets!
return if defined?(@installed_sprockets)
require 'sprockets/version'
v2 = Gem::Dependency.new('', ' ~> 2')
vgte3 = Gem::Dependency.new('', ' >= 3')
sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
initializer_args = case sprockets_version
when -> (v) { v2.match?('', v) }
{ after: "sprockets.environment" }
when -> (v) { vgte3.match?('', v) }
{ after: :engines_blank_point, before: :finisher_hook }
else
raise StandardError, "Sprockets version #{sprockets_version} is not supported"
end
initializer 'js-routes.dependent_on_routes', initializer_args do
case sprockets_version
when -> (v) { v2.match?('', v) },
-> (v) { vgte3.match?('', v) }
Rails.application.config.assets.configure do |config|
config.register_preprocessor(
"application/javascript",
SprocketsExtension,
)
end
else
raise StandardError, "Sprockets version #{sprockets_version} is not supported"
end
end
@installed_sprockets = true
end
if defined?(::Sprockets::Railtie)
install_sprockets!
end
end
end