From ca98b9dd7f96690b99914496259961808f7643d7 Mon Sep 17 00:00:00 2001 From: Jyrki Gadinger Date: Fri, 9 Aug 2024 11:12:38 +0200 Subject: [PATCH] move site config from initialiser to own module for potential improvements in how to access common configs when needed ... way better than using `.dig` by hand everywhere i'd say --- .../user/registrations_controller.rb | 2 +- app/views/about/index.html.haml | 2 +- app/views/application/_questionbox.html.haml | 6 ++-- app/views/navigation/_guest.html.haml | 2 +- config/initializers/10_config.rb | 21 +------------- lib/retrospring/config.rb | 29 +++++++++++++++++++ 6 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 lib/retrospring/config.rb diff --git a/app/controllers/user/registrations_controller.rb b/app/controllers/user/registrations_controller.rb index 721225dc..b7b633b8 100644 --- a/app/controllers/user/registrations_controller.rb +++ b/app/controllers/user/registrations_controller.rb @@ -35,6 +35,6 @@ class User::RegistrationsController < Devise::RegistrationsController end def redirect_if_registrations_disabled! - redirect_to root_path unless APP_CONFIG.dig(:features, :registration, :enabled) + redirect_to root_path unless Retrospring::Config.registrations_enabled? end end diff --git a/app/views/about/index.html.haml b/app/views/about/index.html.haml index f35cb2c9..93e9a195 100644 --- a/app/views/about/index.html.haml +++ b/app/views/about/index.html.haml @@ -5,7 +5,7 @@ = render "layouts/messages" %h1= APP_CONFIG["site_name"] %p= t(".subtitle") - - if APP_CONFIG.dig(:features, :registration, :enabled) + - if Retrospring::Config.registrations_enabled? %p %a.btn.btn-outline-light.btn-lg{ href: url_for(new_user_registration_path) } = t(".register") diff --git a/app/views/application/_questionbox.html.haml b/app/views/application/_questionbox.html.haml index 33ecf06f..ba51deaf 100644 --- a/app/views/application/_questionbox.html.haml +++ b/app/views/application/_questionbox.html.haml @@ -40,11 +40,11 @@ %input{ name: "qb-to", type: "hidden", value: user.id }/ %button.btn.btn-primary{ name: "qb-ask", type: :button, - data: { loading_text: t(".load"), promote: user_signed_in? || !user_signed_in? && !APP_CONFIG.dig(:features, :registration, :enabled) ? "false" : "true", "character-count-target": "action" } } + data: { loading_text: t(".load"), promote: user_signed_in? || !user_signed_in? && !Retrospring::Config.registrations_enabled ? "false" : "true", "character-count-target": "action" } } Ask - unless user_signed_in? - if user.privacy_allow_anonymous_questions? - - if APP_CONFIG.dig(:features, :registration, :enabled) + - if Retrospring::Config.registrations_enabled? .d-none#question-box-promote .row .col-12.text-center @@ -61,7 +61,7 @@ %small= t(".promote.join", app_title: APP_CONFIG["site_name"]) - else .text-center - - if APP_CONFIG.dig(:features, :registration, :enabled) + - if Retrospring::Config.registrations_enabled? %strong= t(".status.non_anonymous_html", sign_in: link_to(t("voc.login"), new_user_session_path), sign_up: link_to(t("voc.register"), new_user_registration_path)) - else %strong= t(".status.non_anonymous_no_registration_html", sign_in: link_to(t("voc.login"), new_user_session_path)) diff --git a/app/views/navigation/_guest.html.haml b/app/views/navigation/_guest.html.haml index a06fab83..69bf3a62 100644 --- a/app/views/navigation/_guest.html.haml +++ b/app/views/navigation/_guest.html.haml @@ -14,5 +14,5 @@ .collapse.navbar-collapse#j2-main-navbar-collapse %ul.nav.navbar-nav.ms-auto = nav_entry t("voc.login"), new_user_session_path - - if APP_CONFIG.dig(:features, :registration, :enabled) + - if Retrospring::Config.registrations_enabled? = nav_entry t("voc.register"), new_user_registration_path diff --git a/config/initializers/10_config.rb b/config/initializers/10_config.rb index deba98c4..6a366cef 100644 --- a/config/initializers/10_config.rb +++ b/config/initializers/10_config.rb @@ -1,23 +1,4 @@ # frozen_string_literal: true # Auxiliary config - -APP_CONFIG = {}.with_indifferent_access - -# load yml config if it's present -justask_yml_path = Rails.root.join("config/justask.yml") -APP_CONFIG.merge!(YAML.load_file(justask_yml_path)) if File.exist?(justask_yml_path) - -# load config from ENV where possible -env_config = { - # The site name, shown everywhere - site_name: ENV.fetch("SITE_NAME", nil), - - hostname: ENV.fetch("HOSTNAME", nil), -}.compact -APP_CONFIG.merge!(env_config) - -# Update rails config for mail -Rails.application.config.action_mailer.default_url_options = { - host: APP_CONFIG["hostname"], -} +APP_CONFIG = Retrospring::Config.config_hash diff --git a/lib/retrospring/config.rb b/lib/retrospring/config.rb new file mode 100644 index 00000000..a4c7fbd4 --- /dev/null +++ b/lib/retrospring/config.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Retrospring + module Config + module_function + + def config_hash = {}.with_indifferent_access.tap do |hash| + # load yml config if it's present + justask_yml_path = Rails.root.join("config/justask.yml") + hash.merge!(YAML.load_file(justask_yml_path)) if File.exist?(justask_yml_path) + + # load config from ENV where possible + env_config = { + # The site name, shown everywhere + site_name: ENV.fetch("SITE_NAME", nil), + + hostname: ENV.fetch("HOSTNAME", nil), + }.compact + hash.merge!(env_config) + + # Update rails config for mail + Rails.application.config.action_mailer.default_url_options = { + host: hash["hostname"], + } + end + + def registrations_enabled? = APP_CONFIG.dig(:features, :registration, :enabled) + end +end