From 54c293dbb95b4d713aab95c97e6673941a839c3f Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Fri, 25 Aug 2023 08:34:46 +0200 Subject: [PATCH 1/3] Make and use "filterable" attribute --- config/rapidez.php | 2 ++ resources/js/components/Listing/Listing.vue | 2 +- routes/api.php | 2 +- src/Models/Attribute.php | 10 ++++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/config/rapidez.php b/config/rapidez.php index 6b86ff619..aab2daa54 100644 --- a/config/rapidez.php +++ b/config/rapidez.php @@ -145,4 +145,6 @@ 'slideover' => 'z-50', ], + // Attribute codes of attributes that need to be filtered but can't be set as filterable in magento + 'extra-filters' => [], ]; diff --git a/resources/js/components/Listing/Listing.vue b/resources/js/components/Listing/Listing.vue index dd328cbc1..22d5d6670 100644 --- a/resources/js/components/Listing/Listing.vue +++ b/resources/js/components/Listing/Listing.vue @@ -49,7 +49,7 @@ export default { computed: { filters: function () { return Object.values(this.attributes) - .filter((attribute) => attribute.filter) + .filter((attribute) => attribute.filterable) .sort((a, b) => a.position - b.position) }, sortings: function () { diff --git a/routes/api.php b/routes/api.php index 63dbbe4fd..99e13514f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -10,7 +10,7 @@ $attributeModel = config('rapidez.models.attribute'); return $attributeModel::getCachedWhere(function ($attribute) { - return $attribute['filter'] || $attribute['sorting']; + return $attribute['filterable'] || $attribute['sorting']; }); }); diff --git a/src/Models/Attribute.php b/src/Models/Attribute.php index c79d84d33..5a7196c1d 100644 --- a/src/Models/Attribute.php +++ b/src/Models/Attribute.php @@ -2,6 +2,7 @@ namespace Rapidez\Core\Models; +use Illuminate\Database\Eloquent\Casts\Attribute as CastsAttribute; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; use Rapidez\Core\Models\Scopes\Attribute\OnlyProductAttributesScope; @@ -12,11 +13,20 @@ class Attribute extends Model protected $primaryKey = 'attribute_id'; + protected $appends = ['filterable']; + protected static function booting() { static::addGlobalScope(new OnlyProductAttributesScope); } + protected function filterable(): CastsAttribute + { + return CastsAttribute::make( + get: fn () => $this->filter || in_array($this->code, config('rapidez.extra-filters')), + )->shouldCache(); + } + public static function getCachedWhere(callable $callback): array { if (! $attributes = config('cache.app.attributes.' . config('rapidez.store'))) { From 1461d812301e7ea63248e4a4cba8a76380a91cb4 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Fri, 25 Aug 2023 09:32:28 +0200 Subject: [PATCH 2/3] Just use `filter` with accessor --- resources/js/components/Listing/Listing.vue | 2 +- routes/api.php | 2 +- src/Models/Attribute.php | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/resources/js/components/Listing/Listing.vue b/resources/js/components/Listing/Listing.vue index 22d5d6670..dd328cbc1 100644 --- a/resources/js/components/Listing/Listing.vue +++ b/resources/js/components/Listing/Listing.vue @@ -49,7 +49,7 @@ export default { computed: { filters: function () { return Object.values(this.attributes) - .filter((attribute) => attribute.filterable) + .filter((attribute) => attribute.filter) .sort((a, b) => a.position - b.position) }, sortings: function () { diff --git a/routes/api.php b/routes/api.php index 99e13514f..63dbbe4fd 100644 --- a/routes/api.php +++ b/routes/api.php @@ -10,7 +10,7 @@ $attributeModel = config('rapidez.models.attribute'); return $attributeModel::getCachedWhere(function ($attribute) { - return $attribute['filterable'] || $attribute['sorting']; + return $attribute['filter'] || $attribute['sorting']; }); }); diff --git a/src/Models/Attribute.php b/src/Models/Attribute.php index 5a7196c1d..f5fe3384c 100644 --- a/src/Models/Attribute.php +++ b/src/Models/Attribute.php @@ -13,17 +13,15 @@ class Attribute extends Model protected $primaryKey = 'attribute_id'; - protected $appends = ['filterable']; - protected static function booting() { static::addGlobalScope(new OnlyProductAttributesScope); } - protected function filterable(): CastsAttribute + protected function filter(): CastsAttribute { return CastsAttribute::make( - get: fn () => $this->filter || in_array($this->code, config('rapidez.extra-filters')), + get: fn ($value) => $value || in_array($this->code, config('rapidez.extra-filters')), )->shouldCache(); } From 81c310106c8dc570c1c9d41fb745ccfd1a89c600 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Fri, 25 Aug 2023 09:58:31 +0200 Subject: [PATCH 3/3] additional_filters & documentation --- config/rapidez.php | 8 ++++++-- src/Models/Attribute.php | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/config/rapidez.php b/config/rapidez.php index aab2daa54..1fa1c34a7 100644 --- a/config/rapidez.php +++ b/config/rapidez.php @@ -145,6 +145,10 @@ 'slideover' => 'z-50', ], - // Attribute codes of attributes that need to be filtered but can't be set as filterable in magento - 'extra-filters' => [], + // From Magento only "Yes/No, Dropdown, Multiple Select and Price" attribute types + // can be configured as filter. If you'd like to have a filter for an attribute + // with, for example, the type of "Text", you can specify the attribute code here. + 'additional_filters' => [ + // Attribute codes + ], ]; diff --git a/src/Models/Attribute.php b/src/Models/Attribute.php index f5fe3384c..9318ddf94 100644 --- a/src/Models/Attribute.php +++ b/src/Models/Attribute.php @@ -21,7 +21,7 @@ protected static function booting() protected function filter(): CastsAttribute { return CastsAttribute::make( - get: fn ($value) => $value || in_array($this->code, config('rapidez.extra-filters')), + get: fn ($value) => $value || in_array($this->code, config('rapidez.additional_filters')), )->shouldCache(); }