From ef573c5f73b09dbc1cb4aa6b842c78770a32633a Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Mon, 2 Dec 2024 08:57:10 +0100 Subject: [PATCH 1/4] Require a tenant to be configured for all queries --- config/initializers/acts_as_tenant.rb | 3 +++ db/seeds.rb | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 config/initializers/acts_as_tenant.rb diff --git a/config/initializers/acts_as_tenant.rb b/config/initializers/acts_as_tenant.rb new file mode 100644 index 0000000..2813ace --- /dev/null +++ b/config/initializers/acts_as_tenant.rb @@ -0,0 +1,3 @@ +ActsAsTenant.configure do |config| + config.require_tenant = true +end \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index e493f6b..e0a11a6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -2,13 +2,15 @@ NUMBER_OF_GUESTS = 50 -TablesArrangement.delete_all -Expense.delete_all -Guest.delete_all -Group.delete_all - -Wedding.delete_all -wedding = Wedding.create!(slug: :default, date: 1.year.from_now) +ActsAsTenant.without_tenant do + TablesArrangement.delete_all + Expense.delete_all + Guest.delete_all + Group.delete_all + + Wedding.delete_all + wedding = Wedding.create!(slug: :default, date: 1.year.from_now) +end ActsAsTenant.with_tenant(wedding) do Expense.create!(name: 'Photographer', amount: 3000, pricing_type: 'fixed') From 3fca449461aa1649ac874c39c36b56850f4f2efa Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Mon, 2 Dec 2024 09:04:48 +0100 Subject: [PATCH 2/4] Limit visibility per tenant --- app/controllers/application_controller.rb | 3 ++- db/seeds.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 15a4b17..8343f65 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,7 @@ # Copyright (C) 2024 Manuel Bustillo class ApplicationController < ActionController::Base + set_current_tenant_through_filter before_action :set_tenant before_action :authenticate_user! after_action :set_csrf_cookie @@ -47,7 +48,7 @@ class ApplicationController < ActionController::Base end def set_tenant - ActsAsTenant.current_tenant = Wedding.find_by(slug: params[:slug]) + set_current_tenant(Wedding.find_by!(slug: params[:slug])) end def development_swagger? diff --git a/db/seeds.rb b/db/seeds.rb index e0a11a6..9d93073 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,9 +9,10 @@ ActsAsTenant.without_tenant do Group.delete_all Wedding.delete_all - wedding = Wedding.create!(slug: :default, date: 1.year.from_now) end +wedding = Wedding.create!(slug: :default, date: 1.year.from_now) + ActsAsTenant.with_tenant(wedding) do Expense.create!(name: 'Photographer', amount: 3000, pricing_type: 'fixed') Expense.create!(name: 'Country house', amount: 6000, pricing_type: 'fixed') From 3ea1d1e7ecd068d1542b89a103bd36693252e28b Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Mon, 2 Dec 2024 08:05:46 +0000 Subject: [PATCH 3/4] Add copyright notice --- config/initializers/acts_as_tenant.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/initializers/acts_as_tenant.rb b/config/initializers/acts_as_tenant.rb index 2813ace..2117f9c 100644 --- a/config/initializers/acts_as_tenant.rb +++ b/config/initializers/acts_as_tenant.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + ActsAsTenant.configure do |config| config.require_tenant = true end \ No newline at end of file From a96be2a79ef6c9227c1299b1bbd62d4bcd132143 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Mon, 2 Dec 2024 20:33:00 +0100 Subject: [PATCH 4/4] Do not require a tenant scope for running tests --- config/initializers/acts_as_tenant.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/acts_as_tenant.rb b/config/initializers/acts_as_tenant.rb index 2117f9c..c535737 100644 --- a/config/initializers/acts_as_tenant.rb +++ b/config/initializers/acts_as_tenant.rb @@ -1,5 +1,5 @@ # Copyright (C) 2024 Manuel Bustillo ActsAsTenant.configure do |config| - config.require_tenant = true + config.require_tenant = !Rails.env.test? end \ No newline at end of file