Skip to content

Commit 135103b

Browse files
committed
Add support for environment specific settings in configuration file
1 parent 45fd7dd commit 135103b

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

lib/jammit.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ def self.load_configuration(config_path, soft=false)
6464
return false if soft && !exists
6565
raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file" unless exists
6666
conf = YAML.load(ERB.new(File.read(config_path)).result)
67+
68+
# Optionally overwrite configurations based on the environment
69+
if conf.has_key?(RAILS_ENV) && conf[RAILS_ENV].is_a?(Hash)
70+
conf.merge!(conf.delete(RAILS_ENV))
71+
end
72+
6773
@config_path = config_path
6874
@configuration = symbolize_keys(conf)
6975
@package_path = conf[:package_path] || DEFAULT_PACKAGE_PATH

test/config/assets-environment.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
compress_assets: on
2+
gzip_assets: on
3+
4+
development:
5+
compress_assets: off
6+
7+
test:
8+
gzip_assets: off

test/unit/test_configuration.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@ def test_jst_compilation
6262
assert packed == File.read('test/fixtures/jammed/jst_test.js')
6363
end
6464

65+
def test_environment_specific_configuration
66+
Object.send(:remove_const, :RAILS_ENV)
67+
Object.const_set :RAILS_ENV, "development"
68+
Jammit.load_configuration('test/config/assets-environment.yml')
69+
70+
assert !Jammit.compress_assets # Should override with environment specific configuration
71+
assert Jammit.gzip_assets # but keep the general configuration
72+
73+
Object.send(:remove_const, :RAILS_ENV)
74+
Object.const_set :RAILS_ENV, "test"
75+
end
6576
end

0 commit comments

Comments
 (0)