Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add causer- and subject-type scopes to Activity model #1294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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