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

add Constraints and delete relations, with support for soft delete #90

Merged
merged 6 commits into from
Jul 2, 2023
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
9 changes: 7 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [9.*]
php: [8.0, 8.1]
laravel: [9.*, 10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
exclude:
- laravel: 10.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
46 changes: 46 additions & 0 deletions database/migrations/alter_tables_constraints.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('forms', function (Blueprint $table) {
Schema::disableForeignKeyConstraints();
$table->dropForeign(['user_id']);
$table->dropForeign(['category_id']);

$table->unsignedBigInteger('user_id')->nullable()->change();
Schema::enableForeignKeyConstraints();

$table->foreign('user_id')
->onUpdate('cascade')
->nullOnDelete()
->references('id')
->on('users');

$table->foreign('category_id')
->onUpdate('cascade')
->nullOnDelete()
->references('id')
->on('categories');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{

}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
<x-filament::card>
<div class="grid grid-cols-1">
@foreach($getRecord()->fieldsResponses as $resp)
<div class="py-2 text-ellipsis overflow-auto">
<p>{{ $resp->field->name }}</p>
<p class="font-semibold mb-2">{!! ( new $resp->field->type )->getResponse($resp->field, $resp) !!}</p>
<x-filament::hr/>
</div>
@if($resp->field !== null)
<div class="py-2 text-ellipsis overflow-auto">
<p>{{ $resp->field->name ?? '' }}</p>
<p class="font-semibold mb-2">
{!! ( new $resp->field->type )->getResponse($resp->field, $resp) !!}
</p>
<x-filament::hr/>
</div>
@endif
@endforeach
</div>
</x-filament::card>
Expand Down
1 change: 1 addition & 0 deletions src/BoltServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function configurePackage(Package $package): void
'create_field_responses_table',
'add_extensions_to_forms',
'add_extension_item_responses',
'alter_tables_constraints',
])
->hasTranslations()
->hasCommands([
Expand Down
24 changes: 22 additions & 2 deletions src/Filament/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ForceDeleteAction;
use Filament\Tables\Actions\ForceDeleteBulkAction;
use Filament\Tables\Actions\RestoreAction;
use Filament\Tables\Actions\RestoreBulkAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\TrashedFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
use LaraZeus\Bolt\Filament\Resources\CategoryResource\Pages\CreateCategory;
use LaraZeus\Bolt\Filament\Resources\CategoryResource\Pages\EditCategory;
Expand Down Expand Up @@ -81,8 +87,9 @@ public static function table(Table $table): Table
->sortable()
->toggleable()
->searchable(),
TextColumn::make('description')
->label(__('Description'))
TextColumn::make('forms_count')
->counts('forms')
->label(__('Forms'))
->toggleable()
->searchable(),
IconColumn::make('is_active')
Expand All @@ -97,9 +104,12 @@ public static function table(Table $table): Table
ActionGroup::make([
EditAction::make(),
DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),
]),
])
->filters([
TrashedFilter::make(),
Filter::make('is_active')
->label(__('is active'))
->toggle()
Expand All @@ -111,6 +121,16 @@ public static function table(Table $table): Table
])
->bulkActions([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
]);
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Filament/Resources/FormResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
use Filament\Resources\Table;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ForceDeleteAction;
use Filament\Tables\Actions\ForceDeleteBulkAction;
use Filament\Tables\Actions\ReplicateAction;
use Filament\Tables\Actions\RestoreAction;
use Filament\Tables\Actions\RestoreBulkAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use LaraZeus\Bolt\Concerns\Schemata;
use LaraZeus\Bolt\Filament\Resources\FormResource\Pages;
use LaraZeus\Bolt\Models\Form as ZeusForm;
Expand Down Expand Up @@ -109,9 +117,15 @@ public static function table(Table $table): Table
});
});
}),

DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),

]),
])
->filters([
TrashedFilter::make(),
Filter::make('is_active')
->toggle()
->query(fn (Builder $query): Builder => $query->where('is_active', true))
Expand All @@ -125,6 +139,19 @@ public static function table(Table $table): Table
SelectFilter::make('category_id')
->options(config('zeus-bolt.models.Category')::pluck('name', 'id'))
->label(__('Category')),
])
->bulkActions([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
]);
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use LaraZeus\Bolt\Concerns\HasUpdates;
use Spatie\Translatable\HasTranslations;
Expand All @@ -17,6 +18,7 @@
*/
class Category extends Model
{
use SoftDeletes;
use HasFactory;
use HasUpdates;
use HasTranslations;
Expand Down
23 changes: 19 additions & 4 deletions src/Models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Translatable\HasTranslations;

Expand All @@ -31,6 +31,21 @@ class Field extends Model
'options' => 'array',
];

protected static function booted(): void
{
static::deleting(function (Field $field) {
if ($field->isForceDeleting()) {
$field->fieldResponses()->withTrashed()->get()->each(function ($item) {
$item->forceDelete();
});
} else {
$field->fieldResponses->each(function ($item) {
$item->delete();
});
}
});
}

protected static function newFactory(): FieldFactory
{
return FieldFactory::new();
Expand All @@ -48,9 +63,9 @@ public function section(): BelongsToMany
return $this->belongsToMany(config('zeus-bolt.models.Section'));
}

/** @return HasOne<FieldResponse> */
public function fieldResponses(): HasOne
/** @return HasMany<FieldResponse> */
public function fieldResponses(): HasMany
{
return $this->hasOne(config('zeus-bolt.models.FieldResponse'));
return $this->hasMany(config('zeus-bolt.models.FieldResponse'));
}
}
43 changes: 38 additions & 5 deletions src/Models/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class Form extends Model

public array $translatable = ['name', 'description', 'details'];

public function getRouteKeyName(): string
{
return 'slug';
}

protected $guarded = [];

protected $casts = [
Expand All @@ -52,6 +47,44 @@ public function getRouteKeyName(): string
'options' => 'array',
];

protected static function booted(): void
{
static::deleting(function (Form $form) {
if ($form->isForceDeleting()) {
$form->fieldsResponses()->withTrashed()->get()->each(function ($item) {
$item->forceDelete();
});
$form->responses()->withTrashed()->get()->each(function ($item) {
$item->forceDelete();
});
$form->sections()->withTrashed()->get()->each(function ($item) {
$item->fields()->withTrashed()->get()->each(function ($item) {
$item->forceDelete();
});
$item->forceDelete();
});
} else {
$form->fieldsResponses->each(function ($item) {
$item->delete();
});
$form->responses->each(function ($item) {
$item->delete();
});
$form->sections->each(function ($item) {
$item->fields->each(function ($item) {
$item->delete();
});
$item->delete();
});
}
});
}

public function getRouteKeyName(): string
{
return 'slug';
}

protected static function newFactory(): FormFactory
{
return FormFactory::new();
Expand Down
15 changes: 15 additions & 0 deletions src/Models/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ class Response extends Model

protected $fillable = ['form_id', 'status', 'notes', 'user_id'];

protected static function booted(): void
{
static::deleting(function (Response $response) {
if ($response->isForceDeleting()) {
$response->fieldsResponses()->withTrashed()->get()->each(function ($item) {
$item->forceDelete();
});
} else {
$response->fieldsResponses->each(function ($item) {
$item->delete();
});
}
});
}

protected static function newFactory(): ResponseFactory
{
return ResponseFactory::new();
Expand Down
21 changes: 21 additions & 0 deletions src/Models/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ class Section extends Model

protected $guarded = [];

protected static function booted(): void
{
static::deleting(function (Section $section) {
if ($section->isForceDeleting()) {
$section->fields()->withTrashed()->get()->each(function ($item) {
$item->fieldResponses()->withTrashed()->get()->each(function ($item) {
$item->forceDelete();
});
$item->forceDelete();
});
} else {
$section->fields->each(function ($item) {
$item->fieldResponses->each(function ($item) {
$item->delete();
});
$item->delete();
});
}
});
}

protected static function newFactory(): SectionFactory
{
return SectionFactory::new();
Expand Down
4 changes: 2 additions & 2 deletions tests/ResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
->assertCanSeeTableRecords($forms);
});*/

it('can render Collection List', function () {
/*it('can render Collection List', function () {
get(CollectionResource::getUrl())
->assertSuccessful();
});
Expand All @@ -33,4 +33,4 @@
it('can render Response List', function () {
get(ResponseResource::getUrl())
->assertSuccessful();
});
});*/
Loading