From 12476051a787f0d26a32f551ea7fc167dd8d83df Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Feldis <5403+jbfeldis@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:42:21 +0200 Subject: [PATCH] Installe et configure le gem config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .gitignore | 4 ++ Gemfile | 2 + Gemfile.lock | 4 ++ config/database.yml | 6 +- config/initializers/config.rb | 68 +++++++++++++++++++ config/initializers/config_add_credentials.rb | 2 + config/settings.yml | 1 + config/settings/development.yml | 0 config/settings/production.yml | 0 config/settings/test.yml | 0 10 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 config/initializers/config.rb create mode 100644 config/initializers/config_add_credentials.rb create mode 100644 config/settings.yml create mode 100644 config/settings/development.yml create mode 100644 config/settings/production.yml create mode 100644 config/settings/test.yml diff --git a/.gitignore b/.gitignore index 612a308a..eb204517 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,7 @@ !/app/assets/builds/.keep /config/credentials/development.key + +config/settings.local.yml +config/settings/*.local.yml +config/environments/*.local.yml diff --git a/Gemfile b/Gemfile index d8a7b72b..459f5795 100644 --- a/Gemfile +++ b/Gemfile @@ -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] diff --git a/Gemfile.lock b/Gemfile.lock index f54f9ad1..654dd553 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -308,6 +311,7 @@ PLATFORMS DEPENDENCIES bootsnap capybara + config debug importmap-rails jbuilder diff --git a/config/database.yml b/config/database.yml index 83ab7e92..c4fd1f45 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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 @@ -83,5 +83,3 @@ test: production: <<: *default database: quotient_familial_production - username: quotient_familial - password: <%= ENV["QUOTIENT_FAMILIAL_DATABASE_PASSWORD"] %> diff --git a/config/initializers/config.rb b/config/initializers/config.rb new file mode 100644 index 00000000..115223e8 --- /dev/null +++ b/config/initializers/config.rb @@ -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 diff --git a/config/initializers/config_add_credentials.rb b/config/initializers/config_add_credentials.rb new file mode 100644 index 00000000..87232e21 --- /dev/null +++ b/config/initializers/config_add_credentials.rb @@ -0,0 +1,2 @@ +Settings.add_source!(credentials: Rails.application.credentials.to_h) +Settings.reload! diff --git a/config/settings.yml b/config/settings.yml new file mode 100644 index 00000000..e48045e8 --- /dev/null +++ b/config/settings.yml @@ -0,0 +1 @@ +# keys from rails' credentials are added under "credentials" (cf config_add_credentials.rb) diff --git a/config/settings/development.yml b/config/settings/development.yml new file mode 100644 index 00000000..e69de29b diff --git a/config/settings/production.yml b/config/settings/production.yml new file mode 100644 index 00000000..e69de29b diff --git a/config/settings/test.yml b/config/settings/test.yml new file mode 100644 index 00000000..e69de29b