-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2989 from CachetHQ/polymorphic-taggables
Make component_tag a polymorphic structure
- Loading branch information
Showing
6 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
database/migrations/2018_04_02_163328_CreateTaggablesTable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
database/migrations/2018_04_02_163658_MigrateComponentTagTable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |