Skip to content

Commit 747986c

Browse files
committed
added subject relation on reputation, closes #7
1 parent 4aa42a2 commit 747986c

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ foreach($user->reputations as $reputation) {
172172
}
173173
```
174174

175+
If you want to get all the points given on a `subject` model. You should define a `morphMany` relations. For example on post model.
176+
177+
```php
178+
/**
179+
* Get all the post's reputation.
180+
*/
181+
public function reputations()
182+
{
183+
return $this->morphMany('QCod\Gamify\Reputation', 'subject');
184+
}
185+
```
186+
187+
Now you can get all the reputation given on a `Post` using `$post->reputations`.
188+
175189
### Configure a Point Type
176190

177191
#### Point payee

src/Reputation.php

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public function payee()
1818
return $this->belongsTo(config('gamify.payee_model'), 'payee_id');
1919
}
2020

21+
/**
22+
* Get the owning subject model
23+
*
24+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
25+
*/
26+
public function subject()
27+
{
28+
return $this->morphTo();
29+
}
30+
2131
/**
2232
* Undo last point
2333
*

tests/Models/Post.php

+5
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public function bestReply()
2525
{
2626
return $this->hasOne(Reply::class, 'id', 'best_reply_id');
2727
}
28+
29+
public function reputations()
30+
{
31+
return $this->morphMany('QCod\Gamify\Reputation', 'subject');
32+
}
2833
}

tests/PointTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ public function it_gives_point_to_a_user()
6969
]);
7070
}
7171

72+
/**
73+
* it can access a reputation payee and subject
74+
*
75+
* @test
76+
*/
77+
public function it_can_access_a_reputation_payee_and_subject()
78+
{
79+
$user = $this->createUser();
80+
$post = $this->createPost(['user_id' => $user->id]);
81+
82+
$user->givePoint(new FakeCreatePostPoint($post));
83+
84+
$point = $user->reputations()->first();
85+
86+
$this->assertEquals($user->id, $point->payee->id);
87+
$this->assertEquals($post->id, $point->subject->id);
88+
89+
$this->assertEquals('FakeCreatePostPoint', $post->reputations->first()->name);
90+
}
91+
7292
/**
7393
* it only adds unique point reward if property is set on point type
7494
*

0 commit comments

Comments
 (0)