Skip to content
Permalink
Browse files

Episode 20 Complete

  • Loading branch information...
JeffreyWay committed Jan 24, 2019
1 parent 816d49a commit 1a0bb81b22606ce386eb9e7a33fdb02e74119ae3
@@ -0,0 +1,15 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Activity extends Model
{
/**
* Attributes to guard against mass assignment.
*
* @var array
*/
protected $guarded = [];
}
@@ -0,0 +1,39 @@
<?php
namespace App\Observers;
use App\Activity;
use App\Project;
class ProjectObserver
{
/**
* Handle the project "created" event.
*
* @param \App\Project $project
* @return void
*/
public function created(Project $project)
{
$this->recordActivity('created', $project);
}
/**
* Handle the project "updated" event.
*
* @param \App\Project $project
* @return void
*/
public function updated(Project $project)
{
$this->recordActivity('updated', $project);
}
protected function recordActivity($type, $project)
{
Activity::create([
'project_id' => $project->id,
'description' => $type
]);
}
}
@@ -53,4 +53,14 @@ public function addTask($body)
{
return $this->tasks()->create(compact('body'));
}
/**
* The activity feed for the project.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activity()
{
return $this->hasMany(Activity::class);
}
}
@@ -2,6 +2,8 @@
namespace App\Providers;
use App\Observers\ProjectObserver;
use App\Project;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -13,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
/**
@@ -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('project_id');
$table->string('description');
$table->timestamps();
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activities');
}
}
@@ -0,0 +1,33 @@
<?php
namespace Tests\Feature;
use Facades\Tests\Setup\ProjectFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ActivityFeedTest extends TestCase
{
use RefreshDatabase;
/** @test */
function creating_a_project_generates_activity()
{
$project = ProjectFactory::create();
$this->assertCount(1, $project->activity);
$this->assertEquals('created', $project->activity[0]->description);
}
/** @test */
function updating_a_project_generates_activity()
{
$project = ProjectFactory::create();
$project->update(['title' => 'Changed']);
$this->assertCount(2, $project->activity);
$this->assertEquals('updated', $project->activity->last()->description);
}
}

0 comments on commit 1a0bb81

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