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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user