Skip to content

Commit

Permalink
Episode 28 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Feb 14, 2019
1 parent ee70299 commit ad46117
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 5 deletions.
10 changes: 10 additions & 0 deletions app/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function subject()
{
return $this->morphTo();
}

/**
* Get the user who triggered the activity.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
1 change: 1 addition & 0 deletions app/RecordsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected static function recordableEvents()
public function recordActivity($description)
{
$this->activity()->create([
'user_id' => ($this->project ?? $this)->owner->id,
'description' => $description,
'changes' => $this->activityChanges(),
'project_id' => class_basename($this) === 'Project' ? $this->id : $this->project_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public function up()
{
Schema::create('activities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('project_id');
$table->nullableMorphs('subject');
$table->string('description');
$table->text('changes')->nullable();
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});
}
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 "{{ $activity->subject->body }}"
{{ $activity->user->name }} completed "{{ $activity->subject->body }}"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
You created the project
{{ $activity->user->name }} created the project
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 "{{ $activity->subject->body }}"
{{ $activity->user->name }} created "{{ $activity->subject->body }}"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
You incompleted "{{ $activity->subject->body }}"
{{ $activity->user->name }} incompleted "{{ $activity->subject->body }}"
6 changes: 5 additions & 1 deletion resources/views/projects/activity/updated_project.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
You updated the project
@if (count($activity->changes['after']) == 1)
{{ $activity->user->name }} updated the {{ key($activity->changes['after']) }} of the project
@else
{{ $activity->user->name }} updated the project
@endif
25 changes: 25 additions & 0 deletions tests/Unit/ActivityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Unit;

use App\Activity;
use App\Project;
use App\User;
use Facades\Tests\Setup\ProjectFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ActivityTest extends TestCase
{
use RefreshDatabase;

/** @test */
function it_has_a_user()
{
$user = $this->signIn();

$project = ProjectFactory::ownedBy($user)->create();

$this->assertEquals($user->id, $project->activity->first()->user->id);
}
}

0 comments on commit ad46117

Please sign in to comment.