Skip to content

Commit

Permalink
ADD allow saving additional fields via add method (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalSloth authored Aug 21, 2023
1 parent be60822 commit ef3c871
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ Like::has($course, $user); // returns whether the given user has marked as liked
Like::count($course); // returns the amount of like marks for the given course
```

### Custom metadata

If needed, you may also add custom metadata when assigning a mark:

``` php
use App\Models\Course;
use Maize\Markable\Models\Like;

$course = Course::firstOrFail();
$user = auth()->user();

Like::add($course, $user, [
'topic' => $course->topic,
]);

Like::toggle($course, $user, [
'topic' => $course->topic,
]);
```

### Custom mark model

The package allows you to define custom marks.
Expand Down
22 changes: 16 additions & 6 deletions src/Mark.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Maize\Markable\Exceptions\InvalidMarkableInstanceException;
use Maize\Markable\Exceptions\InvalidMarkValueException;
Expand All @@ -15,6 +16,10 @@ abstract class Mark extends MorphPivot
{
public $incrementing = true;

protected $casts = [
'metadata' => 'array',
];

abstract public static function markableRelationName(): string;

public static function markRelationName(): string
Expand All @@ -37,7 +42,7 @@ public static function getMarkClassName(): string
->__toString();
}

public static function add(Model $markable, Model $user, string $value = null): self
public static function add(Model $markable, Model $user, string $value = null, array $metadata = []): self
{
static::validMarkable($markable);

Expand All @@ -51,9 +56,14 @@ public static function add(Model $markable, Model $user, string $value = null):
'markable_type' => $markable->getMorphClass(),
'value' => $value,
];
$values = static::forceSingleValuePerUser()
? [Arr::pull($attributes, 'value')]
: [];

$values = collect([
'metadata' => $metadata,
])->when(
value: static::forceSingleValuePerUser(),
callback: fn (Collection $values) => $values
->add(Arr::pull($attributes, 'value'))
)->toArray();

return static::firstOrCreate($attributes, $values);
}
Expand Down Expand Up @@ -91,11 +101,11 @@ public static function has(Model $markable, Model $user, string $value = null):
])->exists();
}

public static function toggle(Model $markable, Model $user, string $value = null)
public static function toggle(Model $markable, Model $user, string $value = null, array $metadata = [])
{
return static::has($markable, $user, $value)
? static::remove($markable, $user, $value)
: static::add($markable, $user, $value);
: static::add($markable, $user, $value, $metadata);
}

public static function hasAllowedValues(?string $value): bool
Expand Down
32 changes: 32 additions & 0 deletions tests/MarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Maize\Markable\Exceptions\InvalidMarkableInstanceException;
use Maize\Markable\Models\Bookmark;
use Maize\Markable\Models\Favorite;
use Maize\Markable\Models\Like;
use Maize\Markable\Tests\Models\Article;
use Maize\Markable\Tests\Models\Post;
Expand All @@ -27,6 +28,37 @@ public function can_add_a_mark()
]);
}

/** @test */
public function can_add_metadata()
{
$article = Article::factory()->create();
$user = User::factory()->create();

Favorite::add($article, $user, null, [
'test_data' => true,
]);

$this->assertDatabaseHas((new Favorite)->getTable(), [
'user_id' => $user->getKey(),
'markable_id' => $article->getKey(),
'markable_type' => $article->getMorphClass(),
'value' => null,
'metadata' => json_encode(['test_data' => true]),
]);

Like::toggle($article, $user, null, [
'test_data' => true,
]);

$this->assertDatabaseHas((new Like)->getTable(), [
'user_id' => $user->getKey(),
'markable_id' => $article->getKey(),
'markable_type' => $article->getMorphClass(),
'value' => null,
'metadata' => json_encode(['test_data' => true]),
]);
}

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

0 comments on commit ef3c871

Please sign in to comment.