Skip to content

Commit

Permalink
Merge pull request #20614 from d-m-u/tenant_uniqueness_when_changed
Browse files Browse the repository at this point in the history
use uniqueness when changed for tenant
  • Loading branch information
kbrock authored Sep 30, 2020
2 parents 5aea3d6 + edb658d commit 8b0ce63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 6 additions & 6 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class Tenant < ApplicationRecord
belongs_to :default_miq_group, :class_name => "MiqGroup", :dependent => :destroy
belongs_to :source, :polymorphic => true

validates :subdomain, :uniqueness => true, :allow_nil => true
validates :domain, :uniqueness => true, :allow_nil => true
validates :subdomain, :uniqueness_when_changed => true, :allow_nil => true
validates :domain, :uniqueness_when_changed => true, :allow_nil => true
validate :validate_only_one_root
validates :description, :presence => true
validates :name, :presence => true, :unless => :use_config_for_attributes?
validates :name, :uniqueness => {:scope => :ancestry,
:conditions => -> { in_my_region },
:message => "should be unique per parent"}
validates :name, :presence => true, :unless => :use_config_for_attributes?,
:uniqueness_when_changed => {:scope => :ancestry,
:conditions => -> { in_my_region },
:message => "should be unique per parent"}
validate :validate_default_tenant, :on => :update, :if => :saved_change_to_default_miq_group_id?

scope :all_tenants, -> { where(:divisible => true) }
Expand Down
5 changes: 5 additions & 0 deletions spec/models/tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
Tenant.seed
end

it "doesn't access database when unchanged model is saved" do
m = described_class.create!(:parent => described_class.create!)
expect { m.valid? }.not_to make_database_queries
end

describe "#default_tenant" do
it "has a default tenant" do
expect(default_tenant).to be_truthy
Expand Down

0 comments on commit 8b0ce63

Please sign in to comment.