Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make component_tag a polymorphic structure #2989

Merged
merged 3 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Foundation/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function boot(Dispatcher $dispatcher)
'metrics' => \CachetHQ\Cachet\Models\Metric::class,
'schedules' => \CachetHQ\Cachet\Models\Schedule::class,
'subscriber' => \CachetHQ\Cachet\Models\Subscriber::class,
'tags' => \CachetHQ\Cachet\Models\Tag::class,
]);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public function meta()
/**
* Get the tags relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function tags()
{
return $this->belongsToMany(Tag::class);
return $this->morphMany(Taggable::class, 'taggable');
}

/**
Expand Down
79 changes: 79 additions & 0 deletions app/Models/Taggable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CachetHQ\Cachet\Models;

use AltThree\TestBench\ValidationTrait;
use Illuminate\Database\Eloquent\Model;

/**
* This is the taggable model class.
*
* @author James Brooks <[email protected]>
*/
class Taggable extends Model
{
use ValidationTrait;

/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'tag_id' => 'int',
'taggable_id' => 'int',
'taggable_type' => 'string',
];

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'tag_id',
'taggable_id',
'taggable_type',
];

/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'tag_id' => 'required|int',
'taggable_id' => 'required|int',
'taggable_type' => 'required|string',
];

/**
* Get the tag relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function tag()
{
return $this->belongsTo(Tag::class);
}

/**
* Get the taggable relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function taggable()
{
return $this->morphTo();
}
}
42 changes: 42 additions & 0 deletions database/migrations/2018_04_02_163328_CreateTaggablesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

class CreateTaggablesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('taggables', function (Blueprint $table) {
$table->increments('id');
$table->integer('tag_id')->unsigned()->index();
$table->morphs('taggable');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('taggables');
}
}
57 changes: 57 additions & 0 deletions database/migrations/2018_04_02_163658_MigrateComponentTagTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use CachetHQ\Cachet\Models\Taggable;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class MigrateComponentTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Start by migrating the data into the new taggables field.
DB::table('component_tag')->get()->each(function ($tag) {
Taggable::create([
'tag_id' => $tag->tag_id,
'taggable_type' => 'components',
'taggable_id' => $tag->component_id,
]);
});

Schema::dropIfExists('component_tag');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::create('component_tag', function (Blueprint $table) {
$table->engine = 'InnoDB';

$table->increments('id');
$table->integer('component_id');
$table->integer('tag_id');

$table->index('component_id');
$table->index('tag_id');
});
}
}
31 changes: 31 additions & 0 deletions tests/Models/TaggableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CachetHQ\Tests\Cachet\Models;

use AltThree\TestBench\ValidationTrait;
use CachetHQ\Cachet\Models\Taggable;
use CachetHQ\Tests\Cachet\AbstractTestCase;

/**
* This is the taggable model test class.
*
* @author James Brooks <[email protected]>
*/
class TaggableTest extends AbstractTestCase
{
use ValidationTrait;

public function testValidation()
{
$this->checkRules(new Taggable());
}
}