Permalink
Browse files

Episode 25

  • Loading branch information...
JeffreyWay committed Apr 21, 2017
1 parent e4bcdba commit 0246376100b1f6eaf7ac1f2617af3c2e5dd93d1d
@@ -0,0 +1,25 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Activity extends Model
{
/**
* Don't auto-apply mass assignment protection.
*
* @var array
*/
protected $guarded = [];
/**
* Fetch the associated subject for the activity.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function subject()
{
return $this->morphTo();
}
}
@@ -0,0 +1,67 @@
<?php
namespace App;
trait RecordsActivity
{
/**
* Boot the trait.
*/
protected static function bootRecordsActivity()
{
if (auth()->guest()) return;
foreach (static::getActivitiesToRecord() as $event) {
static::$event(function ($model) use ($event) {
$model->recordActivity($event);
});
}
}
/**
* Fetch all model events that require activity recording.
*
* @return array
*/
protected static function getActivitiesToRecord()
{
return ['created'];
}
/**
* Record new activity for the model.
*
* @param string $event
*/
protected function recordActivity($event)
{
$this->activity()->create([
'user_id' => auth()->id(),
'type' => $this->getActivityType($event)
]);
}
/**
* Fetch the activity relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function activity()
{
return $this->morphMany('App\Activity', 'subject');
}
/**
* Determine the activity type.
*
* @param string $event
* @return string
*/
protected function getActivityType($event)
{
$type = strtolower((new \ReflectionClass($this))->getShortName());
return "{$event}_{$type}";
}
}
@@ -6,7 +6,7 @@
class Reply extends Model
{
use Favoritable;
use Favoritable, RecordsActivity;
/**
* Don't auto-apply mass assignment protection.
@@ -8,6 +8,8 @@
class Thread extends Model
{
use RecordsActivity;
/**
* Don't auto-apply mass assignment protection.
*
@@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateActivitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->unsignedInteger('subject_id')->index();
$table->string('subject_type', 50);
$table->string('type', 50);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activities');
}
}
@@ -0,0 +1,41 @@
<?php
namespace Tests\Feature;
use App\Activity;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ActivityTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function it_records_activity_when_a_thread_is_created()
{
$this->signIn();
$thread = create('App\Thread');
$this->assertDatabaseHas('activities', [
'type' => 'created_thread',
'user_id' => auth()->id(),
'subject_id' => $thread->id,
'subject_type' => 'App\Thread'
]);
$activity = Activity::first();
$this->assertEquals($activity->subject->id, $thread->id);
}
/** @test */
function it_records_activity_when_a_reply_is_created()
{
$this->signIn();
$reply = create('App\Reply');
$this->assertEquals(2, Activity::count());
}
}

0 comments on commit 0246376

Please sign in to comment.