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

Restore previous tenant and tenant callback #10

Open
wants to merge 4 commits 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
12 changes: 10 additions & 2 deletions lib/multitenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
# Multitenant: making cross tenant data leaks a thing of the past...since 2011
module Multitenant
class << self
attr_accessor :current_tenant
attr_reader :current_tenant

def current_tenant=(tenant)
@current_tenant = tenant
@current_tenant.became_current_tenant if @current_tenant.respond_to?(:became_current_tenant)
@current_tenant
end

# execute a block scoped to the current tenant
# unsets the current tenant after execution
# @param [Object] tenant the new current tenant
def with_tenant(tenant, &block)
previous_tenant = Multitenant.current_tenant
Multitenant.current_tenant = tenant
yield
ensure
Multitenant.current_tenant = nil
Multitenant.current_tenant = previous_tenant
end
end

Expand Down
38 changes: 34 additions & 4 deletions spec/multitenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

class Company < ActiveRecord::Base
has_many :users
attr_accessor :now_is_current_tenant

def became_current_tenant
self.now_is_current_tenant = true
end
end
class User < ActiveRecord::Base
belongs_to :company
Expand All @@ -41,8 +46,13 @@ class Item < ActiveRecord::Base
after { Multitenant.current_tenant = nil }

describe 'Multitenant.current_tenant' do
before { Multitenant.current_tenant = :foo }
it { Multitenant.current_tenant == :foo }
before do
@company = Company.create!(:name => 'foo')
@company.now_is_current_tenant.should == nil
Multitenant.current_tenant = @company
end
it { Multitenant.current_tenant.should == @company }
it { @company.now_is_current_tenant.should == true }
end

describe 'Multitenant.with_tenant block' do
Expand All @@ -58,7 +68,25 @@ class Item < ActiveRecord::Base
end
it 'yields the block' do
@executed.should == true
end
end
end

describe 'Multitenant.with_tenant block with a previous tenant' do
before do
@previous = :whatever
Multitenant.current_tenant = @previous
@executed = false
Multitenant.with_tenant :foo do
Multitenant.current_tenant.should == :foo
@executed = true
end
end
it 'resets current_tenant after block runs' do
Multitenant.current_tenant.should == @previous
end
it 'yields the block' do
@executed.should == true
end
end

describe 'Multitenant.with_tenant block that raises error' do
Expand All @@ -76,12 +104,13 @@ class Item < ActiveRecord::Base
end
it 'yields the block' do
@executed.should == true
end
end
end

describe 'User.all when current_tenant is set' do
before do
@company = Company.create!(:name => 'foo')
@company.now_is_current_tenant.should == nil
@company2 = Company.create!(:name => 'bar')

@user = @company.users.create! :name => 'bob'
Expand All @@ -92,6 +121,7 @@ class Item < ActiveRecord::Base
end
it { @users.length.should == 1 }
it { @users.should == [@user] }
it { @company.now_is_current_tenant.should == true }
end

describe 'Item.all when current_tenant is set' do
Expand Down