Skip to content

Commit

Permalink
Merge pull request #478 from levchenko-ivan/apply_and_fields
Browse files Browse the repository at this point in the history
MorphTo and StackFields fixes [2.x]
  • Loading branch information
lee-to authored Sep 19, 2023
2 parents 6335656 + 252fe41 commit fe22910
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/Applies/Filters/TextModelApply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace MoonShine\Applies\Filters;

use Closure;
use MoonShine\Contracts\ApplyContract;
use MoonShine\Fields\Field;
use Illuminate\Contracts\Database\Eloquent\Builder;

class TextModelApply implements ApplyContract
{
public function apply(Field $field): Closure
{
return static function (Builder $query) use ($field): void {
$query->where($field->column(), 'like', "%{$field->requestValue()}%");
};
}
}
4 changes: 1 addition & 3 deletions src/Collections/MoonShineRenderElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ abstract class MoonShineRenderElements extends Collection
protected function extractOnly($elements, string $type, array &$data): void
{
foreach ($elements as $element) {
if ($element instanceof StackFields) {
$this->extractOnly($element->getFields(), $type, $data);
} elseif ($element instanceof Tabs) {
if ($element instanceof Tabs) {
foreach ($element->tabs() as $tab) {
$this->extractOnly($tab->getFields(), $type, $data);
}
Expand Down
1 change: 1 addition & 0 deletions src/Fields/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\View\ComponentAttributeBag;
use MoonShine\Contracts\Fields\HasAssets;
use MoonShine\Contracts\Fields\HasDefaultValue;
use MoonShine\Contracts\Fields\HasFields;
use MoonShine\Contracts\MoonShineRenderable;
use MoonShine\Support\Condition;
use MoonShine\Traits\Fields\WithFormElementAttributes;
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Relationships/ModelRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function findResource(): ResourceContract
->append('Resource')
->kebab()
->value()
);
) ?? MoonShine::getResourceFromUriKey(moonshineRequest()->getResourceUri());
}

protected function prepareFill(array $raw = [], mixed $casted = null): mixed
Expand Down
57 changes: 54 additions & 3 deletions src/Fields/StackFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MoonShine\Fields;

use Closure;
use Illuminate\Database\Eloquent\Model;
use MoonShine\Contracts\Fields\HasFields;
use MoonShine\Traits\WithFields;
use Throwable;
Expand All @@ -31,13 +32,31 @@ public function hasLabels(): bool
return $this->withLabels;
}

public function resolveFill(
array $raw = [],
mixed $casted = null,
int $index = 0
): Field {
$this->getFields()
->onlyFields()
->each(fn (Field $field) => $field->resolveFill($raw, $casted, $index));

return $this;
}

protected function resolveOnApply(): ?Closure
{
return function ($item) {
$this->getFields()->onlyFields()->each(
static function (Field $field) use (&$item): void {
$item = $field->apply(
fn ($item) => $item,
static function (Field $field) use ($item): void {
$field->apply(
static function (mixed $item) use ($field): mixed {
if ($field->requestValue() !== false) {
data_set($item, $field->column(), $field->requestValue());
}

return $item;
},
$item
);
}
Expand All @@ -55,6 +74,18 @@ protected function resolvePreview(): string
])->render();
}

/**
* @throws Throwable
*/
protected function resolveBeforeApply(mixed $data): mixed
{
$this->getFields()
->onlyFields()
->each(fn (Field $field): mixed => $field->beforeApply($data));

return $data;
}

/**
* @throws Throwable
*/
Expand All @@ -66,4 +97,24 @@ protected function resolveAfterApply(mixed $data): mixed

return $data;
}

/**
* @throws Throwable
*/
protected function resolveAfterDestroy(mixed $data): mixed
{
$this->getFields()
->onlyFields()
->each(fn (Field $field): mixed => $field->afterDestroy($data));

return $data;
}

public function __clone()
{
foreach ($this->fields as $index => $field) {
$this->fields[$index] = clone $field;
}
}

}
5 changes: 5 additions & 0 deletions src/MoonShineRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
use MoonShine\Applies\Filters\JsonModelApply;
use MoonShine\Applies\Filters\RangeModelApply;
use MoonShine\Applies\Filters\SlideModelApply;
use MoonShine\Applies\Filters\TextModelApply;
use MoonShine\Fields\Date;
use MoonShine\Fields\Json;
use MoonShine\Fields\RangeField;
use MoonShine\Fields\Relationships\BelongsToMany;
use MoonShine\Fields\SlideField;
use MoonShine\Fields\Text;
use MoonShine\Fields\Textarea;
use MoonShine\Resources\ModelResource;

final class MoonShineRegister
Expand All @@ -30,6 +33,8 @@ final class MoonShineRegister
SlideField::class => SlideModelApply::class,
BelongsToMany::class => BelongsToManyModelApply::class,
Json::class => JsonModelApply::class,
Text::class => TextModelApply::class,
Textarea::class => TextModelApply::class,
],
],
'fields' => [],
Expand Down
1 change: 1 addition & 0 deletions src/Pages/Crud/IndexPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected function mainLayer(): array
$this->table(),
);
}

protected function metrics(): ?MoonshineComponent
{
$metrics = $this->getResource()->metrics();
Expand Down
34 changes: 34 additions & 0 deletions src/Support/Filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace MoonShine\Support;

use MoonShine\Fields\Password;
use MoonShine\Fields\PasswordRepeat;
use MoonShine\Fields\Relationships\HasMany;
use MoonShine\Fields\Relationships\HasManyThrough;
use MoonShine\Fields\Relationships\HasOne;
use MoonShine\Fields\Relationships\HasOneThrough;
use MoonShine\Fields\Relationships\MorphMany;
use MoonShine\Fields\Relationships\MorphOne;
use MoonShine\Fields\Relationships\MorphTo;
use MoonShine\Fields\Relationships\MorphToMany;
use MoonShine\Fields\StackFields;

final class Filters
{
const NO_FILTERS = [
HasOne::class,
HasMany::class,
HasManyThrough::class,
HasOneThrough::class,
MorphMany::class,
MorphOne::class,
MorphTo::class,
MorphToMany::class,
Password::class,
PasswordRepeat::class,
StackFields::class,
];
}
12 changes: 11 additions & 1 deletion src/Traits/Resource/ResourceWithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace MoonShine\Traits\Resource;

use MoonShine\Exceptions\FilterException;
use MoonShine\Fields\Fields;
use MoonShine\Support\Filters;
use Throwable;

trait ResourceWithFields
Expand Down Expand Up @@ -83,9 +85,17 @@ public function filters(): array
*/
public function getFilters(): Fields
{
return Fields::make($this->filters())
$filters = Fields::make($this->filters())
->filter()
->withoutOutside()
->wrapNames('filters');

$filters->each(function ($filter) {
if(in_array(get_class($filter), Filters::NO_FILTERS)) {
throw new FilterException("You can't use ".get_class($filter)." inside filters.");
}
});

return $filters;
}
}

0 comments on commit fe22910

Please sign in to comment.