From 2d9afda95ee0a722dd97cdc270dbaef686c905d8 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 16 Aug 2021 18:24:38 +0200
Subject: [PATCH] context with env vars

---
 .../application_controller/with_context.rb    | 10 +++++++
 app/models/university/with_identifier.rb      | 26 ++++++++++++++-----
 app/views/application/_footer.html.erb        |  3 +++
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/app/controllers/application_controller/with_context.rb b/app/controllers/application_controller/with_context.rb
index 7d5efa215..34f202700 100644
--- a/app/controllers/application_controller/with_context.rb
+++ b/app/controllers/application_controller/with_context.rb
@@ -14,5 +14,15 @@ module ApplicationController::WithContext
     end
     helper_method :current_website
 
+    def is_university?
+      @is_university ||= !University.with_host(request.host).nil?
+    end
+    helper_method :is_university?
+
+    def is_website?
+      @is_website ||= !Features::Websites::Site.with_host(request.host).nil?
+    end
+    helper_method :is_website?
+
   end
 end
diff --git a/app/models/university/with_identifier.rb b/app/models/university/with_identifier.rb
index 04d3a3a84..b9b0749ec 100644
--- a/app/models/university/with_identifier.rb
+++ b/app/models/university/with_identifier.rb
@@ -6,7 +6,7 @@ module University::WithIdentifier
     validates :identifier, presence: true, uniqueness: true
 
     def self.with_host(host)
-      find_by(identifier: extract_identifier_from(host)) || first
+      find_by(identifier: extract_identifier_from(host))
     end
 
     private
@@ -16,20 +16,32 @@ module University::WithIdentifier
     # Staging     osuny.osuny.dev   -> osuny
     # Dev         osuny.osuny       -> osuny
     def self.extract_identifier_from(host)
-      host.remove('.osuny.org')
-          .remove('.osuny.dev')
-          .remove('.osuny')
+      host.remove(production_domain)
+          .remove(staging_domain)
+          .remove(dev_domain)
+    end
+
+    def self.production_domain
+      ENV['OSUNY_PRODUCTION'] || '.osuny.org'
+    end
+
+    def self.staging_domain
+      ENV['OSUNY_STAGING'] || '.osuny.dev'
+    end
+
+    def self.dev_domain
+      ENV['OSUNY_DEV'] || '.osuny'
     end
   end
 
   def domain_url
     case Rails.env
     when 'development'
-      "http://#{identifier}.osuny:3000"
+      "http://#{identifier}#{University.dev_domain}:3000"
     when 'staging'
-      "https://#{identifier}.osuny.dev"
+      "https://#{identifier}#{University.staging_domain}"
     when 'production'
-      "https://#{identifier}.osuny.org"
+      "https://#{identifier}#{University.production_domain}"
     end
   end
 end
diff --git a/app/views/application/_footer.html.erb b/app/views/application/_footer.html.erb
index 3eacc69e9..ce4091c6d 100644
--- a/app/views/application/_footer.html.erb
+++ b/app/views/application/_footer.html.erb
@@ -1,5 +1,8 @@
 <footer>
   <div class="container small">
     <%= current_university %>
+
+    <p>Is University? <%= is_university? %></p>
+    <p>Is website? <%= is_website? %></p>
   </div>
 </footer>
-- 
GitLab