Skip to content

Commit

Permalink
Merge pull request #1316 from JeRabix/2/chore/export-default-filename
Browse files Browse the repository at this point in the history
fix: ExportHandler, change default filename
  • Loading branch information
lee-to authored Nov 5, 2024
2 parents 3dd7a83 + 3621165 commit 25f2ae5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Handlers/ExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Generator;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
use MoonShine\Contracts\Resources\ResourceContract;
use MoonShine\Exceptions\ActionException;
Expand Down Expand Up @@ -70,6 +71,11 @@ public function filename(string $filename): static
return $this;
}

private function getFilename(): ?string
{
return $this->hasFilename() ? $this->filename : $this->getDefaultFilename();
}

/**
* @param array|Closure(static $ctx): array $ids
*/
Expand Down Expand Up @@ -155,12 +161,17 @@ public function getDelimiter(): string
private function generateFilePath(): string
{
$dir = $this->getDir();
$filename = $this->hasFilename() ? $this->filename : $this->getResource()->uriKey();
$filename = $this->getFilename();
$ext = $this->isCsv() ? 'csv' : 'xlsx';

return sprintf('%s/%s.%s', $dir, $filename, $ext);
}

private function getDefaultFilename(): string
{
return $this->getResource()->uriKey() . '-' . Str::uuid();
}

/**
* @throws WriterNotOpenedException
* @throws IOException
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/Resources/TestImageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MoonShine\Tests\Fixtures\Resources;

use MoonShine\Handlers\ExportHandler;
use Illuminate\Database\Eloquent\Model;
use MoonShine\Fields\ID;
use MoonShine\Fields\Image;
Expand Down Expand Up @@ -33,4 +34,11 @@ public function rules(Model $item): array
{
return [];
}

public function export(): ?ExportHandler
{
return ExportHandler::make(__('moonshine::ui.export'))
->csv()
->filename($this->uriKey());
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Resources/TestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MoonShine\Tests\Fixtures\Resources;

use Closure;
use MoonShine\Handlers\ExportHandler;
use Illuminate\Database\Eloquent\Model;
use MoonShine\Resources\ModelResource;

Expand Down Expand Up @@ -220,4 +221,11 @@ public function setDeleteRelationships(): void
{
$this->deleteRelationships = true;
}

public function export(): ?ExportHandler
{
return ExportHandler::make(__('moonshine::ui.export'))
->csv()
->filename($this->uriKey());
}
}

0 comments on commit 25f2ae5

Please sign in to comment.