Tag syncing is now done within the Component commands

This commit is contained in:
James Brooks
2019-07-12 10:29:23 +01:00
parent 06af61b8c5
commit 6810af48f7
10 changed files with 197 additions and 64 deletions
+27
View File
@@ -55,4 +55,31 @@ class Tag extends Model
{
return $this->belongsToMany(Component::class);
}
/**
* @param array|\ArrayAccess $values
*
* @return \CachetHQ\Cachet\Models\Tag|static
*/
public static function findOrCreate($values)
{
$tags = collect($values)->map(function ($value) {
if ($value instanceof Tag) {
return $value;
}
$tag = static::where('name', '=', $value)->first();
if (!$tag instanceof Tag) {
$tag = static::create([
'name' => $value,
'slug' => Str::slug($value),
]);
}
return $tag;
});
return is_string($values) ? $tags->first() : $tags;
}
}
+110 -2
View File
@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Models\Traits;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* This is the has tags trait.
@@ -21,6 +22,33 @@ use Illuminate\Database\Eloquent\Builder;
*/
trait HasTags
{
/**
* @var array
*/
protected $queuedTags = [];
/**
* Boot the trait.
*
* @return void
*/
public static function bootHasTags()
{
static::created(function (Model $taggableModel) {
if (count($taggableModel->queuedTags) > 0) {
$taggableModel->attachTags($taggableModel->queuedTags);
$taggableModel->queuedTags = [];
}
});
static::deleted(function (Model $deletedModel) {
$tags = $deletedModel->tags()->get();
$deletedModel->detachTags($tags);
});
}
/**
* Get the tags relation.
*
@@ -31,6 +59,20 @@ trait HasTags
return $this->morphToMany(Tag::class, 'taggable');
}
/**
* @param string|array|\ArrayAccess|\CachetHQ\Cachet\Models\Tag $tags
*/
public function setTagsAttribute($tags)
{
if (! $this->exists) {
$this->queuedTags = $tags;
return;
}
$this->attachTags($tags);
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array|\ArrayAccess $tags
@@ -61,12 +103,78 @@ trait HasTags
$tags = static::convertToTags($tags);
return $query->whereHas('tags', function (Builder $query) use ($tags) {
$tagIds = $tags->pluck('id')->toArray();
$tagIds = collect($tags)->pluck('id');
$query->whereIn('taggables.tag_id', $tagIds);
$query->whereIn('tags.id', $tagIds);
});
}
/**
* @param array|\ArrayAccess|\CachetHQ\Cachet\Models\Tag $tags
*
* @return $this
*/
public function attachTags($tags)
{
$tags = collect(Tag::findOrCreate($tags));
$this->tags()->syncWithoutDetaching($tags->pluck('id')->toArray());
return $this;
}
/**
* @param string|\CachetHQ\Cachet\Models\Tag $tag
*
* @return $this
*/
public function attachTag($tag)
{
return $this->attachTags([$tag]);
}
/**
* @param array|\ArrayAccess $tags
*
* @return $this
*/
public function detachTags($tags)
{
$tags = static::convertToTags($tags);
collect($tags)
->filter()
->each(function (Tag $tag) {
$this->tags()->detach($tag);
});
return $this;
}
/**
* @param string|\CachetHQ\Cachet\Models\Tag $tag
*
* @return $this
*/
public function detachTag($tag)
{
return $this->detachTags([$tag]);
}
/**
* @param array|\ArrayAccess $tags
*
* @return $this
*/
public function syncTags($tags)
{
$tags = collect(Tag::findOrCreate($tags));
$this->tags()->sync($tags->pluck('id')->toArray());
return $this;
}
/**
* Convert a list of tags into a collection of \CachetHQ\Cachet\Models\Tag.
*