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

Allow to use custom filename && directory name to store configs #341

Merged
merged 4 commits into from
Feb 13, 2024
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ After installing `Config` in Rails, you will find automatically generated file t

* `const_name` - name of the object holing you settings. Default: `'Settings'`
* `evaluate_erb_in_yaml` - evaluate ERB in YAML config files. Set to false if the config file contains ERB that should not be evaluated at load time. Default: `true`
* `file_name` - name of the file to store general keys accessible in all environments. Default: `'settings'` - located at `config/settings.yml`
* `dir_name` - name of the directory to store environment-specific files. Default: `'settings'` - located at `config/settings/`

### Merge customization

Expand Down
10 changes: 6 additions & 4 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module Config
env_converter: :downcase,
env_parse_values: true,
fail_on_missing: false,
file_name: 'settings',
dir_name: 'settings',
# deep_merge options
knockout_prefix: nil,
merge_nil_values: true,
Expand Down Expand Up @@ -58,17 +60,17 @@ def self.load_and_set_settings(*sources)

def self.setting_files(config_root, env)
[
File.join(config_root, 'settings.yml').to_s,
File.join(config_root, 'settings', "#{env}.yml").to_s,
File.join(config_root, "#{Config.file_name}.yml").to_s,
File.join(config_root, Config.dir_name, "#{env}.yml").to_s,
File.join(config_root, 'environments', "#{env}.yml").to_s,
*local_setting_files(config_root, env)
].freeze
end

def self.local_setting_files(config_root, env)
[
(File.join(config_root, 'settings.local.yml').to_s if env != 'test'),
File.join(config_root, 'settings', "#{env}.local.yml").to_s,
(File.join(config_root, "#{Config.file_name}.local.yml").to_s if env != 'test'),
File.join(config_root, Config.dir_name, "#{env}.local.yml").to_s,
File.join(config_root, 'environments', "#{env}.local.yml").to_s
].compact
end
Expand Down
4 changes: 2 additions & 2 deletions lib/config/integrations/heroku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def invoke
def vars
# Load only local options to Heroku
Config.load_and_set_settings(
Rails.root.join("config", "settings.local.yml").to_s,
Rails.root.join("config", "settings", "#{environment}.local.yml").to_s,
Rails.root.join("config", "#{Config.file_name}.local.yml").to_s,
Rails.root.join("config", Config.dir_name, "#{environment}.local.yml").to_s,
Rails.root.join("config", "environments", "#{environment}.local.yml").to_s
)

Expand Down
12 changes: 6 additions & 6 deletions lib/generators/config/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ def copy_initializer
end

def copy_settings
template "settings.yml", "config/settings.yml"
template "settings.local.yml", "config/settings.local.yml"
directory "settings", "config/settings"
template "settings.yml", "config/#{Config.file_name}.yml"
template "settings.local.yml", "config/#{Config.file_name}.local.yml"
directory "settings", "config/#{Config.dir_name}"
end

def modify_gitignore
create_file '.gitignore' unless File.exist? '.gitignore'

append_to_file '.gitignore' do
"\n" +
"config/settings.local.yml\n" +
"config/settings/*.local.yml\n" +
"\n" +
"config/#{Config.file_name}.local.yml\n" +
"config/#{Config.dir_name}/*.local.yml\n" +
"config/environments/*.local.yml\n"
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/config/templates/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@
# Evaluate ERB in YAML config files at load time.
#
# config.evaluate_erb_in_yaml = true

# Name of directory and file to store config keys
#
# config.file_name = 'settings'
# config.dir_name = 'settings'
end
17 changes: 17 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@

expect(Config.const_name).to eq("Settings2")
end

context "filename and directory" do
it "should allow to use custom names" do
Config.setup do |config|
config.file_name = "custom_file"
config.dir_name = "custom_dir"
end

expect(Config.file_name).to eq("custom_file")
expect(Config.dir_name).to eq("custom_dir")
end

it "should have default values" do
expect(Config.file_name).to eq("settings")
expect(Config.dir_name).to eq("settings")
end
end
end

context "Settings with a type value of 'hash'" do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def self.reset
self.schema = nil
self.validation_contract = nil
self.fail_on_missing = false
self.file_name = 'settings'
self.dir_name = 'settings'
instance_variable_set(:@_ran_once, false)
end
end
Expand Down