Skip to content

Commit 38c6b38

Browse files
[4.x] Always load model for assets (#479)
* Always load model for assets * Fixing test * Fix test, attempt 2 * Fixing test, attempt 3 * Nitpicks - early return and formatting * Update test to be more meaningful with explanation * Revert test changes and make a separate test --------- Co-authored-by: Ryan Mitchell <[email protected]>
1 parent ffb8981 commit 38c6b38

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Assets/Asset.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ public static function makeModelFromContract(AssetContract $source, $meta = [])
173173
public function model($model = null)
174174
{
175175
if (func_num_args() === 0) {
176+
if ($this->model) {
177+
return $this->model;
178+
}
179+
180+
$this->model = app('statamic.eloquent.assets.model')::query()
181+
->where([
182+
'container' => $this->containerHandle(),
183+
'folder' => $this->folder(),
184+
'basename' => $this->basename(),
185+
])
186+
->first();
187+
176188
return $this->model;
177189
}
178190

tests/Assets/AssetTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ protected function setUp(): void
5858

5959
Storage::disk('test')->put('f.jpg', '');
6060
Facades\Asset::make()->container('test')->path('f.jpg')->save();
61+
62+
Storage::disk('test')->put('test-folder/test.jpg', '');
6163
}
6264

6365
#[Test]
@@ -70,7 +72,7 @@ public function it_loads_from_asset_model()
7072
'basename' => 'test.jpg',
7173
'filename' => 'test',
7274
'extension' => 'jpg',
73-
'meta' => ['width' => 100, 'height' => 100, 'data' => []],
75+
'meta' => ['width' => 100, 'height' => 100, 'data' => ['focus' => '50-50-1']],
7476
]);
7577

7678
$asset = (new Asset)->fromModel($model);
@@ -81,7 +83,33 @@ public function it_loads_from_asset_model()
8183
$this->assertSame('test.jpg', $asset->basename());
8284
$this->assertSame('test', $asset->filename());
8385
$this->assertSame('jpg', $asset->extension());
84-
$this->assertSame(['width' => 100, 'height' => 100, 'data' => []], $asset->meta());
86+
$this->assertSame(['width' => 100, 'height' => 100, 'data' => ['focus' => '50-50-1']], $asset->meta());
87+
}
88+
89+
#[Test]
90+
public function it_loads_from_an_existing_model_outside_the_query_builder()
91+
{
92+
$model = new AssetModel([
93+
'container' => 'test',
94+
'path' => 'test-folder/test.jpg',
95+
'folder' => 'test-folder',
96+
'basename' => 'test.jpg',
97+
'filename' => 'test',
98+
'extension' => 'jpg',
99+
'meta' => ['width' => 100, 'height' => 100, 'data' => ['focus' => '50-50-1']],
100+
]);
101+
102+
$model->save();
103+
104+
$asset = $this->container->asset($model->path);
105+
106+
$this->assertSame($model->getKey(), $asset->model()->getKey());
107+
$this->assertSame('test-folder/test.jpg', $asset->path());
108+
$this->assertSame('test-folder', $asset->folder());
109+
$this->assertSame('test.jpg', $asset->basename());
110+
$this->assertSame('test', $asset->filename());
111+
$this->assertSame('jpg', $asset->extension());
112+
$this->assertSame(['width' => 100, 'height' => 100, 'data' => ['focus' => '50-50-1']], $asset->meta());
85113
}
86114

87115
#[Test]
@@ -216,11 +244,13 @@ public function can_save_an_asset_made_on_the_container()
216244

217245
$asset = $this->container->makeAsset('a.jpg');
218246

219-
$this->assertNull($asset->model());
247+
$model = $asset->model(); // it should find the existing model meta
248+
$this->assertNotNull($model);
220249

221250
$asset->save();
222251

223252
$this->assertNotNull($asset->model());
253+
$this->assertSame($model, $asset->model());
224254

225255
Event::assertDispatched(AssetSaved::class, fn ($event) => $event->asset === $asset);
226256

0 commit comments

Comments
 (0)