Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 24, 2024
1 parent 3e2c42c commit f8349f9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/layout/sidebar-group.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class="btn btn--light btn--sm btn--icon block-navigation__toggle"
<x-root::icon name="chevron-down" class="btn__icon" />
</button>
</h3>
<nav class="block-navigation__menu block-navigation__menu--breakout" x-bind:class="{ 'is-open': open }">
<nav class="block-navigation__menu block-navigation__menu--breakout" x-bind:data-state="open ? 'open' : 'closed'">
<ul>
@foreach($items as $item)
<li>
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function fields(Request $request): array
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
);
});
})->display(function (Model $model): mixed {
})->display(function (Model $model): ?string {
return $this->resolveDisplay($model);
}),
];
Expand Down
14 changes: 7 additions & 7 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function collection(string $value): static
/**
* {@inheritdoc}
*/
public function resolveDisplay(Model $related): mixed
public function resolveDisplay(Model $related): ?string
{
if (is_null($this->displayResolver)) {
$this->display(function (Medium $related): string {
Expand Down Expand Up @@ -141,12 +141,9 @@ public function storeUsing(Closure $callback): static
*/
public function store(Request $request, Model $model, UploadedFile $file): array
{
$disk = Storage::build([
'driver' => 'local',
'root' => Config::get('root.media.tmp_dir'),
]);
$disk = Storage::build(Config::get('root.media.tmp_dir'));

$disk->put($file->getClientOriginalName(), $file);
$disk->putFileAs('', $file, $file->getClientOriginalName());

return $this->stored($request, $model, $disk->path($file->getClientOriginalName()));
}
Expand All @@ -170,6 +167,7 @@ protected function stored(Request $request, Model $model, string $path): array

/** @var \Illuminate\Foundation\Auth\User&\Cone\Root\Interfaces\Models\User $user */
$user = $request->user();

$user->uploads()->save($medium);

MoveFile::withChain($medium->convertible() ? [new PerformConversions($medium)] : [])
Expand Down Expand Up @@ -254,7 +252,9 @@ public function toOption(Request $request, Model $model, Model $related): array
'fileName' => $related->file_name,
'isImage' => $related->isImage,
'processing' => false,
'url' => $related->hasConversion('thumbnail') ? $related->getUrl('thumbnail') : $related->getUrl(),
'url' => ! is_null($this->displayConversion) && $related->hasConversion($this->displayConversion)
? $related->getUrl($this->displayConversion)
: $related->getUrl(),
'uuid' => $related->uuid,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function display(Closure|string $callback): static
/**
* Resolve the display format or the query result.
*/
public function resolveDisplay(Model $related): mixed
public function resolveDisplay(Model $related): ?string
{
if (is_null($this->displayResolver)) {
$this->display($related->getKeyName());
Expand Down
43 changes: 43 additions & 0 deletions tests/Fields/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

Check warning on line 1 in tests/Fields/FileTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: no_unused_imports

namespace Cone\Root\Tests\Fields;

use Cone\Root\Fields\File;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;

class FileTest extends TestCase
{
protected File $field;

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

$this->field = new File('Documents');
}

public function test_a_file_field_has_file_template(): void
{
$this->assertSame('root::fields.file', $this->field->getTemplate());
}

public function test_a_file_field_stores_files(): void
{
$user = User::factory()->create();

$file = UploadedFile::fake()->image('cert.png');

$this->app['request']->setUserResolver(fn () => $user);
$this->app['request']->files->add(['documents' => $file]);

$this->field->collection('documents');
$this->field->persist($this->app['request'], $user, []);

$user->save();

$this->assertTrue($user->refresh()->documents->where('file_name', 'cert.png')->isNotEmpty());
}
}
6 changes: 6 additions & 0 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Database\Factories\UserFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class User extends Model implements MustVerifyEmail, RootUser
{
Expand All @@ -29,4 +30,9 @@ public function latestUpload(): HasOne
{
return $this->uploads()->one()->ofMany();
}

public function documents(): MorphToMany
{
return $this->media()->withPivotValue('collection', 'documents');
}
}

0 comments on commit f8349f9

Please sign in to comment.