Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(activity): store when created items
- Loading branch information
Andrey Groza
committed
Oct 13, 2019
1 parent
68a5a0d
commit ce2105e
Showing
8 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Activity extends Model | ||
{ | ||
protected $fillable = [ | ||
'user_id', | ||
'type', | ||
]; | ||
|
||
public function subject() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use App\Activity; | ||
use ReflectionClass; | ||
|
||
trait RecordActivity | ||
{ | ||
protected static function bootRecordActivity() | ||
{ | ||
foreach (static::getEventsToRecord() as $event) { | ||
static::$event(function ($model) use ($event) { | ||
//TODO Проверка есть ли уже активность по этой главе | ||
$model->recordActivity($event); | ||
}); | ||
} | ||
} | ||
|
||
protected static function getEventsToRecord() | ||
{ | ||
return ['created']; | ||
} | ||
|
||
public function recordActivity($event) | ||
{ | ||
$this->activity()->create([ | ||
'user_id' => auth()->id(), | ||
'type' => "{$event}_{$this->getTypeName()}", | ||
]); | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws \ReflectionException | ||
*/ | ||
protected function getTypeName() | ||
{ | ||
try { | ||
return strtolower((new ReflectionClass($this))->getShortName()); | ||
} catch (\ReflectionException $e) { | ||
throw $e; | ||
} | ||
} | ||
|
||
public function activity() | ||
{ | ||
return $this->morphMany(Activity::class, 'subject'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
database/migrations/2019_10_12_084709_create_activities_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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->bigIncrements('id'); | ||
$table->unsignedInteger('user_id')->index(); | ||
$table->unsignedInteger('subject_id')->index(); | ||
$table->string('subject_type', 50); | ||
$table->string('type', 50); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('activities'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use App\ReadChapter; | ||
use App\User; | ||
use Tests\TestCase; | ||
|
||
class ActivityTest extends TestCase | ||
{ | ||
public function testCreateActivityWhenUserCompleteChapter() | ||
{ | ||
$user = factory(User::class)->create(); | ||
|
||
$this->actingAs($user); | ||
|
||
$readChapter = factory(ReadChapter::class)->create(); | ||
|
||
$this->assertDatabaseHas('activities', [ | ||
'user_id' => auth()->id(), | ||
'subject_id' => $readChapter->id, | ||
'subject_type' => ReadChapter::class, | ||
'type' => 'created_readchapter', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters