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
|
before_action :set_asset_host
|
||||||
|
|
||||||
def default_url_options
|
def default_url_options
|
||||||
if Tenant.default?
|
Tenant.current_url_options
|
||||||
super
|
|
||||||
else
|
|
||||||
super.merge(host: host_with_subdomain_for(super[:host]))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_asset_host
|
def set_asset_host
|
||||||
self.asset_host ||= root_url.delete_suffix("/")
|
self.asset_host ||= root_url.delete_suffix("/")
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def host_with_subdomain_for(host)
|
|
||||||
if host == "localhost"
|
|
||||||
"#{Tenant.current_schema}.lvh.me"
|
|
||||||
else
|
|
||||||
"#{Tenant.current_schema}.#{host}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -27,22 +27,36 @@ class Tenant < ApplicationRecord
|
|||||||
%w[mail public shared_extensions www]
|
%w[mail public shared_extensions www]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.default_host
|
def self.default_url_options
|
||||||
ActionMailer::Base.default_url_options[:host]
|
ActionMailer::Base.default_url_options
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.current_schema
|
def self.default_host
|
||||||
Apartment::Tenant.current
|
default_url_options[:host]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.current_url_options
|
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
|
end
|
||||||
|
|
||||||
def self.default?
|
def self.default?
|
||||||
current_schema == "public"
|
current_schema == "public"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.current_schema
|
||||||
|
Apartment::Tenant.current
|
||||||
|
end
|
||||||
|
|
||||||
def self.switch(...)
|
def self.switch(...)
|
||||||
Apartment::Tenant.switch(...)
|
Apartment::Tenant.switch(...)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ describe "rake sitemap:create", type: :system do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "generates a sitemap for every tenant" do
|
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")))
|
FileUtils.rm_f(Dir.glob(Rails.root.join("public", "tenants", "*", "sitemap.xml")))
|
||||||
|
|
||||||
create(:tenant, schema: "debates")
|
create(:tenant, schema: "debates")
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require "rails_helper"
|
|||||||
describe Tenant do
|
describe Tenant do
|
||||||
describe ".resolve_host" do
|
describe ".resolve_host" do
|
||||||
before 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
|
end
|
||||||
|
|
||||||
it "returns nil for empty hosts" do
|
it "returns nil for empty hosts" do
|
||||||
@@ -85,7 +85,7 @@ describe Tenant do
|
|||||||
|
|
||||||
context "default host contains subdomains" do
|
context "default host contains subdomains" do
|
||||||
before 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
|
end
|
||||||
|
|
||||||
it "ignores subdomains already present in the default host" do
|
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
|
context "default host is similar to development and test domains" do
|
||||||
before 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
|
end
|
||||||
|
|
||||||
it "returns nil for the default host" do
|
it "returns nil for the default host" do
|
||||||
|
|||||||
Reference in New Issue
Block a user