Skip to content

Commit

Permalink
Added the is_hidden field to the Property model
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Aug 23, 2023
1 parent 6c6dad6 commit ac8d064
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Properties/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions src/Properties/Models/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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',
],
];
}
}
31 changes: 31 additions & 0 deletions src/Properties/Tests/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Vanilo\Properties\Tests;

use Illuminate\Support\Str;
use Vanilo\Properties\Models\Property;

class PropertyTest extends TestCase
Expand All @@ -25,13 +26,15 @@ public function all_mutable_fields_can_be_mass_assigned()
'name' => 'Funkiness',
'type' => 'text',
'slug' => 'funkiness',
'is_hidden' => true,
'configuration' => ['x' => 'y', 'a' => 'b']
]);

$this->assertEquals('Funkiness', $property->name);
$this->assertEquals('text', $property->type);
$this->assertEquals('funkiness', $property->slug);
$this->assertEquals(['x' => 'y', 'a' => 'b'], $property->configuration);
$this->assertTrue($property->is_hidden);
}

/** @test */
Expand All @@ -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;
Expand Down Expand Up @@ -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());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
public function up(): void
{
Schema::table('properties', function (Blueprint $table) {
$table->boolean('is_hidden')->default(false)->after('configuration');
});
}

public function down(): void
{
Schema::table('properties', function (Blueprint $table) {
$table->dropColumn('is_hidden');
});
}
};

0 comments on commit ac8d064

Please sign in to comment.