Skip to content
Permalink
Browse files

Episode 25 Complete

  • Loading branch information...
JeffreyWay committed Feb 6, 2019
1 parent c2bc749 commit baa2bd53a1456686e75f4c24b523df7c7ef0da81
@@ -12,4 +12,9 @@ class Activity extends Model
* @var array
*/
protected $guarded = [];
public function subject()
{
return $this->morphTo();
}
}
@@ -14,7 +14,7 @@ class TaskObserver
*/
public function created(Task $task)
{
$task->project->recordActivity('created_task');
$task->recordActivity('created_task');
}
/**
@@ -25,6 +25,6 @@ public function created(Task $task)
*/
public function deleted(Task $task)
{
$task->project->recordActivity('deleted_task');
$task->recordActivity('deleted_task');
}
}
@@ -36,7 +36,7 @@ public function complete()
{
$this->update(['completed' => true]);
$this->project->recordActivity('completed_task');
$this->recordActivity('completed_task');
}
/**
@@ -46,7 +46,7 @@ public function incomplete()
{
$this->update(['completed' => false]);
$this->project->recordActivity('incompleted_task');
$this->recordActivity('incompleted_task');
}
/**
@@ -68,4 +68,27 @@ public function path()
{
return "/projects/{$this->project->id}/tasks/{$this->id}";
}
/**
* Record activity for a project.
*
* @param string $description
*/
public function recordActivity($description)
{
$this->activity()->create([
'project_id' => $this->project_id,
'description' => $description
]);
}
/**
* The activity feed for the project.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function activity()
{
return $this->morphMany(Activity::class, 'subject')->latest();
}
}
@@ -16,6 +16,7 @@ public function up()
Schema::create('activities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('project_id');
$table->nullableMorphs('subject');
$table->string('description');
$table->timestamps();
@@ -1 +1 @@
You completed a task
You completed "{{ $activity->subject->body }}"
@@ -1 +1 @@
You created a task
You created "{{ $activity->subject->body }}"
@@ -0,0 +1 @@
You incompleted "{{ $activity->subject->body }}"
@@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\Task;
use Facades\Tests\Setup\ProjectFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@@ -38,7 +39,12 @@ function creating_a_new_task()
$project->addTask('Some task');
$this->assertCount(2, $project->activity);
$this->assertEquals('created_task', $project->activity->last()->description);
tap($project->activity->last(), function ($activity) {
$this->assertEquals('created_task', $activity->description);
$this->assertInstanceOf(Task::class, $activity->subject);
$this->assertEquals('Some task', $activity->subject->body);
});
}
/** @test */
@@ -53,7 +59,11 @@ function completing_a_task()
]);
$this->assertCount(3, $project->activity);
$this->assertEquals('completed_task', $project->activity->last()->description);
tap($project->activity->last(), function ($activity) {
$this->assertEquals('completed_task', $activity->description);
$this->assertInstanceOf(Task::class, $activity->subject);
});
}
/** @test */

0 comments on commit baa2bd5

Please sign in to comment.
You can’t perform that action at this time.