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

This commit is contained in:
Jyrki Gadinger 2024-08-09 11:12:38 +02:00 committed by Andreas Nedbal
parent 076f71860f
commit ca98b9dd7f
6 changed files with 36 additions and 26 deletions

View File

@ -35,6 +35,6 @@ class User::RegistrationsController < Devise::RegistrationsController
end end
def redirect_if_registrations_disabled! 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
end end

View File

@ -5,7 +5,7 @@
= render "layouts/messages" = render "layouts/messages"
%h1= APP_CONFIG["site_name"] %h1= APP_CONFIG["site_name"]
%p= t(".subtitle") %p= t(".subtitle")
- if APP_CONFIG.dig(:features, :registration, :enabled) - if Retrospring::Config.registrations_enabled?
%p %p
%a.btn.btn-outline-light.btn-lg{ href: url_for(new_user_registration_path) } %a.btn.btn-outline-light.btn-lg{ href: url_for(new_user_registration_path) }
= t(".register") = t(".register")

View File

@ -40,11 +40,11 @@
%input{ name: "qb-to", type: "hidden", value: user.id }/ %input{ name: "qb-to", type: "hidden", value: user.id }/
%button.btn.btn-primary{ name: "qb-ask", %button.btn.btn-primary{ name: "qb-ask",
type: :button, 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 Ask
- unless user_signed_in? - unless user_signed_in?
- if user.privacy_allow_anonymous_questions? - if user.privacy_allow_anonymous_questions?
- if APP_CONFIG.dig(:features, :registration, :enabled) - if Retrospring::Config.registrations_enabled?
.d-none#question-box-promote .d-none#question-box-promote
.row .row
.col-12.text-center .col-12.text-center
@ -61,7 +61,7 @@
%small= t(".promote.join", app_title: APP_CONFIG["site_name"]) %small= t(".promote.join", app_title: APP_CONFIG["site_name"])
- else - else
.text-center .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)) %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 - else
%strong= t(".status.non_anonymous_no_registration_html", sign_in: link_to(t("voc.login"), new_user_session_path)) %strong= t(".status.non_anonymous_no_registration_html", sign_in: link_to(t("voc.login"), new_user_session_path))

View File

@ -14,5 +14,5 @@
.collapse.navbar-collapse#j2-main-navbar-collapse .collapse.navbar-collapse#j2-main-navbar-collapse
%ul.nav.navbar-nav.ms-auto %ul.nav.navbar-nav.ms-auto
= nav_entry t("voc.login"), new_user_session_path = 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 = nav_entry t("voc.register"), new_user_registration_path

View File

@ -1,23 +1,4 @@
# frozen_string_literal: true # frozen_string_literal: true
# Auxiliary config # Auxiliary config
APP_CONFIG = Retrospring::Config.config_hash
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"],
}

29
lib/retrospring/config.rb Normal file
View File

@ -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