Skip to content

Commit

Permalink
rename metas to metaData
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Mar 8, 2023
1 parent d6ef8b5 commit 197f16e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public function up(): void
{
Schema::create('root_metas', static function (Blueprint $table): void {
Schema::create('root_meta_data', static function (Blueprint $table): void {
$table->id();
$table->uuidMorphs('metable');
$table->string('key')->index();
Expand All @@ -27,6 +27,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('root_metas');
Schema::dropIfExists('root_meta_data');
}
};
26 changes: 1 addition & 25 deletions src/Models/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Meta extends Model implements Contract
*
* @var string
*/
protected $table = 'root_metas';
protected $table = 'root_meta_data';

/**
* Get the proxied interface.
Expand All @@ -64,28 +64,4 @@ public function metable(): MorphTo
{
return $this->morphTo();
}

/**
* Get the casts array.
*/
public function getCasts(): array
{
$casts = parent::getCasts();

if (! isset($this->attributes['key'])
|| ! isset($this->attributes['metable_type'])
|| ! class_exists($this->attributes['metable_type'])) {
return $casts;
}

$model = new ($this->attributes['metable_type'])();

if (! method_exists($model, 'getMetaCasts')) {
return $casts;
}

return array_merge($casts, array_filter([
'value' => $model->getMetaCasts()[$this->attributes['key']] ?? null,
]));
}
}
25 changes: 0 additions & 25 deletions src/Traits/HasMeta.php

This file was deleted.

17 changes: 17 additions & 0 deletions src/Traits/HasMetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Cone\Root\Traits;

use Cone\Root\Models\Meta;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasMetaData
{
/**
* Get the meta data for the model.
*/
public function metaData(): MorphMany
{
return $this->morphMany(Meta::getProxiedClass(), 'metable');
}
}
10 changes: 5 additions & 5 deletions tests/Models/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Cone\Root\Models\Medium;
use Cone\Root\Models\Meta;
use Cone\Root\Tests\TestCase;
use Cone\Root\Traits\HasMeta;
use Cone\Root\Traits\HasMetaData;

class MetaTest extends TestCase
{
Expand All @@ -17,7 +17,7 @@ public function setUp(): void

$this->model = new class(Medium::factory()->make()->toArray()) extends Medium
{
use HasMeta;
use HasMetaData;
};

$this->model->save();
Expand All @@ -34,12 +34,12 @@ public function a_meta_belongs_to_a_metable()
}

/** @test */
public function a_metable_model_has_metas()
public function a_metable_model_has_meta_data()
{
$this->model->metas()->save(
$this->model->metaData()->save(
$meta = Meta::factory()->make()
);

$this->assertTrue($this->model->metas->first()->is($meta));
$this->assertTrue($this->model->metaData->first()->is($meta));
}
}

0 comments on commit 197f16e

Please sign in to comment.