Skip to content

Commit

Permalink
Fixed #5
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 11, 2022
1 parent 8573e50 commit 4809d9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ composer.lock
.subsplit
.php_cs.cache
.phpunit.result.cache
.php-cs-fixer.cache
4 changes: 2 additions & 2 deletions src/Traits/Votable.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function appendsVotesAttributes($attributes = ['total_votes', 'total_upvo

public function getTotalVotesAttribute()
{
return abs($this->attributes['total_votes'] ?? $this->totalVotes());
return $this->attributes['total_votes'] ?? $this->totalVotes();
}

public function getTotalUpvotesAttribute()
Expand All @@ -89,7 +89,7 @@ public function getTotalDownvotesAttribute()

public function totalVotes()
{
return abs($this->votes()->sum('votes'));
return $this->votes()->sum('votes');
}

public function totalUpvotes()
Expand Down
13 changes: 13 additions & 0 deletions tests/VoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public function test_votable_can_get_total_votes()
});

$this->assertCount(1, $sqls);


// -2 = -1 + -1
$user1->vote($idea, -1); // downvote
$user2->upvote($idea); // upvote
$user3->downvote($idea); // downvote
$user4->vote($idea, -1); // downvote

$ideas = Idea::withVotesAttributes()->get()->toArray();

$this->assertSame(-2, $ideas[0]['total_votes']);
$this->assertSame(1, $ideas[0]['total_upvotes']);
$this->assertSame(3, $ideas[0]['total_downvotes']);
}

public function test_voter_can_attach_vote_status_to_votable_collection()
Expand Down

0 comments on commit 4809d9e

Please sign in to comment.