@@ -7,24 +7,18 @@ module Rails
7
7
class Engine < ::Rails ::Engine
8
8
9
9
### Configuration setup
10
- config . before_configuration do | app |
10
+ config . before_configuration do
11
11
config . requirejs = Requirejs ::Rails ::Config . new
12
12
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
24
13
end
25
14
26
15
config . before_initialize do |app |
27
16
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
+
28
22
config . assets . precompile += config . requirejs . precompile
29
23
30
24
manifest_path = File . join ( ::Rails . public_path , config . assets . prefix , "rjs_manifest.yml" )
@@ -48,6 +42,35 @@ class Engine < ::Rails::Engine
48
42
end
49
43
end
50
44
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
51
74
end # class Engine
52
75
end
53
76
end
0 commit comments