Skip to content
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ You can now edit them to adjust to your needs.

> Note: By default, the config environment will match the Rails environment (`Rails.env`). This can be changed by setting `config.environment`.

You can also define your own settings files and load them in the initializer:

```ruby
Config.setup do |config|
config.load_and_set_settings("/path/to/yaml1", "/path/to/yaml2", ...)
end
```

### Installing on Padrino

Add the gem to your `Gemfile` and run `bundle install` to install it. Then edit `app.rb` and register `Config`
Expand Down
2 changes: 2 additions & 0 deletions lib/config/integrations/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def preload
initializer = ::Rails.root.join('config', 'initializers', 'config.rb')
require initializer if File.exist?(initializer)

return if Object.const_defined?(Config.const_name)

# Parse the settings before any of the initializers
Config.load_and_set_settings(
Config.setting_files(::Rails.root.join('config'), Config.environment.nil? ? ::Rails.env : Config.environment.to_sym)
Expand Down
2 changes: 1 addition & 1 deletion spec/app/rails_5.2/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class Application < Rails::Application
##
# Config
#
config_available?
# config_available? # This is only available in test context
end
end
1 change: 1 addition & 0 deletions spec/app/rails_5.2/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
require "logger"
1 change: 1 addition & 0 deletions spec/app/rails_6.0/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
require "logger"
1 change: 1 addition & 0 deletions spec/app/rails_6.1/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require "bundler/setup" # Set up gems listed in the Gemfile.
require "bootsnap/setup" # Speed up boot time by caching expensive operations.
require "logger"
1 change: 1 addition & 0 deletions spec/app/rails_7.0/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require "bundler/setup" # Set up gems listed in the Gemfile.
require "bootsnap/setup" # Speed up boot time by caching expensive operations.
require "logger"
6 changes: 6 additions & 0 deletions spec/config_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
config_instance
end

before :all do
ENV_BACKUP = ENV.to_hash
end

after :all do
Config.use_env = false

ENV_BACKUP.each { |k, v| ENV[k] = v }
end

before :each do
Expand Down
39 changes: 39 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,45 @@
expect(Settings.size).to eq(2)
end

describe "load_and_set_settings in the Rails context" do
if defined?(Rails)
let(:config_name) { Rails.root.join("config", "initializers", "config.rb") }
let(:default_settings_file_name) { Rails.root.join("config", "settings.yml") }
let(:custom_settings_file_name) { Rails.root.join("config", "settings_custom.yml") }
let(:spring_file_name_for_rails_6_1) { Rails.root.join("bin", "spring") }

before do
File.write(default_settings_file_name, "default: default_value")
File.write(custom_settings_file_name, "custom: custom_value")
# workaround for Rails 6.1
File.write(spring_file_name_for_rails_6_1, "") if Gem::Version.new(Rails.version) >= Gem::Version.new('6.1.0')
end

after do
File.delete(config_name) if File.exist?(config_name)
File.delete(default_settings_file_name) if File.exist?(default_settings_file_name)
File.delete(custom_settings_file_name) if File.exist?(custom_settings_file_name)
File.delete(spring_file_name_for_rails_6_1) if File.exist?(spring_file_name_for_rails_6_1)
end

it "should read value only from default settings without config.rb" do
values = `#{Rails.root.join("bin", "rails")} runner 'print "\#{Settings.default}|\#{Settings.custom}"'`
expect(values).to eq("default_value|")
end

it "should read value only from default settings without use of load_and_set_settings" do
File.write(config_name, "Config.setup { |config| }")
values = `#{Rails.root.join("bin", "rails")} runner 'print "\#{Settings.default}|\#{Settings.custom}"'`
expect(values).to eq("default_value|")
end

it "should read value only from custom settings with use of load_and_set_settings" do
File.write(config_name, "Config.setup { |config| config.load_and_set_settings(['#{custom_settings_file_name}']) }")
values = `#{Rails.root.join("bin", "rails")} runner 'print "\#{Settings.default}|\#{Settings.custom}"'`
expect(values).to eq("|custom_value")
end
end
end


context "Nested Settings" do
Expand Down