From 5f14c7603d4a811f31a3ca174640a09b39e61ec4 Mon Sep 17 00:00:00 2001 From: madhums Date: Tue, 12 Mar 2024 13:06:23 +0100 Subject: [PATCH] add code comments and bring back the singleton Using singleton avoids initialisation of the Settings class (which is mostly global) --- app/views/includes/_footer.html.erb | 2 +- config/initializers/settings.rb | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/views/includes/_footer.html.erb b/app/views/includes/_footer.html.erb index 719dd73..5580b3c 100644 --- a/app/views/includes/_footer.html.erb +++ b/app/views/includes/_footer.html.erb @@ -11,7 +11,7 @@ <%= link_to t('.home'), root_path %>
  • - <%= link_to t('.contact'), "mailto:#{Settings.contact_email}" %> + <%= link_to t('.contact'), "mailto:#{Settings.instance.contact_email}" %>
  • <%= link_to t('.stories'), stories_path %> diff --git a/config/initializers/settings.rb b/config/initializers/settings.rb index 62cb4d8..93ae77a 100644 --- a/config/initializers/settings.rb +++ b/config/initializers/settings.rb @@ -1,7 +1,17 @@ +# The Settings class is a singleton class which holds all configuration +# which is accessed throughout the application outside of `config` files. +# These settings mostly include environemt variables and other global settings. +# +# Similar pattern and inspiration from: +# https://garrettdimon.com/journal/posts/unified-configuration-in-rails +# +# Usage: Settings.instance.contact_email +# + class Settings - class << self - def contact_email - ENV["DOMAIN_EMAIL_ADDRESS"] - end + include Singleton + + def contact_email + ENV["DOMAIN_EMAIL_ADDRESS"] end end