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

Commit a844433

Browse files
committed
Merge pull request #4 from billmag/erb_extension_support
Add support for requirejs.yml.erb filename
2 parents e10e0ed + 76dc052 commit a844433

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
requirejs-rails (0.9.0)
4+
requirejs-rails (0.9.1)
55
railties (>= 3.1.1, < 3.3)
66

77
GEM

lib/requirejs/rails/engine.rb

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +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.requirejs.user_config_file = Pathname.new(app.paths["config"].first)+'requirejs.yml'
19-
if config.requirejs.user_config_file.exist?
20-
config.requirejs.user_config = YAML.load(ERB.new(config.requirejs.user_config_file.read).result)
21-
else
22-
config.requirejs.user_config = {}
23-
end
2413
end
2514

2615
config.before_initialize do |app|
2716
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+
2822
config.assets.precompile += config.requirejs.precompile
2923

3024
manifest_path = File.join(::Rails.public_path, config.assets.prefix, "rjs_manifest.yml")
@@ -48,6 +42,35 @@ class Engine < ::Rails::Engine
4842
end
4943
end
5044

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
5174
end # class Engine
5275
end
5376
end

0 commit comments

Comments
 (0)