Skip to content

Commit d7e422f

Browse files
authored
Fix global record validations with existing scope (#294)
Due to how the `args` object was being (man)handled, we were leaking the tenant scope into the global scope validation where it shouldn't have been. This only happened when there was a scope argument supplied to validates_uniquness_to_tenant.
1 parent 00295e4 commit d7e422f

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

lib/acts_as_tenant/model_extensions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def validates_uniqueness_to_tenant(fields, args = {})
105105

106106
fkey = reflect_on_association(ActsAsTenant.tenant_klass).foreign_key
107107

108-
validation_args = args.clone
108+
validation_args = args.deep_dup
109109
validation_args[:scope] = if args[:scope]
110-
Array(args[:scope]) << fkey
110+
Array(args[:scope]) + [fkey]
111111
else
112112
fkey
113113
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class GlobalProjectWithScope < ActiveRecord::Base
2+
self.table_name = "projects"
3+
4+
acts_as_tenant :account, has_global_records: true
5+
validates_uniqueness_to_tenant :name, scope: [:user_defined_scope]
6+
end

spec/dummy/db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
create_table :projects, force: true do |t|
2222
t.column :name, :string
2323
t.column :account_id, :integer
24+
t.column :user_defined_scope, :string
2425
end
2526

2627
create_table :managers, force: true do |t|

spec/fixtures/global_projects.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ global_foo:
1111
global_bar:
1212
account: bar
1313
name: "global bar"
14+
15+
global_scope:
16+
name: "global scope"
17+
user_defined_scope: abc

spec/models/model_extensions_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@
164164
it "is invalid if tenant record conflicts with global record" do
165165
expect(GlobalProject.new(name: "global").valid?).to be(false)
166166
end
167+
168+
it "is invalid if tenant record conflicts with global record with scope" do
169+
duplicate = GlobalProjectWithScope.new(
170+
name: "global scope",
171+
user_defined_scope: "abc"
172+
)
173+
expect(duplicate.valid?).to be(false)
174+
end
167175
end
168176

169177
context "should validate global records against global & tenant records" do

0 commit comments

Comments
 (0)