Skip to content

Commit 3ce373a

Browse files
authored
Improved rapidez/statamic compatibility (#1175)
1 parent 2613c03 commit 3ce373a

8 files changed

Lines changed: 49 additions & 30 deletions

File tree

resources/views/product/overview.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<dt>SKU</dt>
4545
<dd>{{ $product->sku }}</dd>
4646
@foreach (\Rapidez\Core\Models\EavAttribute::getCachedCatalog()->where(fn ($attribute) => $attribute->is_visible_on_front) as $attribute)
47-
@if (($value = $product->{$attribute['attribute_code']}))
47+
@if (($value = $product->label($attribute['attribute_code'])))
4848
<dt>{{ $attribute['frontend_label'] }}</dt>
4949
@if ($attribute['is_html_allowed_on_front'])
5050
<dd>{!! $value !!}</dd>

src/Models/ProductLink.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,20 @@
55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Database\Eloquent\Relations\HasMany;
77
use Illuminate\Database\Eloquent\Relations\HasOne;
8-
use Rapidez\Core\Models\Traits\HasCustomAttributes;
98

109
class ProductLink extends Model
1110
{
12-
use HasCustomAttributes;
13-
1411
const CREATED_AT = null;
1512
const UPDATED_AT = null;
1613

1714
protected $table = 'catalog_product_link';
1815

19-
protected $attributeTypes = ['int', 'decimal', 'varchar'];
20-
protected $attributeTablePrefix = 'catalog_product_link_attribute';
21-
protected $attributeCode = 'product_link_attribute_code';
22-
2316
protected $primaryKey = 'link_id';
2417

2518
protected static function boot()
2619
{
2720
parent::boot();
2821

29-
static::withCustomAttributes();
3022
static::addGlobalScope('withLinkType', fn (Builder $builder) => $builder->join('catalog_product_link_type', 'catalog_product_link_type.link_type_id', '=', 'catalog_product_link.link_type_id'));
3123
}
3224

src/Models/Scopes/Product/ForCurrentWebsiteScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ForCurrentWebsiteScope implements Scope
1010
{
1111
public function apply(Builder $builder, Model $model)
1212
{
13-
$builder->leftJoin('catalog_product_website', 'catalog_product_website.product_id', '=', $model->getQualifiedKeyName());
13+
$builder->leftJoin('catalog_product_website', 'catalog_product_website.product_id', '=', $model->qualifyColumn('entity_id'));
1414
$builder->where('website_id', config('rapidez.website'));
1515
}
1616
}

src/Models/Traits/HasCustomAttributes.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public function attributeDatetime(): HasMany
130130
return $this->hasManyWithAttributeTypeTable(
131131
config('rapidez.models.attribute_datetime', AttributeDatetime::class),
132132
'datetime',
133-
$this->primaryKey,
134-
$this->primaryKey,
133+
'entity_id',
134+
'entity_id',
135135
);
136136
}
137137

@@ -140,8 +140,8 @@ public function attributeDecimal(): HasMany
140140
return $this->hasManyWithAttributeTypeTable(
141141
config('rapidez.models.attribute_decimal', AttributeDecimal::class),
142142
'decimal',
143-
$this->primaryKey,
144-
$this->primaryKey,
143+
'entity_id',
144+
'entity_id',
145145
);
146146
}
147147

@@ -150,8 +150,8 @@ public function attributeInt(): HasMany
150150
return $this->hasManyWithAttributeTypeTable(
151151
config('rapidez.models.attribute_int', AttributeInt::class),
152152
'int',
153-
$this->primaryKey,
154-
$this->primaryKey,
153+
'entity_id',
154+
'entity_id',
155155
);
156156
}
157157

@@ -160,8 +160,8 @@ public function attributeText(): HasMany
160160
return $this->hasManyWithAttributeTypeTable(
161161
config('rapidez.models.attribute_text', AttributeText::class),
162162
'text',
163-
$this->primaryKey,
164-
$this->primaryKey,
163+
'entity_id',
164+
'entity_id',
165165
);
166166
}
167167

@@ -170,8 +170,8 @@ public function attributeVarchar(): HasMany
170170
return $this->hasManyWithAttributeTypeTable(
171171
config('rapidez.models.attribute_varchar', AttributeVarchar::class),
172172
'varchar',
173-
$this->primaryKey,
174-
$this->primaryKey,
173+
'entity_id',
174+
'entity_id',
175175
);
176176
}
177177

@@ -214,11 +214,39 @@ public function customAttributes(): AttributeCast
214214

215215
public function getCustomAttribute($key)
216216
{
217+
if (! $this->exists) {
218+
return null;
219+
}
220+
217221
return $this->customAttributes[$key] ?? null;
218222
}
219223

224+
public function hasCustomAttribute($key): bool
225+
{
226+
return $this->exists && isset($this->customAttributes[$key]);
227+
}
228+
220229
protected function throwMissingAttributeExceptionIfApplicable($key)
221230
{
222-
return $this->getCustomAttribute($key);
231+
if ($this->hasCustomAttribute($key)) {
232+
return $this->value($key);
233+
}
234+
235+
return parent::throwMissingAttributeExceptionIfApplicable($key);
236+
}
237+
238+
public function value(string $key): mixed
239+
{
240+
return $this->getCustomAttribute($key)?->value ?? null;
241+
}
242+
243+
public function raw(string $key): mixed
244+
{
245+
return $this->getCustomAttribute($key)?->rawValue ?? null;
246+
}
247+
248+
public function label(string $key): ?string
249+
{
250+
return $this->getCustomAttribute($key)?->label ?? null;
223251
}
224252
}

src/Models/Traits/Product/HasSuperAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function superAttributeValues(): Attribute
3838
->mapWithKeys(fn ($attribute) => [
3939
$attribute->attribute_code => $this->children
4040
->mapWithKeys(fn ($child) => [
41-
$child->entity_id => $child->{$attribute->attribute_code},
41+
$child->entity_id => $child->getCustomAttribute($attribute->attribute_code),
4242
])
4343
->sortBy('sort_order')
4444
->groupBy('rawValue')

src/Models/Traits/Product/Searchable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function withCategories(array $data): array
146146
for ($i = 1; $i <= $level; $i++) {
147147
$pathCategories = collect($path)
148148
->take($i)
149-
->map(fn ($id) => $categories[$id]->name->value ?? null)
149+
->map(fn ($id) => $categories[$id]->name ?? null)
150150
->whereNotNull()
151151
->join(' > ');
152152

tests/Unit/ProductTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ public function product_has_custom_attributes()
161161
{
162162
$product = Product::find(10);
163163

164-
$this->assertEqualsCanonicalizing([8, 11], iterator_to_array($product->activity->rawValue), 'Activity attribute on product 10 did not return the right values.');
165-
$this->assertEqualsCanonicalizing(['Gym', 'Yoga'], iterator_to_array($product->activity->value), 'Activity attribute on product 10 did not return the right values.');
166-
$this->assertEquals('Gym, Yoga', $product->activity->label, 'Activity attribute on product 10 did not yield the right text output.');
164+
$this->assertEqualsCanonicalizing([8, 11], iterator_to_array($product->raw('activity')), 'Activity attribute on product 10 did not return the right values.');
165+
$this->assertEqualsCanonicalizing(['Gym', 'Yoga'], iterator_to_array($product->activity), 'Activity attribute on product 10 did not return the right values.');
166+
$this->assertEquals('Gym, Yoga', $product->label('activity'), 'Activity attribute on product 10 did not yield the right text output.');
167167

168-
$this->assertEquals('Savvy Shoulder Tote', $product->name->label, 'Name attribute on product 10 did not return the right value.');
169-
$this->assertEquals($product->name->label, $product->name, 'Product name did not return the right value or did not cast its value correctly.');
168+
$this->assertEquals('Savvy Shoulder Tote', $product->label('name'), 'Name attribute on product 10 did not return the right label.');
170169
}
171170

172171
#[Test]
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)