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

allow changing tenant on models using new without_tenant! block #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions lib/acts_as_tenant/model_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ActsAsTenant
@@tenant_klass = nil
@@models_with_global_records = []
@@mutable_tenant = false

def self.set_tenant_klass(klass)
@@tenant_klass = klass
Expand Down Expand Up @@ -50,6 +51,14 @@ def self.unscoped?
!!unscoped
end

def self.mutable_tenant!(toggle)
@@mutable_tenant = toggle
end

def self.mutable_tenant?
@@mutable_tenant === true
end

class << self
attr_accessor :test_tenant

Expand Down Expand Up @@ -94,6 +103,13 @@ def self.without_tenant(&block)
self.unscoped = old_unscoped
end

def self.without_tenant!(&block)
ActsAsTenant.mutable_tenant!(true)
self.without_tenant(&block)
ensure
ActsAsTenant.mutable_tenant!(false)
end

module ModelExtensions
def self.included(base)
base.extend(ClassMethods)
Expand All @@ -102,6 +118,7 @@ def self.included(base)
module ClassMethods
def acts_as_tenant(tenant = :account, options = {})
ActsAsTenant.set_tenant_klass(tenant)
ActsAsTenant.mutable_tenant!(false)

ActsAsTenant.add_global_record_model(self) if options[:has_global_records]

Expand Down Expand Up @@ -168,13 +185,13 @@ def acts_as_tenant(tenant = :account, options = {})
to_include = Module.new do
define_method "#{fkey}=" do |integer|
write_attribute("#{fkey}", integer)
raise ActsAsTenant::Errors::TenantIsImmutable if send("#{fkey}_changed?") && persisted? && !send("#{fkey}_was").nil?
raise ActsAsTenant::Errors::TenantIsImmutable if send("#{fkey}_changed?") && persisted? && !send("#{fkey}_was").nil? && !ActsAsTenant.mutable_tenant?
integer
end

define_method "#{ActsAsTenant.tenant_klass.to_s}=" do |model|
super(model)
raise ActsAsTenant::Errors::TenantIsImmutable if send("#{fkey}_changed?") && persisted? && !send("#{fkey}_was").nil?
raise ActsAsTenant::Errors::TenantIsImmutable if send("#{fkey}_changed?") && persisted? && !send("#{fkey}_was").nil? && !ActsAsTenant.mutable_tenant?
model
end

Expand Down
33 changes: 33 additions & 0 deletions spec/acts_as_tenant/model_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,39 @@
end
end

describe "::without_tenant!" do
it "should return the value of the block" do
value = ActsAsTenant.without_tenant! do
"something"
end
expect(value).to eq "something"
end

it "should raise an error when no block is provided" do
expect { ActsAsTenant.without_tenant! }.to raise_error(ArgumentError, /block required/)
end

it "should set tenant back to immutable after the block" do
ActsAsTenant.without_tenant! do
"something"
end
expect(ActsAsTenant.mutable_tenant?).to eq false
end

describe 'mutability' do
before do
@account = Account.create!(:name => 'foo')
@project = @account.projects.create!(:name => 'bar')
end

it "should allow tenant_id to change inside the block" do
new_account_id = @account.id + 1
expect{ ActsAsTenant.without_tenant! { @project.account_id = new_account_id } }.to_not raise_error(ActsAsTenant::Errors::TenantIsImmutable)
expect(@project.account_id).to eq new_account_id
end
end
end

# Tenant required
context "tenant required" do
before do
Expand Down