Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block reload config files in Railtie if they already uploaded #353

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions spec/config_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
Config.load_files "#{fixture_path}/settings.yml", "#{fixture_path}/multilevel.yml"
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 @@ -151,6 +151,45 @@
expect(Settings.size).to eq(2)
end

describe "load_and_set_settings in the Rails context" do
vovanmozg marked this conversation as resolved.
Show resolved Hide resolved
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 Rails.version == "6.1.7.6"
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