From d904fe8b4fd2cc1562fb0c814a1ee3ea792c84bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 5 Oct 2022 23:37:51 +0200 Subject: [PATCH] Move subdomain logic to tenant model We had some of the logic in the ApplicationMailer. Since we're going to use this logic in more places, we're moving it to the Tenant model, which is the model handling everything related to hosts. --- app/mailers/application_mailer.rb | 16 +--------------- app/models/tenant.rb | 24 +++++++++++++++++++----- spec/lib/tasks/sitemap_spec.rb | 2 +- spec/models/tenant_spec.rb | 6 +++--- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 76541a06c..757bc32ff 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -7,24 +7,10 @@ class ApplicationMailer < ActionMailer::Base before_action :set_asset_host def default_url_options - if Tenant.default? - super - else - super.merge(host: host_with_subdomain_for(super[:host])) - end + Tenant.current_url_options end def set_asset_host self.asset_host ||= root_url.delete_suffix("/") end - - private - - def host_with_subdomain_for(host) - if host == "localhost" - "#{Tenant.current_schema}.lvh.me" - else - "#{Tenant.current_schema}.#{host}" - end - end end diff --git a/app/models/tenant.rb b/app/models/tenant.rb index b808de150..59b26591c 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -27,22 +27,36 @@ class Tenant < ApplicationRecord %w[mail public shared_extensions www] end - def self.default_host - ActionMailer::Base.default_url_options[:host] + def self.default_url_options + ActionMailer::Base.default_url_options end - def self.current_schema - Apartment::Tenant.current + def self.default_host + default_url_options[:host] end def self.current_url_options - ApplicationMailer.new.default_url_options + default_url_options.merge(host: current_host) + end + + def self.current_host + if default? + default_host + elsif default_host == "localhost" + "#{current_schema}.lvh.me" + else + "#{current_schema}.#{default_host}" + end end def self.default? current_schema == "public" end + def self.current_schema + Apartment::Tenant.current + end + def self.switch(...) Apartment::Tenant.switch(...) end diff --git a/spec/lib/tasks/sitemap_spec.rb b/spec/lib/tasks/sitemap_spec.rb index be989a92c..b6cc68c68 100644 --- a/spec/lib/tasks/sitemap_spec.rb +++ b/spec/lib/tasks/sitemap_spec.rb @@ -74,7 +74,7 @@ describe "rake sitemap:create", type: :system do end it "generates a sitemap for every tenant" do - allow(ActionMailer::Base).to receive(:default_url_options).and_return({ host: "consul.dev" }) + allow(Tenant).to receive(:default_url_options).and_return({ host: "consul.dev" }) FileUtils.rm_f(Dir.glob(Rails.root.join("public", "tenants", "*", "sitemap.xml"))) create(:tenant, schema: "debates") diff --git a/spec/models/tenant_spec.rb b/spec/models/tenant_spec.rb index c8b979023..a8f41428f 100644 --- a/spec/models/tenant_spec.rb +++ b/spec/models/tenant_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" describe Tenant do describe ".resolve_host" do before do - allow(ActionMailer::Base).to receive(:default_url_options).and_return({ host: "consul.dev" }) + allow(Tenant).to receive(:default_url_options).and_return({ host: "consul.dev" }) end it "returns nil for empty hosts" do @@ -85,7 +85,7 @@ describe Tenant do context "default host contains subdomains" do before do - allow(ActionMailer::Base).to receive(:default_url_options).and_return({ host: "demo.consul.dev" }) + allow(Tenant).to receive(:default_url_options).and_return({ host: "demo.consul.dev" }) end it "ignores subdomains already present in the default host" do @@ -120,7 +120,7 @@ describe Tenant do context "default host is similar to development and test domains" do before do - allow(ActionMailer::Base).to receive(:default_url_options).and_return({ host: "mylvh.me" }) + allow(Tenant).to receive(:default_url_options).and_return({ host: "mylvh.me" }) end it "returns nil for the default host" do