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 to Change Tenant on Models Using New with_mutable_tenant Block #230

Merged
merged 7 commits into from
Dec 17, 2022
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ end
```
This is useful in shared routes such as admin panels or internal dashboards when `require_tenant` option is enabled throughout the app.

### Allowing tenant updating for a block ###

```ruby
ActsAsTenant.with_mutable_tenant do
# Tenant updating is enabled for all code in this block
end
```

This will allow you to change the tenant of a model. This feature is useful for admin screens, where it is ok to allow certain users to change the tenant on existing models in specific cases.

### Require tenant to be set always ###

If you want to require the tenant to be set at all times, you can configure acts_as_tenant to raise an error when a query is made without a tenant available. See below under configuration options.
Expand Down
16 changes: 16 additions & 0 deletions lib/acts_as_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module ActsAsTenant
@@configuration = nil
@@tenant_klass = nil
@@models_with_global_records = []
@@mutable_tenant = false

class << self
attr_writer :default_tenant
Expand Down Expand Up @@ -87,6 +88,14 @@ def self.default_tenant
@default_tenant unless unscoped
end

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

def self.mutable_tenant?
@@mutable_tenant
end

def self.with_tenant(tenant, &block)
if block.nil?
raise ArgumentError, "block required"
Expand Down Expand Up @@ -120,6 +129,13 @@ def self.without_tenant(&block)
self.unscoped = old_unscoped
end

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

def self.should_require_tenant?
if configuration.require_tenant.respond_to?(:call)
!!configuration.require_tenant.call
Expand Down
1 change: 1 addition & 0 deletions lib/acts_as_tenant/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module ModelExtensions
class_methods do
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
31 changes: 31 additions & 0 deletions spec/models/model_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,37 @@
end
end

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

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

it "should set tenant back to immutable after the block" do
ActsAsTenant.with_mutable_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.with_mutable_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