Skip to content

Commit

Permalink
doc: add remark for custom model definition (#1966)
Browse files Browse the repository at this point in the history
when a Lunar model is extended/replaced, the media definition config
should be updated. Or standard definition will be used

p/s: not sure should the media definition to change to string alias or
internally auto resolve to lunar model fqcn

---------

Co-authored-by: Alec Ritson <[email protected]>
  • Loading branch information
wychoong and alecritson authored Nov 19, 2024
1 parent 62c41ee commit 1ce7206
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/core/config/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
return [

'definitions' => [
Lunar\Models\Asset::class => StandardMediaDefinitions::class,
Lunar\Models\Brand::class => StandardMediaDefinitions::class,
Lunar\Models\Collection::class => StandardMediaDefinitions::class,
Lunar\Models\Product::class => StandardMediaDefinitions::class,
Lunar\Models\ProductOption::class => StandardMediaDefinitions::class,
Lunar\Models\ProductOptionValue::class => StandardMediaDefinitions::class,
'asset' => StandardMediaDefinitions::class,
'brand' => StandardMediaDefinitions::class,
'collection' => StandardMediaDefinitions::class,
'product' => StandardMediaDefinitions::class,
'product-option' => StandardMediaDefinitions::class,
'product-option-value' => StandardMediaDefinitions::class,
],

'collection' => 'images',
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/Base/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ protected function getDefinitionClass()
{
$conversionClasses = config('lunar.media.definitions', []);

return $conversionClasses[static::class] ?? StandardMediaDefinitions::class;
$alias = \Illuminate\Support\Str::snake(class_basename(static::class));

return $conversionClasses[$alias]
?? $conversionClasses[static::class] // fallback for published config
?? $conversionClasses[get_parent_class(static::class)] // fallback use parent class
?? StandardMediaDefinitions::class;
}
}
7 changes: 7 additions & 0 deletions tests/core/Stubs/TestStandardMediaDefinitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Lunar\Tests\Core\Stubs;

use Lunar\Base\StandardMediaDefinitions;

class TestStandardMediaDefinitions extends StandardMediaDefinitions {}
29 changes: 28 additions & 1 deletion tests/core/Unit/Traits/HasMediaTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

uses(\Lunar\Tests\Core\TestCase::class);
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Lunar\Base\StandardMediaDefinitions;
use Lunar\Models\Product;
use Lunar\Tests\Core\Stubs\TestStandardMediaDefinitions;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);

Expand All @@ -12,7 +14,7 @@

expect($definitions)->toHaveCount(6);

expect($definitions[Product::class])->toEqual(StandardMediaDefinitions::class);
expect($definitions['product'])->toEqual(StandardMediaDefinitions::class);

$file = UploadedFile::fake()->image('avatar.jpg');

Expand All @@ -28,6 +30,31 @@
expect($image->hasGeneratedConversion('zoom'))->toBeTrue();
});

test('custom conversions are loaded', function () {
Config::set('lunar.media.definitions', [
'product' => TestStandardMediaDefinitions::class,
]);

$product = invade(new Product);

expect($product->getDefinitionClass())->toEqual(TestStandardMediaDefinitions::class);
});

test('custom conversions are loaded for extended model', function () {
\Lunar\Facades\ModelManifest::replace(
\Lunar\Models\Contracts\Product::class,
\Lunar\Tests\Core\Stubs\Models\Product::class
);

Config::set('lunar.media.definitions', [
'product' => TestStandardMediaDefinitions::class,
]);

$product = invade(app(\Lunar\Models\Contracts\Product::class));

expect($product->getDefinitionClass())->toEqual(TestStandardMediaDefinitions::class);
});

test('images can have fallback url', function () {
$testImageUrl = 'https://picsum.photos/200';
config()->set('lunar.media.fallback.url', $testImageUrl);
Expand Down

0 comments on commit 1ce7206

Please sign in to comment.