Skip to content

Commit

Permalink
update logic inside the tenant_klass_defined? method (#202)
Browse files Browse the repository at this point in the history
* fix logic inside the tenant_klass_defined method

* add tests for tenant_klass_defined?

* fix failed tests

* rubocop -a

* remove unnecessary word

* add a test case for the following
#105

* test a more appropriate class

* add multi_tenant

* Fixes rubocop warnings
---------

Co-authored-by: Gürkan İndibay <[email protected]>
  • Loading branch information
msasaki666 and gurkanindibay authored Jun 16, 2023
1 parent fd42dbd commit bed29b1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/activerecord-multi-tenant/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def inherited(subclass)
partition_key = @partition_key

# Create an implicit belongs_to association only if tenant class exists
if MultiTenant.tenant_klass_defined?(tenant_name)
if MultiTenant.tenant_klass_defined?(tenant_name, options)
belongs_to tenant_name, **options.slice(:class_name, :inverse_of, :optional)
.merge(foreign_key: options[:partition_key])
end
Expand Down Expand Up @@ -103,7 +103,7 @@ def inherited(subclass)
tenant_id
end

if MultiTenant.tenant_klass_defined?(tenant_name)
if MultiTenant.tenant_klass_defined?(tenant_name, options)
define_method "#{tenant_name}=" do |model|
super(model)
if send("#{partition_key}_changed?") && persisted? && !send("#{partition_key}_was").nil?
Expand Down
9 changes: 7 additions & 2 deletions lib/activerecord-multi-tenant/multi_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ class Current < ::ActiveSupport::CurrentAttributes
attribute :tenant
end

def self.tenant_klass_defined?(tenant_name)
!!tenant_name.to_s.classify.safe_constantize
def self.tenant_klass_defined?(tenant_name, options = {})
class_name = if options[:class_name].present?
options[:class_name]
else
tenant_name.to_s.classify
end
!!class_name.safe_constantize
end

def self.partition_key(tenant_name)
Expand Down
54 changes: 54 additions & 0 deletions spec/activerecord-multi-tenant/multi_tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,58 @@
end
end
end

describe '.tenant_klass_defined?' do
context 'without options' do
before(:all) do
class SampleTenant < ActiveRecord::Base
multi_tenant :sample_tenant
end
end

it 'return true with valid tenant_name' do
expect(MultiTenant.tenant_klass_defined?(:sample_tenant)).to eq(true)
end

it 'return false with invalid_tenant_name' do
invalid_tenant_name = :tenant
expect(MultiTenant.tenant_klass_defined?(invalid_tenant_name)).to eq(false)
end
end

context 'with options' do
context 'and valid class_name' do
it 'return true' do
class SampleTenant < ActiveRecord::Base
multi_tenant :tenant
end

tenant_name = :tenant
options = {
class_name: 'SampleTenant'
}
expect(MultiTenant.tenant_klass_defined?(tenant_name, options)).to eq(true)
end

it 'return true when tenant class is nested' do
module SampleModule
class SampleNestedTenant < ActiveRecord::Base
multi_tenant :tenant
end
# rubocop:disable Layout/TrailingWhitespace
# Trailing whitespace is intentionally left here

class AnotherTenant < ActiveRecord::Base
end
# rubocop:enable Layout/TrailingWhitespace
end
tenant_name = :tenant
options = {
class_name: 'SampleModule::SampleNestedTenant'
}
expect(MultiTenant.tenant_klass_defined?(tenant_name, options)).to eq(true)
end
end
end
end
end

0 comments on commit bed29b1

Please sign in to comment.