Skip to content

Commit

Permalink
Allow specification of type when calling attachTags() or detachTags() (
Browse files Browse the repository at this point in the history
…#273)

* Allow specification of type when calling attachTags()

* StyleCI fix

* Detach method can filter by type

* Add tests for attach/detach with type

* Add attach/detach w/type examples to readme

* Style CI fixes on test
  • Loading branch information
zbmowrey authored Aug 22, 2020
1 parent 5abedfd commit c3ebf5e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ $newsItem = NewsItem::create([

//attaching tags
$newsItem->attachTag('third tag');
$newsItem->attachTag('third tag','some_type');
$newsItem->attachTags(['fourth tag', 'fifth tag']);
$newsItem->attachTags(['fourth_tag','fifth_tag'],'some_type');

//detaching tags
$newsItem->detachTags('third tag');
$newsItem->detachTags('third tag','some_type');
$newsItem->detachTags(['fourth tag', 'fifth tag']);
$newsItem->detachTags(['fourth tag', 'fifth tag'],'some_type');

//syncing tags
$newsItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached
Expand Down
21 changes: 12 additions & 9 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ public function tagsWithType(string $type = null): Collection

/**
* @param array|\ArrayAccess|\Spatie\Tags\Tag $tags
*
* @param string|null $type
* @return $this
*/
public function attachTags($tags)
public function attachTags($tags, string $type = null)
{
$className = static::getTagClassName();

$tags = collect($className::findOrCreate($tags));
$tags = collect($className::findOrCreate($tags, $type));

$this->tags()->syncWithoutDetaching($tags->pluck('id')->toArray());

Expand All @@ -156,21 +156,23 @@ public function attachTags($tags)
/**
* @param string|\Spatie\Tags\Tag $tag
*
* @param string|null $type
* @return $this
*/
public function attachTag($tag)
public function attachTag($tag, string $type = null)
{
return $this->attachTags([$tag]);
return $this->attachTags([$tag], $type);
}

/**
* @param array|\ArrayAccess $tags
*
* @param string|null $type
* @return $this
*/
public function detachTags($tags)
public function detachTags($tags, string $type = null)
{
$tags = static::convertToTags($tags);
$tags = static::convertToTags($tags, $type);

collect($tags)
->filter()
Expand All @@ -184,11 +186,12 @@ public function detachTags($tags)
/**
* @param string|\Spatie\Tags\Tag $tag
*
* @param string|null $type
* @return $this
*/
public function detachTag($tag)
public function detachTag($tag, string $type = null)
{
return $this->detachTags([$tag]);
return $this->detachTags([$tag], $type);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/HasTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public function it_can_attach_a_tag()
$this->assertEquals(['tagName'], $this->testModel->tags->pluck('name')->toArray());
}

public function it_can_attach_a_tag_with_a_type()
{
$this->testModel->attachTag('tagName', 'testType');

$this->assertCount(1, $this->testModel->tags);

$this->assertEquals(['tagName'], $this->testModel->tags->pluck('name')->toArray());

$this->assertEquals(['testType'], $this->testModel->tags->pluck('type')->toArray());
}

/** @test */
public function it_can_attach_a_tag_multiple_times_without_creating_duplicate_entries()
{
Expand Down Expand Up @@ -99,6 +110,14 @@ public function it_can_attach_multiple_tags()
$this->assertCount(2, $this->testModel->tags);
}

/** @test */
public function it_can_attach_multiple_tags_with_a_type()
{
$this->testModel->attachTags(['test1', 'test2'], 'testType');

$this->assertCount(2, $this->testModel->tags->where('type', '=', 'testType')->toArray());
}

/** @test */
public function it_can_attach_a_existing_tag()
{
Expand All @@ -117,6 +136,44 @@ public function it_can_detach_a_tag()
$this->assertEquals(['test1', 'test3'], $this->testModel->tags->pluck('name')->toArray());
}

/** @test */
public function it_can_detach_a_tag_with_a_type()
{
$this->testModel->attachTags(['test1', 'test2'], 'testType');

$this->testModel->detachTag('test2', 'testType');

$this->assertEquals(['test1'], $this->testModel->tags->pluck('name')->toArray());
}

/** @test */
public function it_can_detach_a_tag_with_a_type_and_not_affect_a_tag_without_a_type()
{
$this->testModel->attachTag('test1', 'testType');

$this->testModel->attachTag('test1');

$this->testModel->detachTag('test1', 'testType');

$this->assertEquals(['test1'], $this->testModel->tags->pluck('name')->toArray());

$this->assertNull($this->testModel->tags->where('name', '=', 'test1')->first()->type);
}

/** @test */
public function it_can_detach_a_tag_with_a_type_while_leaving_another_of_a_different_type()
{
$this->testModel->attachTag('test1', 'testType');

$this->testModel->attachTag('test1', 'otherType');

$this->testModel->detachTag('test1', 'testType');

$this->assertEquals(['test1'], $this->testModel->tags->pluck('name')->sort()->toArray());

$this->assertEquals('otherType', $this->testModel->tags->where('name', '=', 'test1')->first()->type);
}

/** @test */
public function it_can_detach_multiple_tags()
{
Expand Down

0 comments on commit c3ebf5e

Please sign in to comment.