Skip to content

Commit

Permalink
Add causer and subject type scopes to Activity model
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Postma committed Apr 16, 2024
1 parent 76de67c commit ce607a9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,25 @@ public function scopeCausedBy(Builder $query, Model $causer): Builder
->where('causer_id', $causer->getKey());
}

public function scopeCausedByType(Builder $query, string|Model $type): Builder
{
$type = is_string($type) ? $type : $type->getMorphClass();
return $query->where('causer_type', $type);
}

public function scopeForSubject(Builder $query, Model $subject): Builder
{
return $query
->where('subject_type', $subject->getMorphClass())
->where('subject_id', $subject->getKey());
}

public function scopeForSubjectType(Builder $query, string|Model $type): Builder
{
$type = is_string($type) ? $type : $type->getMorphClass();
return $query->where('subject_type', $type);
}

public function scopeForEvent(Builder $query, string $event): Builder
{
return $query->where('event', $event);
Expand Down
70 changes: 70 additions & 0 deletions tests/ActivityModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use Illuminate\Database\Eloquent\Relations\Relation;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\Admin;
use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Test\Models\Post;
use Spatie\Activitylog\Test\Models\User;

beforeEach(function () {
Expand Down Expand Up @@ -55,6 +57,40 @@
expect($activities->first()->description)->toEqual('Foo');
});

it('provides a scope to get log items for a specific causer class of given model', function () {
$subject = Article::first();
$causer = User::first();

activity()->on($subject)->by($causer)->log('Foo');
activity()->on($subject)->by(Admin::create([
'name' => 'An Admin User',
]))->log('Bar');

$activities = Activity::causedByType($causer)->get();

expect($activities)->toHaveCount(1);
expect($activities->first()->causer_id)->toEqual($causer->getKey());
expect($activities->first()->causer_type)->toEqual(get_class($causer));
expect($activities->first()->description)->toEqual('Foo');
});

it('provides a scope to get log items for a specific causer class of given class name', function () {
$subject = Article::first();
$causer = User::first();

activity()->on($subject)->by($causer)->log('Foo');
activity()->on($subject)->by(Admin::create([
'name' => 'An Admin User',
]))->log('Bar');

$activities = Activity::causedByType(User::class)->get();

expect($activities)->toHaveCount(1);
expect($activities->first()->causer_id)->toEqual($causer->getKey());
expect($activities->first()->causer_type)->toEqual(get_class($causer));
expect($activities->first()->description)->toEqual('Foo');
});

it('provides a scope to get log items for a specific event', function () {
$subject = Article::first();
activity()
Expand Down Expand Up @@ -83,6 +119,40 @@
expect($activities->first()->description)->toEqual('Foo');
});

it('provides a scope to get log items for a specific subject class of given model', function () {
$subject = Article::first();
$causer = User::first();

activity()->on($subject)->by($causer)->log('Foo');
activity()->on(Post::create([
'name' => 'A Post',
]))->by($causer)->log('Bar');

$activities = Activity::forSubjectType($subject)->get();

expect($activities)->toHaveCount(1);
expect($activities->first()->subject_id)->toEqual($subject->getKey());
expect($activities->first()->subject_type)->toEqual(get_class($subject));
expect($activities->first()->description)->toEqual('Foo');
});

it('provides a scope to get log items for a specific subject class of given class name', function () {
$subject = Article::first();
$causer = User::first();

activity()->on($subject)->by($causer)->log('Foo');
activity()->on(Post::create([
'name' => 'A Post',
]))->by($causer)->log('Bar');

$activities = Activity::forSubjectType(Article::class)->get();

expect($activities)->toHaveCount(1);
expect($activities->first()->subject_id)->toEqual($subject->getKey());
expect($activities->first()->subject_type)->toEqual(get_class($subject));
expect($activities->first()->description)->toEqual('Foo');
});

it('provides a scope to get log items for a specific morphmapped causer', function () {
Relation::morphMap([
'articles' => 'Spatie\Activitylog\Test\Models\Article',
Expand Down
12 changes: 12 additions & 0 deletions tests/Models/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spatie\Activitylog\Test\Models;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\CausesActivity;

class Admin extends User implements Authenticatable
{
protected $table = 'admins';
}
8 changes: 8 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Spatie\Activitylog\Test\Models;

class Post extends Article
{
protected $table = 'posts';
}
8 changes: 7 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Spatie\Activitylog\ActivitylogServiceProvider;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\Admin;
use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Test\Models\Post;
use Spatie\Activitylog\Test\Models\User;

abstract class TestCase extends OrchestraTestCase
Expand Down Expand Up @@ -49,7 +51,7 @@ protected function setUpDatabase()
{
$this->migrateActivityLogTable();

$this->createTables('articles', 'users');
$this->createTables('articles', 'posts', 'users', 'admins');
$this->seedModels(Article::class, User::class);
}

Expand Down Expand Up @@ -82,6 +84,10 @@ protected function createTables(...$tableNames)
$table->decimal('price')->nullable();
$table->string('status')->nullable();
}
if ($tableName === 'posts') {
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
}
});
});
}
Expand Down

0 comments on commit ce607a9

Please sign in to comment.