diff --git a/README.md b/README.md index 842ca0c..e4dbdeb 100644 --- a/README.md +++ b/README.md @@ -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 ]; ``` diff --git a/config/model-notes.php b/config/model-notes.php index 5030f33..7015947 100644 --- a/config/model-notes.php +++ b/config/model-notes.php @@ -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 ]; diff --git a/src/Note.php b/src/Note.php index 128035f..c067448 100644 --- a/src/Note.php +++ b/src/Note.php @@ -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(); @@ -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; diff --git a/tests/NoteTenantTest.php b/tests/NoteTenantTest.php index 060d956..6a27ce0 100644 --- a/tests/NoteTenantTest.php +++ b/tests/NoteTenantTest.php @@ -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); } }