Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global record validations with existing scope #294

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/acts_as_tenant/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def validates_uniqueness_to_tenant(fields, args = {})

fkey = reflect_on_association(ActsAsTenant.tenant_klass).foreign_key

validation_args = args.clone
validation_args = args.deep_dup
validation_args[:scope] = if args[:scope]
Array(args[:scope]) << fkey
Array(args[:scope]) + [fkey]
else
fkey
end
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/models/global_project_with_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class GlobalProjectWithScope < ActiveRecord::Base
self.table_name = "projects"

acts_as_tenant :account, has_global_records: true
validates_uniqueness_to_tenant :name, scope: [:user_defined_scope]
end
1 change: 1 addition & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
create_table :projects, force: true do |t|
t.column :name, :string
t.column :account_id, :integer
t.column :user_defined_scope, :string
end

create_table :managers, force: true do |t|
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/global_projects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ global_foo:
global_bar:
account: bar
name: "global bar"

global_scope:
name: "global scope"
user_defined_scope: abc
8 changes: 8 additions & 0 deletions spec/models/model_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
it "is invalid if tenant record conflicts with global record" do
expect(GlobalProject.new(name: "global").valid?).to be(false)
end

it "is invalid if tenant record conflicts with global record with scope" do
duplicate = GlobalProjectWithScope.new(
name: "global scope",
user_defined_scope: "abc"
)
expect(duplicate.valid?).to be(false)
end
end

context "should validate global records against global & tenant records" do
Expand Down