-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from conedevelopment/events
Events
- Loading branch information
Showing
9 changed files
with
296 additions
and
10 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
database/migrations/2024_03_21_175513_create_root_events_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Cone\Root\Models\User; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('root_events', static function (Blueprint $table): void { | ||
$table->id(); | ||
$table->foreignIdFor(User::getProxiedClass())->nullable()->constrained()->nullOnDelete(); | ||
$table->uuidMorphs('target'); | ||
$table->string('action'); | ||
$table->json('payload')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::drop('root_events'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Fields; | ||
|
||
use Closure; | ||
use Cone\Root\Http\Controllers\RelationController; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Router; | ||
|
||
class Events extends MorphMany | ||
{ | ||
/** | ||
* Indicates whether the relation is a sub resource. | ||
*/ | ||
protected bool $asSubResource = true; | ||
|
||
/** | ||
* The relations to eager load on every query. | ||
*/ | ||
protected array $with = [ | ||
'user', | ||
'target', | ||
]; | ||
|
||
/** | ||
* Create a new relation field instance. | ||
*/ | ||
public function __construct(?string $label = null, Closure|string|null $modelAttribute = 'rootEvents', Closure|string|null $relation = null) | ||
{ | ||
parent::__construct($label ?: __('Events'), $modelAttribute, $relation); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function persist(Request $request, Model $model, mixed $value): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveHydrate(Request $request, Model $model, mixed $value): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function routes(Router $router): void | ||
{ | ||
$router->get('/', [RelationController::class, 'index']); | ||
$router->get("/{{$this->getRouteKeyName()}}", [RelationController::class, 'show']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapRelationAbilities(Request $request, Model $model): array | ||
{ | ||
return [ | ||
'viewAny' => $this->resolveAbility('viewAny', $request, $model), | ||
'create' => false, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapRelatedAbilities(Request $request, Model $model, Model $related): array | ||
{ | ||
return [ | ||
'view' => $this->resolveAbility('view', $request, $model, $related), | ||
'update' => false, | ||
'restore' => false, | ||
'delete' => false, | ||
'forceDelete' => false, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fields(Request $request): array | ||
{ | ||
return [ | ||
ID::make()->sortable(), | ||
|
||
Text::make(__('Action'), 'action')->sortable(), | ||
|
||
BelongsTo::make(__('User'), 'user') | ||
->display('name'), | ||
|
||
Date::make(__('Date'), 'created_at') | ||
->withTime(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Interfaces\Models; | ||
|
||
interface Event | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Models; | ||
|
||
use Cone\Root\Interfaces\Models\Event as Contract; | ||
use Cone\Root\Traits\InteractsWithProxy; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
|
||
class Event extends Model implements Contract | ||
{ | ||
use HasFactory; | ||
use InteractsWithProxy; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $casts = [ | ||
'payload' => 'json', | ||
]; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<string> | ||
*/ | ||
protected $fillable = [ | ||
'action', | ||
'payload', | ||
]; | ||
|
||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'root_events'; | ||
|
||
/** | ||
* Get the proxied interface. | ||
*/ | ||
public static function getProxiedInterface(): string | ||
{ | ||
return Contract::class; | ||
} | ||
|
||
/** | ||
* Get the event target. | ||
*/ | ||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::getProxiedClass()); | ||
} | ||
|
||
/** | ||
* Get the event target. | ||
*/ | ||
public function target(): MorphTo | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Traits; | ||
|
||
use Cone\Root\Models\Event; | ||
use Cone\Root\Models\User; | ||
use Illuminate\Database\Eloquent\Relations\MorphMany; | ||
|
||
trait HasRootEvents | ||
{ | ||
/** | ||
* Get the events for the model. | ||
*/ | ||
public function rootEvents(): MorphMany | ||
{ | ||
return $this->morphMany(Event::getProxiedClass(), 'target'); | ||
} | ||
|
||
/** | ||
* Record a new root event for the model. | ||
*/ | ||
public function recordRootEvent(string $action, ?User $user = null, ?array $payload = null): Event | ||
{ | ||
$event = $this->rootEvents()->make([ | ||
'action' => $action, | ||
'payload' => $payload, | ||
]); | ||
|
||
$event->user()->associate($user)->save(); | ||
|
||
return $event; | ||
} | ||
} |