Skip to content

Commit

Permalink
Episode 21 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Jan 25, 2019
1 parent eca2a55 commit 1ab3d06
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 24 deletions.
9 changes: 5 additions & 4 deletions app/Http/Controllers/ProjectTasksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
19 changes: 2 additions & 17 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Observers;

use App\Activity;
use App\Project;

class ProjectObserver
Expand All @@ -15,7 +14,7 @@ class ProjectObserver
*/
public function created(Project $project)
{
$this->recordActivity('created', $project);
$project->recordActivity('created');
}

/**
Expand All @@ -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');
}
}
13 changes: 13 additions & 0 deletions app/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
31 changes: 31 additions & 0 deletions app/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion database/factories/TaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
});
29 changes: 27 additions & 2 deletions tests/Feature/ActivityFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
}
12 changes: 12 additions & 0 deletions tests/Unit/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.