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

For [Adding a new column for events #673] #702

Merged
merged 7 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ build
composer.lock
vendor
.phpunit.result.cache
./.idea
./.idea/*
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/composerJson.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions .idea/laravel-activitylog.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions migrations/update_activity_log_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UpdateActivityLogTable extends Migration
AbdullahFaqeir marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* Run the migrations.
*/
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->string('event')->nullable()->after('subject_type');
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
}
}
11 changes: 11 additions & 0 deletions src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ public function byAnonymous()
return $this->causedByAnonymous();
}

public function event(?string $event)
{
return $this->setEvent($event);
AbdullahFaqeir marked this conversation as resolved.
Show resolved Hide resolved
}

public function setEvent(?string $event)
{
$this->activity->event = $event;
return $this;
}

public function withProperties($properties)
{
$this->getActivity()->properties = collect($properties);
Expand Down
8 changes: 8 additions & 0 deletions src/ActivitylogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function boot()
__DIR__.'/../migrations/create_activity_log_table.php.stub' => database_path("/migrations/{$timestamp}_create_activity_log_table.php"),
], 'migrations');
}

if (!class_exists('UpdateActivityLogTable')) {
$timestamp = date('Y_m_d_His', time());
AbdullahFaqeir marked this conversation as resolved.
Show resolved Hide resolved

$this->publishes([
__DIR__ . '/../migrations/update_activity_log_table.php.stub' => database_path("/migrations/{$timestamp}_update_activity_log_table.php"),
], 'migrations');
}
}

public function register()
Expand Down
7 changes: 5 additions & 2 deletions src/Contracts/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Collection;

interface Activity
{
interface Activity {

public function subject(): MorphTo;

public function causer(): MorphTo;
Expand All @@ -21,5 +21,8 @@ public function scopeInLog(Builder $query, ...$logNames): Builder;

public function scopeCausedBy(Builder $query, Model $causer): Builder;

public function scopeForEvent(Builder $query, string $event);
AbdullahFaqeir marked this conversation as resolved.
Show resolved Hide resolved

public function scopeForSubject(Builder $query, Model $subject): Builder;

}
6 changes: 6 additions & 0 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ public function scopeForSubject(Builder $query, Model $subject): Builder
->where('subject_type', $subject->getMorphClass())
->where('subject_id', $subject->getKey());
}

public function scopeForEvent(Builder $query, string $event)
{
return $query->where('event', $event);
}

}
1 change: 1 addition & 0 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected static function bootLogsActivity()

$logger = app(ActivityLogger::class)
->useLog($logName)
->event($eventName)
->performedOn($model)
->withProperties($attrs);

Expand Down
20 changes: 20 additions & 0 deletions tests/ActivityLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ public function it_can_replace_the_placeholders()
$this->assertEquals($expectedDescription, $this->getLastActivity()->description);
}

/** @test */
public function it_can_log_an_activity_with_event()
{
$article = Article::create(['name' => 'article name']);
activity()
->performedOn($article)
->event('create')
->log('test event');

$this->assertEquals('create', $this->getLastActivity()->event);
}

/** @test */
public function it_will_not_replace_non_placeholders()
{
Expand Down Expand Up @@ -301,6 +313,14 @@ public function it_accepts_null_parameter_for_caused_by()
$this->markTestAsPassed();
}

/** @test */
public function it_accepts_null_parameter_for_event()
{
activity()->event(NULL)->log('Foo');

$this->markTestAsPassed();
}

/** @test */
public function it_can_log_activity_when_attributes_are_changed_with_tap()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/ActivityModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ public function it_provides_a_scope_to_get_log_items_for_a_specific_causer()
$this->assertEquals('Foo', $activities->first()->description);
}

/** @test */
public function it_provides_a_scope_to_get_log_items_for_a_specific_event()
{
$subject = Article::first();
activity()
->on($subject)
->event('create')
->log('Foo');
$activities = Activity::forEvent('create')->get();
$this->assertCount(1, $activities);
$this->assertEquals('create', $activities->first()->event);
}

/** @test */
public function it_provides_a_scope_to_get_log_items_for_a_specific_subject()
{
Expand Down
Loading