Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Oct 23, 2024
1 parent 2699e97 commit 35d4903
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
46 changes: 6 additions & 40 deletions packages/builder/src/Resources/ItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
use Filament\Tables\Actions\RestoreBulkAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TagsColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Moox\Builder\Models\Item;
use Moox\Builder\Resources\ItemResource\Pages\CreateItem;
use Moox\Builder\Resources\ItemResource\Pages\EditItem;
Expand Down Expand Up @@ -380,16 +378,6 @@ public static function setCurrentTab(?string $tab): void
static::$currentTab = $tab;
}

protected static function handleTaxonomies(Model $record, array $data): void
{
$taxonomyService = static::getTaxonomyService();
foreach ($taxonomyService->getTaxonomies() as $taxonomy => $settings) {
if (isset($data[$taxonomy])) {
$record->$taxonomy()->sync($data[$taxonomy]);
}
}
}

public static function mutateFormDataBeforeCreate(array $data): array
{
return $data;
Expand All @@ -415,36 +403,14 @@ public static function getResourceName(): string
return static::getModel()::getResourceName();
}

protected static function getTaxonomyColumns(): array
protected static function handleTaxonomies(Model $record, array $data): void
{
$taxonomyService = static::getTaxonomyService();
$taxonomies = $taxonomyService->getTaxonomies();

return collect($taxonomies)->map(function ($settings, $taxonomy) use ($taxonomyService) {
return TagsColumn::make($taxonomy)
->label($settings['label'] ?? ucfirst($taxonomy))
->getStateUsing(function ($record) use ($taxonomy, $taxonomyService, $settings) {
$relationshipName = $settings['relationship'] ?? $taxonomy;
$table = $taxonomyService->getTaxonomyTable($taxonomy);
$foreignKey = $taxonomyService->getTaxonomyForeignKey($taxonomy);
$relatedKey = $taxonomyService->getTaxonomyRelatedKey($taxonomy);
$modelClass = $taxonomyService->getTaxonomyModel($taxonomy);

$model = app($modelClass);
$modelTable = $model->getTable();

$tags = DB::table($table)
->join($modelTable, "{$table}.{$relatedKey}", '=', "{$modelTable}.id")
->where("{$table}.{$foreignKey}", $record->id)
->pluck("{$modelTable}.title")
->toArray();

return $tags;
})
->toggleable(isToggledHiddenByDefault: true)
->separator(',')
->searchable();
})->toArray();
foreach ($taxonomyService->getTaxonomies() as $taxonomy => $settings) {
if (isset($data[$taxonomy])) {
$record->$taxonomy()->sync($data[$taxonomy]);
}
}
}

public static function getEloquentQuery(): Builder
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/Traits/HasDynamicTaxonomyFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,17 @@ protected static function getTaxonomyColumns(): array
$foreignKey = $taxonomyService->getTaxonomyForeignKey($taxonomy);
$relatedKey = $taxonomyService->getTaxonomyRelatedKey($taxonomy);
$modelClass = $taxonomyService->getTaxonomyModel($taxonomy);

$model = app($modelClass);
$modelTable = $model->getTable();

return DB::table($table)
$tags = DB::table($table)
->join($modelTable, "{$table}.{$relatedKey}", '=', "{$modelTable}.id")
->where("{$table}.{$foreignKey}", $record->id)
->pluck("{$modelTable}.title")
->toArray();

return $tags;
})
->toggleable(isToggledHiddenByDefault: true)
->separator(',')
Expand Down

0 comments on commit 35d4903

Please sign in to comment.