Skip to content

Commit

Permalink
chore: extract whitelabel detection into middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
salzig committed Jan 25, 2025
1 parent a90cd06 commit bac7a73
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 24 deletions.
2 changes: 0 additions & 2 deletions app/controllers/admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# you're free to overwrite the RESTful controller actions.
module Admin
class ApplicationController < Administrate::ApplicationController
include WhitelabelDetection
include UserHandling
include LocaleDetection
include TimeZoneDetection
Expand All @@ -26,7 +25,6 @@ class ApplicationController < Administrate::ApplicationController
private

def setup
switch_label
switch_locale
switch_time_zone
end
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
include WhitelabelDetection
include LocaleDetection
include TimeZoneDetection
include UserHandling
Expand All @@ -23,7 +22,6 @@ class ApplicationController < ActionController::Base
protected

def setup
switch_label
switch_locale
switch_time_zone
end
Expand Down
18 changes: 0 additions & 18 deletions app/controllers/concerns/whitelabel_detection.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/controllers/super_admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# you're free to overwrite the RESTful controller actions.
module SuperAdmin
class ApplicationController < Administrate::ApplicationController
include WhitelabelDetection
include UserHandling
include LocaleDetection
include TimeZoneDetection
Expand All @@ -26,7 +25,6 @@ class ApplicationController < Administrate::ApplicationController
private

def setup
switch_label
switch_locale
switch_time_zone
end
Expand Down
28 changes: 28 additions & 0 deletions app/lib/whitelabel_detection_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class WhitelabelDetectionMiddleware
def initialize(app)
@app = app
end

def call(env)
Whitelabel.reset!
request = ActionDispatch::Request.new(env)
Whitelabel.label = detect_label(request)
@app.call(env)
end

private

def detect_label(request)
Whitelabel.label_for(request.subdomains.first).tap { |label| return label unless label.nil? }

Whitelabel.labels.find do |label|
label.domains&.any? do |custom_domain|
return label if request.host =~ /#{custom_domain}/
end
end

Whitelabel.labels.first
end
end
2 changes: 2 additions & 0 deletions config/initializers/whitelabel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Usergroup.initialize!

I18n.extend WhitelabelTranslation

Rails.application.config.middleware.insert_before OmniAuth::Builder, WhitelabelDetectionMiddleware
29 changes: 29 additions & 0 deletions spec/lib/whitelabel_detection_middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'
require 'whitelabel_detection_middleware'

describe WhitelabelDetectionMiddleware do
subject(:middleware) { described_class.new(dummy_app) }

let(:dummy_app) { ->(_env) { response } }
let(:host) { 'localhost' }
let(:rack_env) { { 'HTTP_HOST' => host } }
let(:response) { [418, { 'Content-Type' => 'text/plain' }, ['418 No coffee here.']] }

before { middleware.call(rack_env) }

{
'hamburg.onruby.de' => 'hamburg',
'onruby.eu' => 'hamburg',
'dresdenrb.onruby.de' => 'dresdenrb',
'tallinn.ruby.ee' => 'tallinn_rug',
'www.rug-b.de' => 'berlin',
}.each do |host, label|
context "with host '#{host}'" do
let(:host) { host }

it "uses Whitelabel '#{label}'" do
expect(Whitelabel[:label_id]).to eq(label)
end
end
end
end

0 comments on commit bac7a73

Please sign in to comment.