Skip to content

Commit

Permalink
Installe et configure le gem config
Browse files Browse the repository at this point in the history
Ce commit bouge aussi la configuration de la BDD (qui utilise les
credentials chiffés de Rails) sous l'objet Settings
pour n'avoir qu'un point d'entrée.
  • Loading branch information
jbfeldis committed May 24, 2024
1 parent 991fd90 commit 1247605
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
!/app/assets/builds/.keep

/config/credentials/development.key

config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ gem "bootsnap", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem "config"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[mri windows]
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
concurrent-ruby (1.2.3)
config (5.4.0)
deep_merge (~> 1.2, >= 1.2.1)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
deep_merge (1.2.2)
drb (2.2.1)
erubi (1.12.0)
globalid (1.2.1)
Expand Down Expand Up @@ -308,6 +311,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
capybara
config
debug
importmap-rails
jbuilder
Expand Down
6 changes: 2 additions & 4 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ default: &default
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= Rails.application.credentials.database.username %>
password: <%= Rails.application.credentials.database.password %>
username: <%= Settings.credentials.database.username %>
password: <%= Settings.credentials.database.password %>

development:
<<: *default
Expand Down Expand Up @@ -83,5 +83,3 @@ test:
production:
<<: *default
database: quotient_familial_production
username: quotient_familial
password: <%= ENV["QUOTIENT_FAMILIAL_DATABASE_PASSWORD"] %>
68 changes: 68 additions & 0 deletions config/initializers/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Config.setup do |config|
# Name of the constant exposing loaded settings
config.const_name = "Settings"

# Ability to remove elements of the array set in earlier loaded settings file. For example value: '--'.
#
# config.knockout_prefix = nil

# Overwrite an existing value when merging a `nil` value.
# When set to `false`, the existing value is retained after merge.
#
# config.merge_nil_values = true

# Overwrite arrays found in previously loaded settings file. When set to `false`, arrays will be merged.
#
# config.overwrite_arrays = true

# Defines current environment, affecting which settings file will be loaded.
# Default: `Rails.env`
#
# config.environment = ENV.fetch('ENVIRONMENT', :development)

# Load environment variables from the `ENV` object and override any settings defined in files.
#
config.use_env = false

# Define ENV variable prefix deciding which variables to load into config.
#
# Reading variables from ENV is case-sensitive. If you define lowercase value below, ensure your ENV variables are
# prefixed in the same way.
#
# When not set it defaults to `config.const_name`.
#
config.env_prefix = "SETTINGS"

# What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well
# with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where
# using dots in variable names might not be allowed (eg. Bash).
#
config.env_separator = "__"

# Ability to process variables names:
# * nil - no change
# * :downcase - convert to lower case
#
# config.env_converter = :downcase

# Parse numeric values as integers instead of strings.
#
# config.env_parse_values = true

# Validate presence and type of specific config values. Check https://github.com/dry-rb/dry-validation for details.
#
# config.schema do
# required(:name).filled
# required(:age).maybe(:int?)
# required(:email).filled(format?: EMAIL_REGEX)
# end

# 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
2 changes: 2 additions & 0 deletions config/initializers/config_add_credentials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Settings.add_source!(credentials: Rails.application.credentials.to_h)
Settings.reload!
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# keys from rails' credentials are added under "credentials" (cf config_add_credentials.rb)
Empty file added config/settings/development.yml
Empty file.
Empty file added config/settings/production.yml
Empty file.
Empty file added config/settings/test.yml
Empty file.

0 comments on commit 1247605

Please sign in to comment.