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

Don't clear caches if we're setting current to the existing value #28

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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### 0.13.1
- Don't reset `current`/`current_id` when setting it the current value to avoid invalidating caches.

### 0.13.0
* Add `RailsMultitenant::GlobalContextRegistry.merge!` and
` RailsMultitenant::GlobalContextRegistry.with_merged_registry`
Expand Down
3 changes: 3 additions & 0 deletions lib/rails_multitenant/global_context_registry/current.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def current

def current=(object)
raise "#{object} is not a #{self}" if object.present? && !object.is_a?(self)
# Don't clear caches if we're setting current to the existing value
return if current.object_id == object.object_id

GlobalContextRegistry.set(current_registry_obj, object)
__clear_dependents!
Expand All @@ -36,6 +38,7 @@ def current!

def clear_current!
GlobalContextRegistry.delete(current_registry_obj)
__clear_dependents!
end

def as_current(object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ module CurrentInstance

module ClassMethods
def current_id=(id)
return if current_id == id

GlobalContextRegistry.delete(current_instance_registry_obj)
GlobalContextRegistry.set(current_instance_registry_id, id)
__clear_dependents!
end

def current=(object)
raise "#{object} is not a #{self}" if object.present? && !object.is_a?(self)
# Don't clear caches if we're setting current to the existing value
return if GlobalContextRegistry.get(current_instance_registry_obj).object_id == object.object_id

GlobalContextRegistry.set(current_instance_registry_obj, object)
GlobalContextRegistry.set(current_instance_registry_id, object.try(:id))
Expand Down Expand Up @@ -62,6 +66,8 @@ def as_current(model)

def clear_current!
GlobalContextRegistry.delete(current_instance_registry_obj)
GlobalContextRegistry.delete(current_instance_registry_id)
__clear_dependents!
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_multitenant/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RailsMultitenant
VERSION = '0.13.0'
VERSION = '0.13.1'
end
83 changes: 79 additions & 4 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe Item do

let!(:item1) { Item.create! }

let!(:org1) { Organization.current! }
let!(:org2) { Organization.create! }
let!(:item2) { org2.as_current { Item.create! } }
let!(:item3) { org2.as_current { Item.create! } }
Expand Down Expand Up @@ -41,6 +41,55 @@ class SubOrganization < Organization

end

describe ".current_id=" do
before do
Organization.current = org1
end

it "invalidates the cached object" do
Organization.current_id = org2.id
expect(Organization.current).to eq(org2)
end

it "invalidates the cached id" do
Organization.current_id = org2.id
expect(Organization.current_id).to eq(org2.id)
end

it "doesn't clear the cached object when set to the same current_id" do
allow(Organization).to receive(:find).and_raise("Shouldn't be called")
Organization.current_id = org1.id
expect(Organization.current).to equal(org1)
end
end

describe ".current=" do
before do
Organization.current = org1
end

it "invalidates the cached object" do
Organization.current = org2
expect(Organization.current).to eq(org2)
end

it "invalidates the cached id" do
Organization.current = org2
expect(Organization.current_id).to eq(org2.id)
end

it "doesn't clear the cached object when set to the same current" do
allow(Organization).to receive(:find).and_raise("Shouldn't be called")
Organization.current = org1
expect(Organization.current).to equal(org1)
end

it "clears the cached object when set to a different instance of current" do
Organization.current = Organization.find(Organization.current_id)
expect(Organization.current).not_to equal(org1)
end
end

describe ".as_current" do
it "returns the correct items with an org supplied" do
Organization.as_current(org2) do
Expand All @@ -60,21 +109,47 @@ class SubOrganization < Organization
end

it "invalidates dependent models" do
DependentModel.current = DependentModel.create!
dependent = DependentModel.current
dependent = DependentModel.create!
DependentModel.current = dependent

SubOrganization.create!.as_current do
expect(DependentModel.current).not_to equal(dependent)
end
end

it "invalidates dependent objects" do
dependent = DependentClass.current
dependent = DependentClass.new
DependentClass.current = dependent

SubOrganization.create!.as_current do
expect(DependentClass.current).not_to equal(dependent)
end
end
end

describe ".clear_current!" do
let(:dependent) { DependentModel.create! }

before do
Organization.current = org2
DependentModel.current = dependent
Organization.clear_current!
end

it "clears the current object" do
expect(Organization.current).to be_nil
end

it "clears the current id" do
expect(Organization.current_id).to be_nil
end

it "clears the dependent current object" do
expect(DependentModel.current).to be_nil
end

it "clears the dependent current id" do
expect(DependentModel.current_id).to be_nil
end
end
end