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

Fixed with_tenant when block fails #26

Merged
merged 1 commit into from
Dec 14, 2017
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
Fixed with_tenant when block fails
  • Loading branch information
Richard-Degenne committed Dec 14, 2017
commit 210ea716bebbef7d2477463dc2a40504cd864a22
12 changes: 7 additions & 5 deletions lib/mongoid/multitenancy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def current_tenant
def with_tenant(tenant, &block)
raise ArgumentError, 'block required' if block.nil?

old_tenant = current_tenant
self.current_tenant = tenant
result = yield
self.current_tenant = old_tenant
result
begin
old_tenant = current_tenant
self.current_tenant = tenant
yield
ensure
self.current_tenant = old_tenant
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/mongoid-multitenancy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@
Mongoid::Multitenancy.with_tenant(another_client) { ; }
expect(Mongoid::Multitenancy.current_tenant).to eq client
end

context 'when the block fails' do
it 'restores the current tenant' do
begin
Mongoid::Multitenancy.with_tenant(another_client) { raise StandardError }
rescue StandardError; end
expect(Mongoid::Multitenancy.current_tenant).to eq client
end
end
end
end