Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Apr 3, 2024
1 parent 9858f32 commit f96a000
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public function up(): void
Schema::create('root_events', static function (Blueprint $table): void {
$table->id();
$table->foreignIdFor(User::getProxiedClass())->nullable()->constrained()->nullOnDelete();
$table->morphs('target');
$table->uuidMorphs('target');
$table->string('action');
$table->string('label');
$table->json('payload')->nullable();
$table->timestamp('created_at');
$table->timestamps();
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cone\Root\Support\Alert;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\HasRootEvents;
use Cone\Root\Traits\Makeable;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesVisibility;
Expand Down Expand Up @@ -209,6 +210,15 @@ public function handleFormRequest(Request $request, Model $model): void
};

$this->handle($request, $models);

if (in_array(HasRootEvents::class, class_uses_recursive($model))) {
$models->each(static function (Model $model) use ($request): void {
$model->recordRootEvent(
Str::of(static::class)->classBasename()->headline()->value(),
$request->user()
);
});
}
}

/**
Expand Down
101 changes: 101 additions & 0 deletions src/Fields/Events.php
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(),
];
}
}
8 changes: 8 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cone\Root\Interfaces\Form;
use Cone\Root\Root;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\HasRootEvents;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesActions;
use Cone\Root\Traits\ResolvesFields;
Expand Down Expand Up @@ -616,6 +617,13 @@ public function handleFormRequest(Request $request, Model $model): void

$this->saved($request, $model);

if (in_array(HasRootEvents::class, class_uses_recursive($model))) {
$model->recordRootEvent(
$model->wasRecentlyCreated ? 'Created' : 'Updated',
$request->user()
);
}

DB::commit();
} catch (Throwable $exception) {
report($exception);
Expand Down
33 changes: 30 additions & 3 deletions src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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
{
Expand All @@ -21,6 +23,23 @@ class Event extends Model implements Contract
'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.
*/
Expand All @@ -30,10 +49,18 @@ public static function getProxiedInterface(): string
}

/**
* Create a new method.
* Get the event target.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::getProxiedClass());
}

/**
* Get the event target.
*/
public function target(): mixed
public function target(): MorphTo
{
//
return $this->morphTo();
}
}
18 changes: 9 additions & 9 deletions src/Models/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ class Meta extends Model implements Contract
use InteractsWithProxy;

/**
* The attributes that are mass assignable.
* The attributes that should be cast to native types.
*
* @var array<string>
* @var array<string, string>
*/
protected $fillable = [
'key',
'value',
protected $casts = [
'value' => MetaValue::class,
];

/**
* The attributes that should be cast to native types.
* The attributes that are mass assignable.
*
* @var array<string, string>
* @var array<string>
*/
protected $casts = [
'value' => MetaValue::class,
protected $fillable = [
'key',
'value',
];

/**
Expand Down
30 changes: 29 additions & 1 deletion src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Cone\Root\Actions\Action;
use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Fields\Events;
use Cone\Root\Fields\Field;
use Cone\Root\Fields\Fields;
use Cone\Root\Fields\Relation;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\RenderableFilter;
Expand All @@ -16,6 +18,7 @@
use Cone\Root\Root;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\HasRootEvents;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesActions;
use Cone\Root\Traits\ResolvesFilters;
Expand All @@ -37,7 +40,9 @@

abstract class Resource implements Arrayable, Form
{
use AsForm;
use AsForm {
AsForm::resolveFields as __resolveFields;
}
use Authorizable;
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
Expand Down Expand Up @@ -298,6 +303,22 @@ public function modelTitle(Model $model): string
return $model->getKey();
}

/**
* Resolve the fields collection.
*/
public function resolveFields(Request $request): Fields
{
if (is_null($this->fields)) {
$this->withFields(function (): array {
return in_array(HasRootEvents::class, class_uses_recursive($this->getModel()))
? [new Events()]
: [];
});
}

return $this->__resolveFields($request);
}

/**
* Define the filters for the object.
*/
Expand Down Expand Up @@ -429,6 +450,13 @@ public function handleFormRequest(Request $request, Model $model): void

$this->saved($request, $model);

if (in_array(HasRootEvents::class, class_uses_recursive($model))) {
$model->recordRootEvent(
$model->wasRecentlyCreated ? 'Created' : 'Updated',
$request->user()
);
}

DB::commit();
} catch (Throwable $exception) {
report($exception);
Expand Down
33 changes: 33 additions & 0 deletions src/Traits/HasRootEvents.php
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;
}
}

0 comments on commit f96a000

Please sign in to comment.