Single database for multiple shops

Use ActsAsTenant as Multi-tenancy for shops

See below files:
- app/controllers/concern/multi_tenancy.rb
- app/models/application_record.rb
- app/models/shop.rb

An initializer can be created to control option in ActsAsTenant.
config/initializers/acts_as_tenant.rb
require 'acts_as_tenant/sidekiq'
ActsAsTenant.configure do |config|
  config.require_tenant = false # true
end

more details: https://github.com/ErwinM/acts_as_tenant
This commit is contained in:
Thein Lin Kyaw
2019-12-02 17:19:28 +06:30
parent 5d10d4b6a1
commit 937f40e7c1
78 changed files with 215 additions and 173 deletions

View File

@@ -3,7 +3,7 @@ module LoginVerification
included do
before_action :authenticate_session_token
helper_method :current_company,:current_shop, :current_login_employee, :current_user, :get_cashier, :order_reservation, :bank_integration, :shop_detail
helper_method :current_company, :current_shop, :current_login_employee, :current_user, :get_cashier, :order_reservation, :bank_integration, :shop_detail
end
#this is base api base controller to need to inherit.
@@ -19,7 +19,7 @@ module LoginVerification
def current_shop
begin
return Shop.first
return Shop.current_shop
rescue
return nil
end
@@ -41,7 +41,7 @@ module LoginVerification
#Shop Name in Navbor
def shop_detail
@shop = Shop.first
@shop = current_shop
end
#check order reservation used
@@ -78,11 +78,11 @@ module LoginVerification
protected
# Authenticate the user with token based authentication
def authenticate
authenticate_session_token || render_unauthorized
def authenticate
authenticate_session_token || render_unauthorized
end
def authenticate_session_token
def authenticate_session_token
token = session[:session_token]
if (token)
#@current_user = User.find_by(api_key: token)

View File

@@ -0,0 +1,20 @@
module MultiTenancy
extend ActiveSupport::Concern
included do
set_current_tenant_through_filter if respond_to? :set_current_tenant_through_filter
before_action :find_shop_by_subdomain_or_frist if respond_to? :before_action
helper_method :current_shop if respond_to? :helper_method
end
private
def find_shop_by_subdomain_or_frist
if request.subdomain.present?
shop_code = request.subdomain.partition('-').last
shop = Shop.find_by(shop_code: shop_code)
else
shop = Shop.first
end
set_current_tenant(shop)
end
end