Skip to content

Commit be86ddc

Browse files
refactor: remove cross tenant configuration
1 parent b2819c0 commit be86ddc

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

lib/active_record/tenanted/cross_tenant_associations.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ module CrossTenantAssociations
66
extend ActiveSupport::Concern
77

88
class_methods do
9-
# I think we can have more configs later
10-
def cross_tenant_config(**config)
11-
@cross_tenant_config = config
12-
end
13-
14-
def get_cross_tenant_config
15-
@cross_tenant_config ||= {}
16-
end
17-
189
def has_one(name, scope = nil, **options)
1910
define_enhanced_association(:has_one, name, scope, **options)
2011
end
@@ -26,9 +17,8 @@ def has_many(name, scope = nil, **options)
2617
private
2718
# For now association methods are identical
2819
def define_enhanced_association(association_type, name, scope, **options)
29-
config = get_cross_tenant_config
30-
tenant_column = config[:tenant_column] || :tenant_id
31-
custom_options = options.merge(tenant_column: tenant_column)
20+
tenant_key = options.delete(:tenant_key)
21+
custom_options = { tenant_key: tenant_key || :tenant_id }
3222

3323
enhanced_scope = enhance_cross_tenant_association(name, scope, custom_options)
3424
method(association_type).super_method.call(name, enhanced_scope, **options)
@@ -40,11 +30,11 @@ def enhance_cross_tenant_association(name, scope, options)
4030
return scope unless target_class
4131

4232
unless target_class.tenanted?
43-
tenant_column = options[:tenant_column]
33+
tenant_key = options[:tenant_key]
4434

4535
return ->(record) {
4636
base_scope = scope ? target_class.instance_exec(&scope) : target_class.all
47-
base_scope.where(tenant_column => record.tenant)
37+
base_scope.where(tenant_key => record.tenant)
4838
}
4939
end
5040

test/unit/tenant_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,7 @@
657657
ActiveRecord::Base.connection.add_column :announcements, :custom_tenant_id, :string
658658
ActiveRecord::Base.connection.add_column :announcements, :user_id, :integer
659659

660-
User.cross_tenant_config(tenant_column: :custom_tenant_id)
661-
User.has_one :announcement, class_name: "Announcement"
660+
User.has_one :announcement, class_name: "Announcement", tenant_key: :custom_tenant_id
662661
Announcement.belongs_to :user
663662
end
664663

@@ -688,8 +687,7 @@
688687
ActiveRecord::Base.connection.add_column :announcements, :custom_tenant_id, :string
689688
ActiveRecord::Base.connection.add_column :announcements, :user_id, :integer
690689

691-
User.cross_tenant_config(tenant_column: :custom_tenant_id)
692-
User.has_many :announcements, class_name: "Announcement"
690+
User.has_many :announcements, class_name: "Announcement", tenant_key: :custom_tenant_id
693691
Announcement.belongs_to :user
694692
end
695693

0 commit comments

Comments
 (0)