Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
hkp22 committed Apr 1, 2019
2 parents 4acfb4f + 2f762be commit 4936e3f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 22 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@

All notable changes to `laravel-reactions` will be documented in this file

## 2.2.0 - 2019-04-01

### Fixed
- Changed output for `reactionSummary()` and 'reaction_summary' method in reactable model.
```php
$article->reactionSummary();
$article->reaction_summary;

// example
$article->reaction_summary->toArray();
// output
/*
[
"like" => 5,
"dislike" => 2,
"clap" => 4,
"hooray" => 1
]
*/
```
- Fixed `toggleReaction()` function. Now it will return `Qirolab\Laravel\Reactions\Models\Reaction` object.

### Added
- new `reacted()` method added on in reactable model.
```php
$article->reacted(); // current login user
```
- new `reactedOn($article)` method added to reacts model.
```php
$user->reactedOn($article);
```

## 2.1.0 - 2019-03-20

- Laravel 5.8 compatibility
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ $article->reaction_summary->toArray();
// output
/*
[
['type' => 'clap', 'count' => '4'],
['type' => 'dislike', 'count' => '2'],
['type' => 'hooray', 'count' => '1'],
['type' => 'like', 'count' => '5']
"like" => 5,
"dislike" => 2,
"clap" => 4,
"hooray" => 1
]
*/
```
Expand All @@ -137,6 +137,16 @@ Article::whereReactedBy($user)->get();
Article::whereReactedBy($user->id)->get();
```

### Reaction on Model
```php
// It will return the Reaction object that is reacted by given user.
$article->reacted($user);
$article->reacted(); // current login user
$article->reacted; // current login user

$user->reactedOn($article);
```

### Events

On each reaction added `\Qirolab\Laravel\Reactions\Events\OnReaction` event is fired.
Expand Down
35 changes: 28 additions & 7 deletions src/Traits/Reactable.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ public function getReactionsByAttribute()
*/
public function reactionSummary()
{
return $this->reactions()
->getQuery()
->select('type', \DB::raw('count(*) as count'))
->groupBy('type')
->get();
return $this->reactions->groupBy('type')->map(function ($val) {
return $val->count();
});
}

/**
Expand Down Expand Up @@ -107,17 +105,40 @@ public function removeReaction($user = null)
*
* @param mixed $reactionType
* @param mixed $user
* @return void
* @return void|Reaction
*/
public function toggleReaction($reactionType, $user = null)
{
$user = $this->getUser($user);

if ($user) {
$user->toggleReactionOn($this, $reactionType);
return $user->toggleReactionOn($this, $reactionType);
}
}

/**
* Reaction on reactable model by user.
*
* @param mixed $user
* @return Reaction
*/
public function reacted($user = null)
{
$user = $this->getUser($user);

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

/**
* Reaction on reactable model by user.
*
* @return Reaction
*/
public function getReactedAttribute()
{
return $this->reacted();
}

/**
* Check is reacted by user.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/Reacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public function toggleReactionOn(ReactableInterface $reactable, $type)
return $this->storeReaction($reactable, $type);
}

/**
* Reaction on reactable model.
*
* @param ReactableInterface $reactable
* @return Reaction
*/
public function ReactedOn(ReactableInterface $reactable)
{
return $reactable->reacted($this);
}

/**
* Check is reacted on reactable model.
*
Expand Down
53 changes: 43 additions & 10 deletions tests/Unit/ReactableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Qirolab\Tests\Laravel\Reactions\Unit;

use Qirolab\Tests\Laravel\Reactions\TestCase;
use Qirolab\Laravel\Reactions\Models\Reaction;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\User;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\Article;

Expand Down Expand Up @@ -122,12 +123,14 @@ public function it_can_toggle_reaction_type_by_current_user()

$this->actingAs($user);

$article->toggleReaction('like');
$reaction = $article->toggleReaction('like');
$this->assertInstanceOf(Reaction::class, $reaction);
$this->assertEquals(1, $article->reactions()->count());
$this->assertEquals($user->id, $article->reactions()->first()->user_id);
$this->assertEquals('like', $article->reactions()->first()->type);

$article->toggleReaction('clap');
$reaction = $article->toggleReaction('clap');
$this->assertInstanceOf(Reaction::class, $reaction);
$this->assertEquals(1, $article->reactions()->count());
$this->assertEquals($user->id, $article->reactions()->first()->user_id);
$this->assertEquals('clap', $article->reactions()->first()->type);
Expand Down Expand Up @@ -266,10 +269,10 @@ public function it_can_has_reaction_summery()
$summaryAsArray = $article->reactionSummary()->toArray();

$this->assertEquals([
['type' => 'clap', 'count' => '4'],
['type' => 'dislike', 'count' => '2'],
['type' => 'hooray', 'count' => '1'],
['type' => 'like', 'count' => '5'],
"like" => 5,
"dislike" => 2,
"clap" => 4,
"hooray" => 1
], $summaryAsArray);
}

Expand Down Expand Up @@ -301,10 +304,10 @@ public function it_can_has_reaction_summery_attribute()
$summaryAsArray = $article->reaction_summary->toArray();

$this->assertEquals([
['type' => 'clap', 'count' => '4'],
['type' => 'dislike', 'count' => '2'],
['type' => 'hooray', 'count' => '1'],
['type' => 'like', 'count' => '5'],
"like" => 5,
"dislike" => 2,
"clap" => 4,
"hooray" => 1
], $summaryAsArray);
}

Expand Down Expand Up @@ -341,4 +344,34 @@ public function it_can_has_collection_of_reactions_by_users_using_attribute()

$this->assertEquals($users->toArray(), $article->reactions_by->toArray());
}

/** @test **/
public function it_can_has_reacted_reactions_by_current_login_user()
{
$article = factory(Article::class)->create();

$user = factory(User::class)->create();

$this->actingAs($user);

$article->react('like');

$this->assertInstanceOf(Reaction::class, $article->reacted());
$this->assertInstanceOf(Reaction::class, $article->reacted);
$this->assertEquals('like', $article->reacted()->type);
$this->assertEquals('like', $article->reacted->type);
}

/** @test **/
public function it_can_has_reacted_reactions_by_given_user()
{
$article = factory(Article::class)->create();

$user = factory(User::class)->create();

$article->react('like', $user);

$this->assertInstanceOf(Reaction::class, $article->reacted($user));
$this->assertEquals('like', $article->reacted($user)->type);
}
}
16 changes: 15 additions & 1 deletion tests/Unit/ReactsModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Qirolab\Tests\Laravel\Reactions\Unit;

use Qirolab\Tests\Laravel\Reactions\TestCase;
use Qirolab\Laravel\Reactions\Models\Reaction;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\User;
use Qirolab\Tests\Laravel\Reactions\Stubs\Models\Article;

Expand Down Expand Up @@ -146,7 +147,7 @@ public function it_can_remove_reaction_with_toggle()
}

/** @test */
public function it_can_check_if_liker_has_liked_likeable()
public function it_can_check_if_reacted_on_reactable_model()
{
$user1 = factory(User::class)->create();

Expand All @@ -160,4 +161,17 @@ public function it_can_check_if_liker_has_liked_likeable()

$this->assertFalse($user2->isReactedOn($article));
}

/** @test **/
public function it_can_have_reacted_reaction_on_reactable_model()
{
$user = factory(User::class)->create();

$article = factory(Article::class)->create();

$user->reactTo($article, 'like');

$this->assertInstanceOf(Reaction::class, $user->reactedOn($article));
$this->assertEquals('like', $user->reactedOn($article)->type);
}
}

0 comments on commit 4936e3f

Please sign in to comment.