Skip to content

Commit

Permalink
Move routing constraints to BopsCore engine
Browse files Browse the repository at this point in the history
This is so they can be reused in other engines.
  • Loading branch information
pixeltrix committed Jan 16, 2025
1 parent 6d7ebf7 commit 3462fac
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative "boot"
require_relative "../lib/constraints/subdomain"

require "rails"
# Pick the frameworks you want:
Expand Down
12 changes: 6 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

Rails.application.routes.draw do
extend BopsCore::Routing

get :healthcheck, to: proc { [200, {}, %w[OK]] }

constraints Constraints::DeviseSubdomain do
devise_subdomain do
devise_for :users, controllers: {
sessions: "users/sessions",
confirmations: "confirmations"
Expand All @@ -16,7 +18,7 @@
end
end

constraints Constraints::LocalAuthoritySubdomain do
local_authority_subdomain do
root to: "planning_applications#index"

concern :positionable do |options|
Expand Down Expand Up @@ -382,11 +384,9 @@
end
end

constraints Constraints::ConfigSubdomain do
config_subdomain do
mount BopsConfig::Engine, at: "/", as: :bops_config
end

constraints Constraints::UploadsSubdomain do
mount BopsUploads::Engine, at: "/", as: :bops_uploads
end
mount BopsUploads::Engine, at: "/", as: :bops_uploads
end
1 change: 1 addition & 0 deletions engines/bops_core/lib/bops_core.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "bops_core/engine"
require "bops_core/routing"

module BopsCore
class << self
Expand Down
55 changes: 55 additions & 0 deletions engines/bops_core/lib/bops_core/routing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

module BopsCore
module Routing
extend ActiveSupport::Concern

class LocalAuthoritySubdomain
class << self
def matches?(request)
LocalAuthority.pluck(:subdomain).include?(request.subdomain)
end
end
end

class ConfigSubdomain
class << self
def matches?(request)
request.subdomain == "config"
end
end
end

class DeviseSubdomain
class << self
def matches?(request)
ConfigSubdomain.matches?(request) || LocalAuthoritySubdomain.matches?(request)
end
end
end

class UploadsSubdomain
class << self
def matches?(request)
request.subdomain == "uploads"
end
end
end

def local_authority_subdomain(&)
constraints(LocalAuthoritySubdomain, &)
end

def config_subdomain(&)
constraints(ConfigSubdomain, &)
end

def devise_subdomain(&)
constraints(DeviseSubdomain, &)
end

def uploads_subdomain(&)
constraints(UploadsSubdomain, &)
end
end
end
6 changes: 5 additions & 1 deletion engines/bops_uploads/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

BopsUploads::Engine.routes.draw do
get "/:key", to: "files#show", as: "file"
extend BopsCore::Routing

uploads_subdomain do
get "/:key", to: "files#show", as: "file"
end
end

Rails.application.routes.draw do
Expand Down
35 changes: 0 additions & 35 deletions lib/constraints/subdomain.rb

This file was deleted.

0 comments on commit 3462fac

Please sign in to comment.