Skip to content

Commit

Permalink
Episode 25 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Feb 6, 2019
1 parent c2bc749 commit baa2bd5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ class Activity extends Model
* @var array
*/
protected $guarded = [];

public function subject()
{
return $this->morphTo();
}
}
4 changes: 2 additions & 2 deletions app/Observers/TaskObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TaskObserver
*/
public function created(Task $task)
{
$task->project->recordActivity('created_task');
$task->recordActivity('created_task');
}

/**
Expand All @@ -25,6 +25,6 @@ public function created(Task $task)
*/
public function deleted(Task $task)
{
$task->project->recordActivity('deleted_task');
$task->recordActivity('deleted_task');
}
}
27 changes: 25 additions & 2 deletions app/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function complete()
{
$this->update(['completed' => true]);

$this->project->recordActivity('completed_task');
$this->recordActivity('completed_task');
}

/**
Expand All @@ -46,7 +46,7 @@ public function incomplete()
{
$this->update(['completed' => false]);

$this->project->recordActivity('incompleted_task');
$this->recordActivity('incompleted_task');
}

/**
Expand All @@ -68,4 +68,27 @@ public function path()
{
return "/projects/{$this->project->id}/tasks/{$this->id}";
}

/**
* Record activity for a project.

This comment has been minimized.

Copy link
@PresKhaled

PresKhaled Jul 2, 2020

Record activity for a task

*
* @param string $description
*/
public function recordActivity($description)
{
$this->activity()->create([
'project_id' => $this->project_id,
'description' => $description
]);
}

/**
* The activity feed for the project.

This comment has been minimized.

Copy link
@PresKhaled

PresKhaled Jul 2, 2020

The activity feed for the task

*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function activity()
{
return $this->morphMany(Activity::class, 'subject')->latest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion resources/views/projects/activity/completed_task.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
You completed a task
You completed "{{ $activity->subject->body }}"
2 changes: 1 addition & 1 deletion resources/views/projects/activity/created_task.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
You created a task
You created "{{ $activity->subject->body }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You incompleted "{{ $activity->subject->body }}"
14 changes: 12 additions & 2 deletions tests/Feature/TriggerActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Task;
use Facades\Tests\Setup\ProjectFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down

0 comments on commit baa2bd5

Please sign in to comment.