Skip to content
Permalink
Browse files

Episode 21 Complete

  • Loading branch information...
JeffreyWay committed Jan 25, 2019
1 parent eca2a55 commit 1ab3d0663eb7188396810c804405c2a15f8d5389
@@ -39,10 +39,11 @@ public function update(Project $project, Task $task)
request()->validate(['body' => 'required']);
$task->update([
'body' => request('body'),
'completed' => request()->has('completed')
]);
$task->update(['body' => request('body')]);
if (request()->has('completed')) {
$task->complete();
}
return redirect($project->path());
}
@@ -2,7 +2,6 @@
namespace App\Observers;
use App\Activity;
use App\Project;
class ProjectObserver
@@ -15,7 +14,7 @@ class ProjectObserver
*/
public function created(Project $project)
{
$this->recordActivity('created', $project);
$project->recordActivity('created');
}
/**
@@ -26,20 +25,6 @@ public function created(Project $project)
*/
public function updated(Project $project)
{
$this->recordActivity('updated', $project);
}
/**
* Record activity for a project.
*
* @param string $type
* @param \App\Project $project
*/
protected function recordActivity($type, $project)
{
Activity::create([
'project_id' => $project->id,
'description' => $type
]);
$project->recordActivity('updated');
}
}
@@ -54,6 +54,19 @@ public function addTask($body)
return $this->tasks()->create(compact('body'));
}
/**
* Record activity for a project.
*
* @param string $type
*/
public function recordActivity($type)
{
Activity::create([
'project_id' => $this->id,
'description' => $type
]);
}
/**
* The activity feed for the project.
*
@@ -20,6 +20,37 @@ class Task extends Model
*/
protected $touches = ['project'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'completed' => 'boolean'
];
/**
* Boot the model.
*/
protected static function boot()
{
parent::boot();
static::created(function ($task) {
$task->project->recordActivity('created_task');
});
}
/**
* Mark the task as complete.
*/
public function complete()
{
$this->update(['completed' => true]);
$this->project->recordActivity('completed_task');
}
/**
* Get the owning project.
*
@@ -5,6 +5,7 @@
$factory->define(App\Task::class, function (Faker $faker) {
return [
'body' => $faker->sentence,
'project_id' => factory(\App\Project::class)
'project_id' => factory(\App\Project::class),
'completed' => false
];
});
@@ -11,7 +11,7 @@ class ActivityFeedTest extends TestCase
use RefreshDatabase;
/** @test */
function creating_a_project_generates_activity()
function creating_a_project_records_activity()
{
$project = ProjectFactory::create();
@@ -20,14 +20,39 @@ function creating_a_project_generates_activity()
}
/** @test */
function updating_a_project_generates_activity()
function updating_a_project_records_activity()
{
$project = ProjectFactory::create();
$project->update(['title' => 'Changed']);
$this->assertCount(2, $project->activity);
$this->assertEquals('updated', $project->activity->last()->description);
}
/** @test */
function creating_a_new_task_records_project_activity()
{
$project = ProjectFactory::create();
$project->addTask('Some task');
$this->assertCount(2, $project->activity);
$this->assertEquals('created_task', $project->activity->last()->description);
}
/** @test */
function completing_a_new_task_records_project_activity()
{
$project = ProjectFactory::withTasks(1)->create();
$this->actingAs($project->owner)
->patch($project->tasks[0]->path(), [
'body' => 'foobar',
'completed' => true
]);
$this->assertCount(3, $project->activity);
$this->assertEquals('completed_task', $project->activity->last()->description);
}
}
@@ -26,4 +26,16 @@ function it_has_a_path()
$this->assertEquals("/projects/{$task->project->id}/tasks/{$task->id}", $task->path());
}
/** @test */
function it_can_be_completed()
{
$task = factory(Task::class)->create();
$this->assertFalse($task->completed);
$task->complete();
$this->assertTrue($task->fresh()->completed);
}
}

0 comments on commit 1ab3d06

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