Skip to content

Commit

Permalink
fixed query string cache, added prefix path to file fields, fixed cha…
Browse files Browse the repository at this point in the history
…ngelog
  • Loading branch information
lee-to committed Dec 2, 2022
1 parent 688dd54 commit c9aef2f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion resources/views/base/changelog.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if($item->changeLogs && $item->changeLogs->isNotEmpty())
@if(isset($item->changeLogs) && $item->changeLogs && $item->changeLogs->isNotEmpty())
<div class="my-6 text-lg">@lang('moonshine::ui.last_changes')</div>

<div class="flex flex-col mt-8">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/base/index/shared/filters.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class="-mr-2 w-10 h-10 bg-white text-black dark:bg-purple dark:text-white p-2 ro

@if(request('filters'))
<div class="mt-5">
<a href="{{ $resource->route("index") }}"
<a href="{{ $resource->route('index', query: ['reset' => true]) }}"
class="bg-transparent hover:bg-purple text-purple
font-semibold hover:text-white py-2 px-4 border border-purple
hover:border-transparent rounded">
Expand Down
4 changes: 4 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public function formViewValue(Model $item): mixed
return $item->{$this->relation()} ?? $this->getDefault();
}

if (is_callable($this->valueCallback())) {
return $this->valueCallback()($item) ?? $this->getDefault();
}

return $item->{$this->field()} ?? $this->getDefault();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function indexViewValue(Model $item, bool $container = true): mixed
if ($this->isMultiple()) {
return collect($item->{$this->field()})
->map(fn($value, $index) => view('moonshine::fields.shared.file', [
'value' => Storage::url($value),
'value' => Storage::url($this->prefixedValue($value)),
'index' => $index + 1,
'canDownload' => $this->canDownload(),
])->render())->implode('');
Expand All @@ -50,7 +50,7 @@ public function indexViewValue(Model $item, bool $container = true): mixed
return view(
'moonshine::fields.shared.file',
[
'value' => parent::indexViewValue($item),
'value' => Storage::url($this->prefixedValue($item->{$this->field()})),
'canDownload' => $this->canDownload(),
]
);
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public function indexViewValue(Model $item, bool $container = true): mixed

if ($this->isMultiple()) {
$values = collect($item->{$this->field()})
->map(fn($value) => "'".Storage::url($value)."'")->implode(',');
->map(fn($value) => "'".Storage::url($this->prefixedValue($value))."'")->implode(',');

return view('moonshine::shared.carousel', [
'values' => $values
]);
}

return view('moonshine::fields.shared.thumbnail', [
'value' => Storage::url($item->{$this->field()}) ,
'value' => Storage::url($this->prefixedValue($item->{$this->field()})),
]);
}
}
1 change: 1 addition & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ public function query(): Builder
$query = $query->orderBy(static::$orderField, static::$orderType);
}

Cache::forget("moonshine_query_{$this->routeAlias()}");
Cache::remember("moonshine_query_{$this->routeAlias()}", now()->addHours(2), function () {
return request()->getQueryString();
});
Expand Down
32 changes: 30 additions & 2 deletions src/Traits/Fields/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Leeto\MoonShine\Exceptions\FieldException;
use Leeto\MoonShine\Helpers\Condition;
use Throwable;
Expand All @@ -16,10 +17,19 @@ trait FileTrait

protected string $dir = '/';

protected string $withPrefix = '';

protected array $allowedExtensions = [];

protected bool $disableDownload = false;

public function withPrefix(string $withPrefix): static
{
$this->withPrefix = $withPrefix;

return $this;
}

public function disk(string $disk): static
{
$this->disk = $disk;
Expand Down Expand Up @@ -93,6 +103,9 @@ public function store(UploadedFile $file): string
return $file->store($this->getDir(), $this->getDisk());
}

/**
* @throws Throwable
*/
public function save(Model $item): Model
{
$requestValue = $this->requestValue();
Expand All @@ -104,12 +117,12 @@ public function save(Model $item): Model
$paths = [];

foreach ($requestValue as $file) {
$paths[] = $this->store($file);
$paths[] = $this->prefixedValue($this->store($file));
}

$saveValue = $saveValue->merge($paths)->unique()->toArray();
} else {
$saveValue = $this->store($requestValue);
$saveValue = $this->prefixedValue($this->store($requestValue));
}
}

Expand All @@ -120,6 +133,21 @@ public function save(Model $item): Model
return $item;
}

public function formViewValue(Model $item): mixed
{
if ($this->isMultiple()) {
return collect($item->{$this->field()})
->map(fn($value) => $this->prefixedValue($value));
}

return $this->prefixedValue($item->{$this->field()});
}

protected function prefixedValue(string $value): string
{
return ltrim($value, $this->withPrefix);
}

public function exportViewValue(Model $item): string
{
if ($this->isMultiple()) {
Expand Down

0 comments on commit c9aef2f

Please sign in to comment.