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

feat(manage): add CRUD operations for emulator systems and releases #2708

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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('title')
->required()
->maxLength(255),
Forms\Components\TextInput::make('version'),

Forms\Components\DatePicker::make('created_at')
->label('Release Date')
->native(false)
->date(),

Forms\Components\Toggle::make('stable'),

Forms\Components\Toggle::make('minimum'),
]);
}

Expand All @@ -54,13 +61,17 @@ public function table(Table $table): Table
->alignCenter(),
])
->filters([

Tables\Filters\TrashedFilter::make(),
])
->headerActions([

Tables\Actions\CreateAction::make(),
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
])
->actions([

Tables\Actions\ActionGroup::make([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
Tables\Actions\RestoreAction::make(),
]),
])
->defaultSort(function (Builder $query): Builder {
return $query->orderByDesc('created_at');
Expand Down
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,37 @@ public function form(Form $form): Form
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('title')
->recordTitleAttribute('name_full')
->columns([
Tables\Columns\ImageColumn::make('icon_url')
->label('')
->size(config('media.icon.sm.width')),

Tables\Columns\TextColumn::make('ID')
->label('ID')
->sortable()
->searchable(),
->searchable(query: function (Builder $query, string $search): Builder {
return $query->where('Console.ID', 'like', "%{$search}%");
}),

Tables\Columns\TextColumn::make('name_full')
->label('Full name')
->description(fn (System $record): ?string => $record->name_short)
->searchable()
->searchable(query: function (Builder $query, string $search): Builder {
return $query->where('Console.name_full', 'like', "%{$search}%");
})
->sortable()
->grow(true),
])
->filters([

])
->headerActions([

Tables\Actions\AttachAction::make()
->preloadRecordSelect(),
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
])
->actions([

Tables\Actions\DetachAction::make(),
])
->defaultSort(function (Builder $query): Builder {
return $query->orderBy('name_full');
Expand Down
54 changes: 53 additions & 1 deletion app/Models/Emulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use App\Support\Database\Eloquent\BaseModel;
use App\Support\Database\Eloquent\BasePivot;
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\EloquentSortable\SortableTrait;
Expand All @@ -21,9 +24,18 @@

class Emulator extends BaseModel implements HasMedia
{
/*
* Framework Traits
*/
use SoftDeletes;
use SortableTrait;

/*
* Providers Traits
*/
use PivotEventTrait;
use InteractsWithMedia;

use LogsActivity {
LogsActivity::activities as auditLog;
}
Expand All @@ -38,6 +50,46 @@ class Emulator extends BaseModel implements HasMedia
'source_url',
];

public static function boot()
{
parent::boot();

static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
if ($relationName === 'systems') {
/** @var User $user */
$user = Auth::user();

activity()->causedBy($user)->performedOn($model)
->withProperty('old', [$relationName => null])
->withProperty('attributes', [$relationName => (new Collection($pivotIds))
->map(fn ($pivotId) => [
'id' => $pivotId,
'attributes' => $pivotIdsAttributes[$pivotId],
]),
])
->event('pivotAttached')
->log('pivotAttached');
}
});

static::pivotDetached(function ($model, $relationName, $pivotIds) {
if ($relationName === 'systems') {
/** @var User $user */
$user = Auth::user();

activity()->causedBy($user)->performedOn($model)
->withProperty('old', [$relationName => (new Collection($pivotIds))
->map(fn ($pivotId) => [
'id' => $pivotId,
]),
])
->withProperty('attributes', [$relationName => null])
->event('pivotDetached')
->log('pivotDetached');
}
});
}

// audit activity log

public function getActivitylogOptions(): LogOptions
Expand Down Expand Up @@ -89,7 +141,7 @@ public function registerMediaCollections(): void
*/
public function systems(): BelongsToMany
{
return $this->belongsToMany(System::class, 'system_emulators')
return $this->belongsToMany(System::class, 'system_emulators', 'emulator_id', 'system_id')
->using(BasePivot::class)
->withTimestamps();
}
Expand Down
36 changes: 36 additions & 0 deletions app/Models/EmulatorRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Support\Database\Eloquent\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

Expand All @@ -17,6 +19,10 @@ class EmulatorRelease extends BaseModel implements HasMedia
use InteractsWithMedia;
use HasStabilityFlags;

use LogsActivity {
LogsActivity::activities as auditLog;
}

protected $table = 'emulator_releases';

protected $fillable = [
Expand All @@ -25,13 +31,43 @@ class EmulatorRelease extends BaseModel implements HasMedia
'notes',
'stable',
'version',
'created_at',
];

protected $with = [
'emulator',
'media',
];

protected static function booted()
{
static::saving(function (EmulatorRelease $release) {
if ($release->minimum) {
// Set all other releases for this emulator to not be the minimum.
// There can only be one minimum.
static::where('emulator_id', $release->emulator_id)
->where('id', '!=', $release->id)
->update(['minimum' => false]);
}
});
}

// audit activity log

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly([
'emulator_id',
'version',
'stable',
'minimum',
'notes',
])
->logOnlyDirty()
->dontSubmitEmptyLogs();
}

// == media

public function registerMediaCollections(): void
Expand Down
9 changes: 7 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Jenssegers\Optimus\Optimus;
use Laravel\Scout\Searchable;
use Spatie\Activitylog\LogOptions;
Expand Down Expand Up @@ -219,7 +220,9 @@ public static function boot()

static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
if ($relationName === 'roles') {
activity()->causedBy(auth()->user())->performedOn($model)
$user = Auth::user();

activity()->causedBy($user)->performedOn($model)
->withProperty('old', [$relationName => null])
->withProperty('attributes', [$relationName => (new Collection($pivotIds))
->map(fn ($pivotId) => [
Expand All @@ -234,7 +237,9 @@ public static function boot()

static::pivotDetached(function ($model, $relationName, $pivotIds) {
if ($relationName === 'roles') {
activity()->causedBy(auth()->user())->performedOn($model)
$user = Auth::user();

activity()->causedBy($user)->performedOn($model)
->withProperty('old', [$relationName => (new Collection($pivotIds))
->map(fn ($pivotId) => [
'id' => $pivotId,
Expand Down
7 changes: 7 additions & 0 deletions app/Policies/EmulatorReleasePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public function restore(User $user, EmulatorRelease $emulatorRelease): bool
]);
}

public function restoreAny(User $user): bool
{
return $user->hasAnyRole([
Role::RELEASE_MANAGER,
]);
}

public function forceDelete(User $user, EmulatorRelease $emulatorRelease): bool
{
return $user->hasAnyRole([
Expand Down