Skip to content

Commit

Permalink
Merge pull request #1516 from moonshine-software/fix-b2m-timeout
Browse files Browse the repository at this point in the history
Fix b2m timeout
  • Loading branch information
lee-to authored Feb 1, 2025
2 parents 87133f8 + 6fb7fdb commit 2a92154
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/Laravel/src/DependencyInjection/MoonShine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MoonShine\Contracts\Core\DependencyInjection\StorageContract;
use MoonShine\Core\Core;
use MoonShine\Core\Storage\FileStorage;
use MoonShine\Laravel\Fields\Relationships\ModelRelationField;

/**
* @extends Core<MoonShineConfigurator>
Expand Down Expand Up @@ -58,4 +59,11 @@ public function getStorage(...$parameters): StorageContract
{
return app()->make(StorageContract::class, $parameters) ?? new FileStorage();
}

public function flushState(): void
{
parent::flushState();

ModelRelationField::$excludeInstancing = [];
}
}
40 changes: 29 additions & 11 deletions src/Laravel/src/Fields/Relationships/ModelRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ abstract class ModelRelationField extends Field implements HasResourceContract

protected bool $isMorph = false;

public static array $excludeInstancing = [];

/**
* @throws Throwable
*/
Expand Down Expand Up @@ -76,7 +78,7 @@ public function __construct(
->singular()
->snake()
->append('_id')
->value()
->value(),
);
}

Expand All @@ -89,11 +91,27 @@ public function __construct(
}

// required to create field entities and load assets
if ($this instanceof HasFieldsContract && ! $this->isMorph()) {
if ($this instanceof HasFieldsContract && ! $this->isExcludeInstancing() && ! $this->isMorph()) {
$this->excludeInstancing();
$this->getResource()?->getFormFields();
}
}

public function excludeInstancing(): void
{
self::$excludeInstancing[$this->getExcludeInstanceName()] = true;
}

private function getExcludeInstanceName(): string
{
return class_basename($this) . $this->getRelationName();
}

private function isExcludeInstancing(): bool
{
return isset(self::$excludeInstancing[$this->getExcludeInstanceName()]);
}

/**
* @param ?class-string<ModelResource> $classString
* @throws Throwable
Expand All @@ -112,13 +130,13 @@ protected function findResource(?string $classString = null): ModelResource
->singular()
->append('Resource')
->kebab()
->value()
->value(),
);

if (\is_null($resource) && $this->isMorph()) {
/** @var ModelResource $resource */
$resource = moonshine()->getResources()->findByUri(
moonshineRequest()->getResourceUri()
moonshineRequest()->getResourceUri(),
);
}

Expand All @@ -127,9 +145,9 @@ protected function findResource(?string $classString = null): ModelResource
function (?ModelResource $resource): void {
throw_if(
\is_null($resource),
FieldException::resourceRequired(static::class, $this->getRelationName())
FieldException::resourceRequired(static::class, $this->getRelationName()),
);
}
},
);
}

Expand All @@ -156,15 +174,15 @@ protected function resolveFill(array $raw = [], ?DataWrapperContract $casted = n

if ($this->isToOne()) {
$this->setColumn(
$this->getRelation()?->getForeignKeyName() ?? ''
$this->getRelation()?->getForeignKeyName() ?? '',
);

$this->setRawValue(
$raw[$this->getColumn()] ?? null
$raw[$this->getColumn()] ?? null,
);

$this->setFormattedValue(
data_get($data, $this->getResourceColumn())
data_get($data, $this->getResourceColumn()),
);
}

Expand All @@ -185,8 +203,8 @@ public function toFormattedValue(): mixed
$this->getFormattedValueCallback(),
$value ?? $this->getRelation()?->getModel(),
$this->getRowIndex(),
$this
)
$this,
),
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Laravel/src/Traits/Fields/BelongsToOrManyCreatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use MoonShine\Contracts\UI\ActionButtonContract;
use MoonShine\Laravel\Buttons\BelongsToOrManyButton;
use MoonShine\Laravel\Fields\Relationships\BelongsToMany;
use Throwable;

trait BelongsToOrManyCreatable
Expand Down Expand Up @@ -43,6 +44,10 @@ public function getCreateButton(): ?ActionButtonContract
return null;
}

if ($this->getParent() instanceof BelongsToMany) {
return null;
}

$button = BelongsToOrManyButton::for($this, button: $this->creatableButton);

return $button->isSee()
Expand Down
26 changes: 25 additions & 1 deletion src/UI/src/Fields/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ abstract class FormElement extends MoonShineComponent implements FormElementCont

protected static ?Closure $requestValueResolver = null;

protected ?Closure $onRequestValue = null;

protected ?string $requestKeyPrefix = null;

protected bool $hasOld = true;
Expand Down Expand Up @@ -520,18 +522,40 @@ public static function requestValueResolver(Closure $resolver): void
static::$requestValueResolver = $resolver;
}

/**
* @param Closure(mixed $value, string $name, mixed $default, static $ctx): mixed $callback
*/
public function onRequestValue(Closure $callback): static
{
$this->onRequestValue = $callback;

return $this;
}

public function getRequestValue(string|int|null $index = null): mixed
{
if (! \is_null(static::$requestValueResolver)) {
return \call_user_func(static::$requestValueResolver, $index, $this->getDefaultIfExists(), $this);
}

return $this->prepareRequestValue(
$value = $this->prepareRequestValue(
$this->getCore()->getRequest()->get(
$this->getRequestNameDot($index),
$this->getDefaultIfExists()
) ?? false
);

if ($this->onRequestValue instanceof Closure) {
return \call_user_func(
$this->onRequestValue,
$value,
$this->getRequestNameDot($index),
$this->getDefaultIfExists(),
$this
);
}

return $value;
}

public function getRequestNameDot(string|int|null $index = null): string
Expand Down

0 comments on commit 2a92154

Please sign in to comment.