Skip to content

Commit

Permalink
Merge pull request #1 from enginedigital/bugfix/tenant-ids
Browse files Browse the repository at this point in the history
Fixed: resolve and assign tenant_id properly when set
  • Loading branch information
james2doyle authored Aug 12, 2021
2 parents 4238aca + b40234b commit 9a087d8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ return [
'model_primary_key_attribute' => 'model_id',
'encrypt_notes' => false,
'tenant_model' => null, // App\Models\Company::class
'tenant_resolver' => null, // a class that uses `__invoke` to get the id of the current tenant
'tenant_resolver' => null, // a class that uses `__invoke` or a container function to get the id of the current tenant
];
```

Expand Down
2 changes: 1 addition & 1 deletion config/model-notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
'model_primary_key_attribute' => 'model_id',
'encrypt_notes' => false,
'tenant_model' => null, // App\Models\Company::class
'tenant_resolver' => null, // a class that uses `__invoke` to get the id of the current tenant
'tenant_resolver' => null, // a class that uses `__invoke` or a container function to get the id of the current tenant
];
18 changes: 11 additions & 7 deletions src/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class Note extends Model
'updated_at' => 'datetime',
];

protected static function booted(): void
{
static::creating(function (Note $model) {
$resolver = config('model-notes.tenant_resolver');

if ($resolver) {
$model->attributes['tenant_id'] = $resolver && is_callable(app($resolver)) ? app($resolver)() : null;
}
});
}

public function model(): MorphTo
{
return $this->morphTo();
Expand Down Expand Up @@ -55,13 +66,6 @@ public function setTypeAttribute(string $value = ''): void
$this->attributes['type'] = strtolower($value);
}

public function setTenantIdAttribute(string $value = null): void
{
$resolver = config('model-notes.tenant_resolver');

$this->attributes['tenant_id'] = $resolver && is_callable(app($resolver)) ? app($resolver)() : $value;
}

public function __toString(): string
{
return $this->note;
Expand Down
21 changes: 15 additions & 6 deletions tests/NoteTenantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,42 @@ class FakeCompany extends Model

class NoteTenantTest extends TestCase
{
public $company;

public function setUp(): void
{
parent::setUp();

config()->set('model-notes.tenant_model', FakeCompany::class);
app()->bind('App\\TenantResolver', function () {
return function () {
return FakeCompany::first()->id;
};
});

config()->set('model-notes.tenant_resolver', 'App\\TenantResolver');

$createCompany = require __DIR__ . '/create_company_table.php';
$createCompany->up();

$createNotes = require __DIR__ . '/../database/migrations/create_model_notes_table.php.stub';
$createNotes->up();

$this->company = FakeCompany::create([
'name' => 'My Company',
]);
}

/** @test */
public function notes_can_have_tenants()
{
$company = FakeCompany::create([
'name' => 'My Company',
]);

$note = Note::create([
'note' => 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
'tenant_id' => $company->id,
// 'tenant_id' => $this->company->id, // should be set by the model booted method
'model_type' => Note::class,
'model_id' => 1,
]);

$this->assertEquals($company->id, $note->fresh()->tenant_id);
$this->assertEquals($this->company->id, $note->fresh()->tenant_id);
}
}

0 comments on commit 9a087d8

Please sign in to comment.