From ac8d064a01d6fdde45bca8d529976a6788164ee6 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:23:46 +0300 Subject: [PATCH] Added the `is_hidden` field to the `Property` model --- Changelog.md | 1 + src/Properties/Changelog.md | 1 + src/Properties/Models/Property.php | 22 +++++++++++-- src/Properties/Tests/PropertyTest.php | 31 +++++++++++++++++++ ..._155708_add_hidden_field_to_properties.php | 23 ++++++++++++++ 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/Properties/resources/database/migrations/2023_08_23_155708_add_hidden_field_to_properties.php diff --git a/Changelog.md b/Changelog.md index 8e0b3880..286ac5ac 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,6 +13,7 @@ - Removed the Vanilo v2 `Framework` namespace compatibility layer - Added the `currency` field to the orders table - Added `currency` field to the Channel model/table +- Added the `is_hidden` field to the `Property` model ## 3.x Series diff --git a/src/Properties/Changelog.md b/src/Properties/Changelog.md index a6d1dbf4..f8d37a7d 100644 --- a/src/Properties/Changelog.md +++ b/src/Properties/Changelog.md @@ -5,6 +5,7 @@ ## Unreleased ##### 2023-XX-YY +- Added the `is_hidden` field to the `Property` model - Dropped PHP 8.0 & PHP 8.1 Support - Dropped Laravel 9 Support diff --git a/src/Properties/Models/Property.php b/src/Properties/Models/Property.php index bd715869..0598bdb5 100644 --- a/src/Properties/Models/Property.php +++ b/src/Properties/Models/Property.php @@ -16,6 +16,7 @@ use Cviebrock\EloquentSluggable\Sluggable; use Cviebrock\EloquentSluggable\SluggableScopeHelpers; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Collection; @@ -29,7 +30,11 @@ * @property string $slug * @property string $type * @property array $configuration + * @property bool $is_hidden * @property Collection $propertyValues + * + * @method static Builder hiddenOnes() + * @method static Builder visibleOnes() */ class Property extends Model implements PropertyContract { @@ -43,7 +48,8 @@ class Property extends Model implements PropertyContract protected $guarded = ['id', 'created_at', 'updated_at']; protected $casts = [ - 'configuration' => 'array' + 'configuration' => 'array', + 'is_hidden' => 'boolean', ]; public function getType(): PropertyType @@ -79,12 +85,22 @@ public function propertyValues(): HasMany return $this->hasMany(PropertyValueProxy::modelClass()); } + public function scopeHiddenOnes(Builder $query): Builder + { + return $query->where('is_hidden', true); + } + + public function scopeVisibleOnes(Builder $query): Builder + { + return $query->where('is_hidden', false); + } + public function sluggable(): array { return [ 'slug' => [ - 'source' => 'name' - ] + 'source' => 'name', + ], ]; } } diff --git a/src/Properties/Tests/PropertyTest.php b/src/Properties/Tests/PropertyTest.php index 31a7d174..dfe5c5b6 100644 --- a/src/Properties/Tests/PropertyTest.php +++ b/src/Properties/Tests/PropertyTest.php @@ -14,6 +14,7 @@ namespace Vanilo\Properties\Tests; +use Illuminate\Support\Str; use Vanilo\Properties\Models\Property; class PropertyTest extends TestCase @@ -25,6 +26,7 @@ public function all_mutable_fields_can_be_mass_assigned() 'name' => 'Funkiness', 'type' => 'text', 'slug' => 'funkiness', + 'is_hidden' => true, 'configuration' => ['x' => 'y', 'a' => 'b'] ]); @@ -32,6 +34,7 @@ public function all_mutable_fields_can_be_mass_assigned() $this->assertEquals('text', $property->type); $this->assertEquals('funkiness', $property->slug); $this->assertEquals(['x' => 'y', 'a' => 'b'], $property->configuration); + $this->assertTrue($property->is_hidden); } /** @test */ @@ -42,10 +45,15 @@ public function all_mutable_fields_can_be_set() $property->name = 'Creepiness'; $property->type = 'number'; $property->slug = 'creepiness'; + $property->is_hidden = true; $property->configuration = ['bam' => 'zdish', 'bumm' => 'tsish']; + $property->save(); + $property->refresh(); + $this->assertEquals('Creepiness', $property->name); $this->assertEquals('number', $property->type); + $this->assertTrue($property->is_hidden); $this->assertEquals('creepiness', $property->slug); $cfg = $property->configuration; @@ -125,4 +133,27 @@ public function it_can_be_retrieved_by_slug_using_the_static_finder_method() $this->assertInstanceOf(Property::class, $screen); $this->assertEquals('Screen Size', $screen->name); } + + /** @test */ + public function the_hidden_scopes_can_be_used_to_query_models() + { + for ($i = 0; $i < 8; $i++) { + Property::create([ + 'name' => Str::ulid()->toBase58(), + 'type' => 'text', + 'is_hidden' => true, + ]); + } + for ($i = 0; $i < 5; $i++) { + Property::create([ + 'name' => Str::ulid()->toBase58(), + 'type' => 'text', + 'is_hidden' => false, + ]); + } + + $this->assertEquals(5, Property::visibleOnes()->count()); + $this->assertEquals(8, Property::hiddenOnes()->count()); + + } } diff --git a/src/Properties/resources/database/migrations/2023_08_23_155708_add_hidden_field_to_properties.php b/src/Properties/resources/database/migrations/2023_08_23_155708_add_hidden_field_to_properties.php new file mode 100644 index 00000000..8635cf32 --- /dev/null +++ b/src/Properties/resources/database/migrations/2023_08_23_155708_add_hidden_field_to_properties.php @@ -0,0 +1,23 @@ +boolean('is_hidden')->default(false)->after('configuration'); + }); + } + + public function down(): void + { + Schema::table('properties', function (Blueprint $table) { + $table->dropColumn('is_hidden'); + }); + } +};