Skip to content

Commit

Permalink
feat: allow configuration for reacts_model and reacts_id_column
Browse files Browse the repository at this point in the history
  • Loading branch information
hkp22 committed Jun 11, 2024
1 parent 0d5c9e3 commit 8ddf93f
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 41 deletions.
17 changes: 17 additions & 0 deletions config/reactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@
*/
'table_name' => 'reactions',

/**
* The name of the model that will be used to represent
* the model (eg. User) that is reacting to a reactable model (eg. Post).
*
* If this is set to null, the package will attempt to use
* the default user model for your application.
*/
'reacts_model' => null,

/*
* This is the name of the column on the "reactions" table that will be used to
* identify the "reacts_model" relationship.
*
* If this is set to null, the package will attempt to use the "user_id" column
* on the "Reaction" model.
*/
'reacts_id_column' => null,
];
7 changes: 5 additions & 2 deletions migrations/2018_07_10_000000_create_reactions_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Qirolab\Laravel\Reactions\Helper;

/**
* Class CreateLoveLikesTable.
Expand All @@ -17,15 +18,17 @@ class CreateReactionsTable extends Migration
public function up()
{
Schema::create(config('reactions.table_name', 'reactions'), function (Blueprint $table) {
$userIdColumn = Helper::resolveReactsIdColumn();

$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->integer($userIdColumn)->unsigned()->index();
$table->morphs('reactable');
$table->string('type')->nullable();
$table->timestamps();
$table->unique([
'reactable_type',
'reactable_id',
'user_id',
$userIdColumn,
], 'react_user_unique');
// $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Expand Down
36 changes: 36 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Qirolab\Laravel\Reactions;

class Helper
{
/**
* Retrieve User's model class name.
*
* @return string
*/
public static function resolveReactsModel()
{
$userModel = config('reactions.reacts_model');
if ($userModel) {
return $userModel;
}

return config('auth.providers.users.model');
}

/**
* Retrieve User's model column name in reactions table.
*
* @return string
*/
public static function resolveReactsIdColumn()
{
$userModel = config('reactions.reacts_id_column');
if ($userModel) {
return $userModel;
}

return 'user_id';
}
}
13 changes: 6 additions & 7 deletions src/Models/Reaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Qirolab\Laravel\Reactions\Models;

use Illuminate\Database\Eloquent\Model;
use Qirolab\Laravel\Reactions\Helper;

class Reaction extends Model
{
Expand All @@ -14,14 +15,11 @@ class Reaction extends Model
protected $table = 'reactions';

/**
* The attributes that are mass assignable.
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'type',
];
protected $guarded = [];

public function __construct(array $attributes = [])
{
Expand All @@ -46,8 +44,9 @@ public function reactable()
*/
public function reactBy()
{
$userModel = config('auth.providers.users.model');
$userModel = Helper::resolveReactsModel();
$userIdColumn = Helper::resolveReactsIdColumn();

return $this->belongsTo($userModel, 'user_id');
return $this->belongsTo($userModel, $userIdColumn);
}
}
25 changes: 8 additions & 17 deletions src/Traits/Reactable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Qirolab\Laravel\Reactions\Contracts\ReactsInterface;
use Qirolab\Laravel\Reactions\Exceptions\InvalidReactionUser;
use Qirolab\Laravel\Reactions\Helper;
use Qirolab\Laravel\Reactions\Models\Reaction;

trait Reactable
Expand All @@ -26,9 +27,9 @@ public function reactions()
*/
public function reactionsBy()
{
$userModel = $this->resolveUserModel();
$userModel = Helper::resolveReactsModel();

$userIds = $this->reactions->pluck('user_id');
$userIds = $this->reactions->pluck(Helper::resolveReactsIdColumn());

return $userModel::whereKey($userIds)->get();
}
Expand Down Expand Up @@ -136,7 +137,7 @@ public function reacted($user = null)
{
$user = $this->getUser($user);

return $this->reactions->where('user_id', $user->getKey())->first();
return $this->reactions->where(Helper::resolveReactsIdColumn(), $user->getKey())->first();
}

/**
Expand Down Expand Up @@ -196,15 +197,15 @@ public function scopeWhereReactedBy(Builder $query, $userId = null, $type = null
try {
$user = $this->getUser($userId);
} catch (InvalidReactionUser $e) {
if (! $user && ! $userId) {
if (!$user && !$userId) {
throw InvalidReactionUser::notDefined();
}
}

$userId = ($user) ? $user->getKey() : $userId;

return $query->whereHas('reactions', function ($innerQuery) use ($userId, $type) {
$innerQuery->where('user_id', $userId);
$innerQuery->where(Helper::resolveReactsIdColumn(), $userId);

if ($type) {
$innerQuery->where('type', $type);
Expand All @@ -222,28 +223,18 @@ public function scopeWhereReactedBy(Builder $query, $userId = null, $type = null
*/
private function getUser($user = null)
{
if (! $user && auth()->check()) {
if (!$user && auth()->check()) {
return auth()->user();
}

if ($user instanceof ReactsInterface) {
return $user;
}

if (! $user) {
if (!$user) {
throw InvalidReactionUser::notDefined();
}

throw InvalidReactionUser::invalidReactByUser();
}

/**
* Retrieve User's model class name.
*
* @return \Illuminate\Contracts\Auth\Authenticatable
*/
private function resolveUserModel()
{
return config('auth.providers.users.model');
}
}
17 changes: 9 additions & 8 deletions src/Traits/Reacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Qirolab\Laravel\Reactions\Contracts\ReactableInterface;
use Qirolab\Laravel\Reactions\Events\OnDeleteReaction;
use Qirolab\Laravel\Reactions\Events\OnReaction;
use Qirolab\Laravel\Reactions\Helper;
use Qirolab\Laravel\Reactions\Models\Reaction;

trait Reacts
Expand All @@ -19,10 +20,10 @@ trait Reacts
public function reactTo(ReactableInterface $reactable, $type)
{
$reaction = $reactable->reactions()->where([
'user_id' => $this->getKey(),
Helper::resolveReactsIdColumn() => $this->getKey(),
])->first();

if (! $reaction) {
if (!$reaction) {
return $this->storeReaction($reactable, $type);
}

Expand All @@ -44,10 +45,10 @@ public function reactTo(ReactableInterface $reactable, $type)
public function removeReactionFrom(ReactableInterface $reactable)
{
$reaction = $reactable->reactions()->where([
'user_id' => $this->getKey(),
Helper::resolveReactsIdColumn() => $this->getKey(),
])->first();

if (! $reaction) {
if (!$reaction) {
return;
}

Expand All @@ -64,10 +65,10 @@ public function removeReactionFrom(ReactableInterface $reactable)
public function toggleReactionOn(ReactableInterface $reactable, $type)
{
$reaction = $reactable->reactions()->where([
'user_id' => $this->getKey(),
Helper::resolveReactsIdColumn() => $this->getKey(),
])->first();

if (! $reaction) {
if (!$reaction) {
return $this->storeReaction($reactable, $type);
}

Expand Down Expand Up @@ -101,7 +102,7 @@ public function ReactedOn(ReactableInterface $reactable)
public function isReactedOn(ReactableInterface $reactable, $type = null)
{
$isReacted = $reactable->reactions()->where([
'user_id' => $this->getKey(),
Helper::resolveReactsIdColumn() => $this->getKey(),
]);

if ($type) {
Expand All @@ -123,7 +124,7 @@ public function isReactedOn(ReactableInterface $reactable, $type = null)
protected function storeReaction(ReactableInterface $reactable, $type)
{
$reaction = $reactable->reactions()->create([
'user_id' => $this->getKey(),
Helper::resolveReactsIdColumn() => $this->getKey(),
'type' => $type,
]);

Expand Down
28 changes: 28 additions & 0 deletions tests/Stubs/Models/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Qirolab\Tests\Laravel\Reactions\Stubs\Models;

use Illuminate\Database\Eloquent\Model;
use Qirolab\Laravel\Reactions\Contracts\ReactsInterface;
use Qirolab\Laravel\Reactions\Traits\Reacts;

class Profile extends Model implements ReactsInterface
{
use Reacts;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];
}
21 changes: 17 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\File;
use Orchestra\Testbench\TestCase as Orchestra;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\Article;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\Profile;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\User;

abstract class TestCase extends Orchestra
Expand Down Expand Up @@ -50,7 +51,7 @@ protected function publishPackageMigrations()
*/
protected function destroyPackageMigrations()
{
File::cleanDirectory(__DIR__.'/../vendor/orchestra/testbench-core/laravel/database/migrations');
File::cleanDirectory(__DIR__ . '/../vendor/orchestra/testbench-core/laravel/database/migrations');
}

/**
Expand Down Expand Up @@ -83,9 +84,9 @@ protected function getPackageProviders($app)
*/
protected function setUpDatabase()
{
include_once __DIR__.'/../migrations/2018_07_10_000000_create_reactions_table.php';
include_once __DIR__.'/database/migrations/2018_07_10_000000_create_users_table.php';
include_once __DIR__.'/database/migrations/2018_07_11_000000_create_articles_table.php';
include_once __DIR__ . '/../migrations/2018_07_10_000000_create_reactions_table.php';
include_once __DIR__ . '/database/migrations/2018_07_10_000000_create_users_table.php';
include_once __DIR__ . '/database/migrations/2018_07_11_000000_create_articles_table.php';

(new \CreateReactionsTable())->up();
(new \CreateUsersTable())->up();
Expand Down Expand Up @@ -151,4 +152,16 @@ public function createUser($attributes = [], $amount = null)
$amount
);
}

public function createProfile($attributes = [], $amount = null)
{
return $this->factory(
Profile::class,
array_merge(
['name' => $this->faker()->name],
$attributes
),
$amount
);
}
}
7 changes: 4 additions & 3 deletions tests/Unit/ReactableReactionEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Event;
use Qirolab\Laravel\Reactions\Events\OnDeleteReaction;
use Qirolab\Laravel\Reactions\Events\OnReaction;
use Qirolab\Laravel\Reactions\Helper;
use Qirolab\Tests\Laravel\Reactions\TestCase;

class ReactableReactionEventTest extends TestCase
Expand Down Expand Up @@ -41,7 +42,7 @@ public function it_can_fire_model_was_reacted_event_on_toggle_reaction()
public function it_can_fire_reaction_deleted_and_model_was_reacted_event_on_change_reaction()
{
$this->article->reactions()->create([
'user_id' => $this->user->getKey(),
Helper::resolveReactsIdColumn() => $this->user->getKey(),
'type' => 'like',
]);

Expand All @@ -55,7 +56,7 @@ public function it_can_fire_reaction_deleted_and_model_was_reacted_event_on_chan
public function it_can_fire_reaction_deleted_and_model_was_reacted_event_on_change_reaction_via_toggle()
{
$this->article->reactions()->create([
'user_id' => $this->user->getKey(),
Helper::resolveReactsIdColumn() => $this->user->getKey(),
'type' => 'like',
]);

Expand All @@ -68,7 +69,7 @@ public function it_can_fire_reaction_deleted_and_model_was_reacted_event_on_chan
public function it_can_fire_reaction_was_deleted_event()
{
$this->article->reactions()->create([
'user_id' => $this->user->getKey(),
Helper::resolveReactsIdColumn() => $this->user->getKey(),
'type' => 'like',
]);

Expand Down
Loading

0 comments on commit 8ddf93f

Please sign in to comment.