acts_as_tenant 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8891ee294c7263049d6bb44c00b66231976574ca467fbf88496510a422b751d5
4
- data.tar.gz: 580108fc430c92f9f124841243e1ac2d070c7a899e011e153d94543283ff938f
3
+ metadata.gz: e172e79670c2137d3641f2062a07e351ad5291db58ffabad5cd15e8e52850583
4
+ data.tar.gz: 3445b7da7ef300fbecaa402e351c139f4c994e0ef5c990700ac57d69a9c02c1a
5
5
  SHA512:
6
- metadata.gz: 5bfef7a385bdbb1d9264ef13a1f521d1538262858480c3c3869c3167a9761bba1c6a5fd02008948d04452c87acf49405eb0aa71630213f2d9a13982c32856605
7
- data.tar.gz: ee45c099a0e26311071f3de62317bc9425e8c9f7deb3273220d65c446f0dbdb549840551ec903a6202a75777d2f491f3a7f1808bfa08c6432844ef48da0d1da5
6
+ metadata.gz: bf02fcd6565346a41cbdb033952b25c48d79029a496239e2e785386527f0be23acbbe21983b0bc23b39f79c31752857b6eaa2f68bd9a0014f2313e7c8ff7573d
7
+ data.tar.gz: 9d1b38f89c0a0c26733effc2331d6d9adef3a523dd4333204c8c8234d4c92d363e4d923fcc7b854c517a04946362362a30760639ccbf63dbf65513b10c4ef998
@@ -118,14 +118,30 @@ module ActsAsTenant
118
118
 
119
119
  associations.each do |a|
120
120
  attr = a.foreign_key.to_sym
121
- next unless record.will_save_change_to_attribute?(attr)
122
- next if a.name == tenant.to_sym || polymorphic_foreign_keys.include?(a.foreign_key)
121
+ next if a.name == tenant.to_sym
122
+
123
+ if a.polymorphic?
124
+ next unless record.will_save_change_to_attribute?(attr) || record.will_save_change_to_attribute?(a.foreign_type)
125
+ else
126
+ # Associations sharing a polymorphic foreign key are checked through the polymorphic association
127
+ next if polymorphic_foreign_keys.include?(a.foreign_key)
128
+ next unless record.will_save_change_to_attribute?(attr)
129
+ end
123
130
 
124
131
  value = record.read_attribute_for_validation(attr)
125
132
  next if value.nil?
126
133
 
127
- relation = a.scope ? a.klass.class_eval(&a.scope) : a.klass
128
- associated = relation.find_by(a.association_primary_key => value)
134
+ klass = if a.polymorphic?
135
+ type = record.read_attribute(a.foreign_type)&.safe_constantize
136
+ type if type.is_a?(Class) && type < ActiveRecord::Base
137
+ else
138
+ a.klass
139
+ end
140
+
141
+ associated = if klass
142
+ relation = a.scope ? a.scope_for(klass.all, record) : klass
143
+ relation.find_by(a.association_primary_key(klass) => value)
144
+ end
129
145
 
130
146
  if associated.nil? || tenant_mismatch.call(record, associated)
131
147
  record.errors.add attr, "association is invalid [ActsAsTenant]"
@@ -1,3 +1,3 @@
1
1
  module ActsAsTenant
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.2"
3
3
  end
@@ -109,7 +109,7 @@ module ActsAsTenant
109
109
  def self.with_tenant(tenant, &block)
110
110
  raise ArgumentError, "block required" if block.nil?
111
111
 
112
- Current.set(current_tenant: tenant, &block)
112
+ Current.set(current_tenant: tenant) { block.call }
113
113
  end
114
114
 
115
115
  def self.without_tenant(&block)
@@ -118,7 +118,7 @@ module ActsAsTenant
118
118
  old_test_tenant = test_tenant
119
119
  self.test_tenant = nil
120
120
  begin
121
- Current.set(current_tenant: nil, acts_as_tenant_unscoped: true, &block)
121
+ Current.set(current_tenant: nil, acts_as_tenant_unscoped: true) { block.call }
122
122
  ensure
123
123
  self.test_tenant = old_test_tenant
124
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erwin Matthijssen