Skip to content

Commit

Permalink
added hasParticipant() method to Thread model
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgmyr committed Jun 16, 2015
1 parent bf6d8c1 commit 70e969e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Cmgmyr/Messenger/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ public function participantsString($userId=null, $columns=['name'])
return implode(', ', $userNames);
}

/**
* Checks to see if a user is a current participant of the thread
*
* @param $userId
* @return bool
*/
public function hasParticipant($userId)
{
$participants = $this->participants()->where('user_id', '=', $userId);
if ($participants->count() > 0) {
return true;
}

return false;
}

/**
* Generates a select string used in participantsString()
*
Expand Down
15 changes: 15 additions & 0 deletions tests/EloquentThreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,19 @@ public function it_should_get_participants_string()
$string = $thread->participantsString(1, ['email']);
$this->assertEquals("[email protected], [email protected]", $string);
}

/** @test */
public function it_should_check_users_and_participants()
{
$thread = $this->faktory->create('thread');

$participant_1 = $this->faktory->build('participant');
$participant_2 = $this->faktory->build('participant', ['user_id' => 2]);

$thread->participants()->saveMany([$participant_1, $participant_2]);

$this->assertTrue($thread->hasParticipant(1));
$this->assertTrue($thread->hasParticipant(2));
$this->assertFalse($thread->hasParticipant(3));
}
}

0 comments on commit 70e969e

Please sign in to comment.