Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 76dc052

Browse files
author
Ian Leue
committed
Only do erb processing if it's a yml.erb file, and do it in before_initialize so we can pick up environment-specific injections
1 parent 6b829d3 commit 76dc052

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

lib/requirejs/rails/engine.rb

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,18 @@ module Rails
77
class Engine < ::Rails::Engine
88

99
### Configuration setup
10-
config.before_configuration do |app|
10+
config.before_configuration do
1111
config.requirejs = Requirejs::Rails::Config.new
1212
config.requirejs.precompile = [/require\.js$/]
13-
14-
# Location of the user-supplied config parameters, which will be
15-
# merged with the default params. It should be a YAML file with
16-
# a single top-level hash, keys/values corresponding to require.js
17-
# config parameters.
18-
config_path = Pathname.new(app.paths["config"].first)
19-
config.requirejs.user_config_file = config_path+'requirejs.yml'
20-
# If requirejs.yml doesn't exist, fall back to requirejs.yml.erb
21-
unless config.requirejs.user_config_file.exist?
22-
config.requirejs.user_config_file = config_path+'requirejs.yml.erb'
23-
end
24-
25-
if config.requirejs.user_config_file.exist?
26-
config.requirejs.user_config = YAML.load(ERB.new(config.requirejs.user_config_file.read).result)
27-
else
28-
config.requirejs.user_config = {}
29-
end
3013
end
3114

3215
config.before_initialize do |app|
3316
config = app.config
17+
18+
# Process the user config file in #before_initalization (instead of #before_configuration) so that
19+
# environment-specific configuration can be injected into the user configuration file
20+
process_user_config_file(app, config)
21+
3422
config.assets.precompile += config.requirejs.precompile
3523

3624
manifest_path = File.join(::Rails.public_path, config.assets.prefix, "rjs_manifest.yml")
@@ -54,6 +42,35 @@ class Engine < ::Rails::Engine
5442
end
5543
end
5644

45+
private
46+
47+
# Process the user-supplied config parameters, which will be
48+
# merged with the default params. It should be a YAML file with
49+
# a single top-level hash, keys/values corresponding to require.js
50+
# config parameters.
51+
def process_user_config_file(app, config)
52+
config_path = Pathname.new(app.paths["config"].first)
53+
config.requirejs.user_config_file = config_path+'requirejs.yml'
54+
55+
yaml_file_contents = nil
56+
if config.requirejs.user_config_file.exist?
57+
yaml_file_contents = config.requirejs.user_config_file.read
58+
else
59+
# if requirejs.yml doesn't exist, look for requirejs.yml.erb and process it as an erb
60+
config.requirejs.user_config_file = config_path+'requirejs.yml.erb'
61+
62+
if config.requirejs.user_config_file.exist?
63+
yaml_file_contents = ERB.new(config.requirejs.user_config_file.read).result
64+
end
65+
end
66+
67+
if yaml_file_contents.nil?
68+
# If we couldn't find any matching file contents to process, empty user config
69+
config.requirejs.user_config = {}
70+
else
71+
config.requirejs.user_config = YAML.load(yaml_file_contents)
72+
end
73+
end
5774
end # class Engine
5875
end
5976
end

0 commit comments

Comments
 (0)