acts_as_tenant 2.0.1 → 2.1.0

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: a241e056fc0e64709f94c3b8df2d91d192fa904a990bff28e951fceec7071351
4
- data.tar.gz: d57c2ebb29e624ccacaa4b031214539a2a28363da65a653a9a75172e9f3a29dd
3
+ metadata.gz: 83cdd10871addd92f256ed17b6e49a841de127487d7e165011348a40a0185f01
4
+ data.tar.gz: b45e0af7799acd0fc982c1af602e3384dd2637b930901cfd4f731263a7cf5c7f
5
5
  SHA512:
6
- metadata.gz: ed5452f120dc6beaafee9952a3850d0378a212a081eecc5783115733e045e3e7658827bc0992e9e97101990751e3cfae5940379cee24d7a272e123ecfa4d51d6
7
- data.tar.gz: b4890ebc99294ece2e1e487c088800c7c5374baeee4d67a0e538e71051079589455562452bb1cf62d6f9d910777b29fbc4beab5f93da52250a8817577fbaa090
6
+ metadata.gz: 5a3373802ec3b706290d9277c7a3704055f15ebd4028d29d3b797b1f228a62f2d8168a0ce357a5ae8a25f8748c63eab735545ba007807fe5c2f96a1dc76ab4a7
7
+ data.tar.gz: aa31a475db70064a9a8edeae329456b82e7225a000008d5fa93ef2c9c265b7c8ae1e59c0111172d26232e037d19a7e1324f2e176c981fbc070144f0b3c906424
data/README.md CHANGED
@@ -210,6 +210,20 @@ New records are assigned the current tenant unless they already have one. A reco
210
210
 
211
211
  When no tenant is set (for example in an admin panel or inside `without_tenant`), `belongs_to` associations to tenanted models are validated to belong to the same tenant as the record. Associated records without a tenant, such as global records, are allowed.
212
212
 
213
+ The validation error messages can be overridden in your locale files, using the same lookup as other Active Record errors:
214
+
215
+ ```yaml
216
+ en:
217
+ activerecord:
218
+ errors:
219
+ messages:
220
+ acts_as_tenant:
221
+ tenant_mismatch: "must be the current tenant"
222
+ association_invalid: "is invalid"
223
+ ```
224
+
225
+ Messages can also be overridden for one model or one attribute, such as `activerecord.errors.models.task.acts_as_tenant.association_invalid` or `activerecord.errors.models.task.attributes.project_id.acts_as_tenant.association_invalid`.
226
+
213
227
  ### Validating attribute uniqueness
214
228
 
215
229
  If you need to validate for uniqueness, chances are that you want to scope this validation to a tenant. You can do so by using:
@@ -0,0 +1,7 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ acts_as_tenant:
6
+ tenant_mismatch: "must be the current tenant [ActsAsTenant]"
7
+ association_invalid: "association is invalid [ActsAsTenant]"
@@ -107,7 +107,7 @@ module ActsAsTenant
107
107
  matches = record_id.to_s == current_tenant.public_send(pkey).to_s
108
108
  matches &&= record_type == current_tenant.class.polymorphic_name if options[:polymorphic]
109
109
 
110
- record.errors.add(fkey, "must be the current tenant [ActsAsTenant]") unless matches
110
+ record.errors.add(fkey, :"acts_as_tenant.tenant_mismatch") unless matches
111
111
  end
112
112
 
113
113
  # Associations are looked up at validation time so belongs_to associations
@@ -118,17 +118,33 @@ 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.scope_for(a.klass.all, record) : 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
- record.errors.add attr, "association is invalid [ActsAsTenant]"
147
+ record.errors.add(attr, :"acts_as_tenant.association_invalid")
132
148
  end
133
149
  end
134
150
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsTenant
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -137,6 +137,10 @@ module ActsAsTenant
137
137
  end
138
138
  end
139
139
 
140
+ ActiveSupport.on_load(:i18n) do
141
+ I18n.load_path << File.expand_path("acts_as_tenant/locale/en.yml", __dir__)
142
+ end
143
+
140
144
  ActiveSupport.on_load(:active_record) do |base|
141
145
  base.include ActsAsTenant::ModelExtensions
142
146
  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.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erwin Matthijssen
@@ -44,6 +44,7 @@ files:
44
44
  - lib/acts_as_tenant/controller_extensions/subdomain.rb
45
45
  - lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb
46
46
  - lib/acts_as_tenant/errors.rb
47
+ - lib/acts_as_tenant/locale/en.yml
47
48
  - lib/acts_as_tenant/model_extensions.rb
48
49
  - lib/acts_as_tenant/sidekiq.rb
49
50
  - lib/acts_as_tenant/tenant_helper.rb