Skip to content

Commit

Permalink
added subject relation on reputation, closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Mar 17, 2019
1 parent 4aa42a2 commit 747986c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ foreach($user->reputations as $reputation) {
}
```

If you want to get all the points given on a `subject` model. You should define a `morphMany` relations. For example on post model.

```php
/**
* Get all the post's reputation.
*/
public function reputations()
{
return $this->morphMany('QCod\Gamify\Reputation', 'subject');
}
```

Now you can get all the reputation given on a `Post` using `$post->reputations`.

### Configure a Point Type

#### Point payee
Expand Down
10 changes: 10 additions & 0 deletions src/Reputation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public function payee()
return $this->belongsTo(config('gamify.payee_model'), 'payee_id');
}

/**
* Get the owning subject model
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function subject()
{
return $this->morphTo();
}

/**
* Undo last point
*
Expand Down
5 changes: 5 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function bestReply()
{
return $this->hasOne(Reply::class, 'id', 'best_reply_id');
}

public function reputations()
{
return $this->morphMany('QCod\Gamify\Reputation', 'subject');
}
}
20 changes: 20 additions & 0 deletions tests/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ public function it_gives_point_to_a_user()
]);
}

/**
* it can access a reputation payee and subject
*
* @test
*/
public function it_can_access_a_reputation_payee_and_subject()
{
$user = $this->createUser();
$post = $this->createPost(['user_id' => $user->id]);

$user->givePoint(new FakeCreatePostPoint($post));

$point = $user->reputations()->first();

$this->assertEquals($user->id, $point->payee->id);
$this->assertEquals($post->id, $point->subject->id);

$this->assertEquals('FakeCreatePostPoint', $post->reputations->first()->name);
}

/**
* it only adds unique point reward if property is set on point type
*
Expand Down

0 comments on commit 747986c

Please sign in to comment.