Merge pull request #5030 from consul/tenant_variants
Allow rendering different ERB files per tenant
This commit is contained in:
2
Gemfile
2
Gemfile
@@ -60,7 +60,7 @@ gem "turbolinks", "~> 5.2.1"
|
||||
gem "turnout", "~> 2.5.0"
|
||||
gem "uglifier", "~> 4.2.0"
|
||||
gem "uuidtools", "~> 2.2.0"
|
||||
gem "view_component", "~> 2.69.0"
|
||||
gem "view_component", "~> 2.78.0"
|
||||
gem "whenever", "~> 1.0.0", require: false
|
||||
gem "wicked_pdf", "~> 2.6.3"
|
||||
gem "wkhtmltopdf-binary", "~> 0.12.6"
|
||||
|
||||
@@ -678,7 +678,7 @@ GEM
|
||||
unicode-display_width (2.2.0)
|
||||
uniform_notifier (1.16.0)
|
||||
uuidtools (2.2.0)
|
||||
view_component (2.69.0)
|
||||
view_component (2.78.0)
|
||||
activesupport (>= 5.0.0, < 8.0)
|
||||
concurrent-ruby (~> 1.0)
|
||||
method_source (~> 1.0)
|
||||
@@ -810,7 +810,7 @@ DEPENDENCIES
|
||||
turnout (~> 2.5.0)
|
||||
uglifier (~> 4.2.0)
|
||||
uuidtools (~> 2.2.0)
|
||||
view_component (~> 2.69.0)
|
||||
view_component (~> 2.78.0)
|
||||
web-console (~> 4.2.0)
|
||||
webdrivers (~> 4.7.0)
|
||||
whenever (~> 1.0.0)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require "application_responder"
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
include TenantVariants
|
||||
include GlobalizeFallbacks
|
||||
include HasFilters
|
||||
include HasOrders
|
||||
|
||||
13
app/controllers/concerns/tenant_variants.rb
Normal file
13
app/controllers/concerns/tenant_variants.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module TenantVariants
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_action :set_tenant_variant
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_tenant_variant
|
||||
request.variant = Tenant.current_schema.to_sym
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,5 @@
|
||||
class Management::BaseController < ActionController::Base
|
||||
include TenantVariants
|
||||
include GlobalizeFallbacks
|
||||
layout "management"
|
||||
default_form_builder ConsulFormBuilder
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require "manager_authenticator"
|
||||
|
||||
class Management::SessionsController < ActionController::Base
|
||||
include TenantVariants
|
||||
include GlobalizeFallbacks
|
||||
include AccessDeniedHandler
|
||||
default_form_builder ConsulFormBuilder
|
||||
|
||||
@@ -3,6 +3,7 @@ class ApplicationMailer < ActionMailer::Base
|
||||
default from: proc { "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>" }
|
||||
layout "mailer"
|
||||
before_action :set_asset_host
|
||||
before_action :set_variant
|
||||
after_action :set_smtp_settings
|
||||
|
||||
def default_url_options
|
||||
@@ -13,6 +14,10 @@ class ApplicationMailer < ActionMailer::Base
|
||||
self.asset_host ||= root_url.delete_suffix("/")
|
||||
end
|
||||
|
||||
def set_variant
|
||||
lookup_context.variants = [Tenant.current_schema.to_sym]
|
||||
end
|
||||
|
||||
def set_smtp_settings
|
||||
unless Tenant.default?
|
||||
mail.delivery_method.settings.merge!(Tenant.current_secrets.smtp_settings.to_h)
|
||||
|
||||
23
spec/controllers/concerns/tenant_variants_spec.rb
Normal file
23
spec/controllers/concerns/tenant_variants_spec.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe TenantVariants do
|
||||
controller(ActionController::Base) do
|
||||
include TenantVariants
|
||||
|
||||
def index
|
||||
render plain: request.variant
|
||||
end
|
||||
end
|
||||
|
||||
it "uses the default tenant by default" do
|
||||
get :index
|
||||
expect(response.body).to eq "[:public]"
|
||||
end
|
||||
|
||||
it "uses the current tenant schema when defined" do
|
||||
allow(Tenant).to receive(:current_schema).and_return("random-name")
|
||||
|
||||
get :index
|
||||
expect(response.body).to eq '[:"random-name"]'
|
||||
end
|
||||
end
|
||||
@@ -78,4 +78,22 @@ describe ApplicationMailer do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#set_variant" do
|
||||
let(:mailer) { ApplicationMailer.new }
|
||||
|
||||
it "uses the default tenant by default" do
|
||||
mailer.set_variant
|
||||
|
||||
expect(mailer.lookup_context.variants).to eq [:public]
|
||||
end
|
||||
|
||||
it "uses the current tenant schema when defined" do
|
||||
allow(Tenant).to receive(:current_schema).and_return("random-name")
|
||||
|
||||
mailer.set_variant
|
||||
|
||||
expect(mailer.lookup_context.variants).to eq [:"random-name"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user