From 08a128edbfc96493f45d785381ab0be4479cd763 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 23 Mar 2020 18:09:07 +0100 Subject: [PATCH 01/34] v3.14.1 --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5577289d..f5a32824 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to `spatie/laravel-activitylog` will be documented in this file +## 3.14.1 - 2020-03-23 + +- revert breaking changes in `v3.14.0` + +## 3.14.0 - 2020-03-23 - BC + +Please use `v3.14.1` instead - this release is breaking because of the new column. There is also a `v4.0.0-rc.1` release that equals to this one. + +- add `\Spatie\Activitylog\ActivityLogger::event()` method and column [#702](https://github.com/spatie/laravel-activitylog/pull/702) + ## 3.13.0 - 2020-03-13 - add `\Spatie\Activitylog\ActivityLogger::withoutLogs()` method [#695](https://github.com/spatie/laravel-activitylog/pull/695) From d9506c936b7f40bb3d9f7f927a53c187bebba951 Mon Sep 17 00:00:00 2001 From: Vaibhav Pandey Date: Fri, 27 Mar 2020 14:46:40 +0530 Subject: [PATCH 02/34] Use the `nullableMorphs` blueprint method. Since Laravel 5.8, `nullableMorphs` is available as a shortcut method and the package has dropped support for Laravel 5.7 & lower since 3.3 so this should now be used in newer versions. --- migrations/create_activity_log_table.php.stub | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/migrations/create_activity_log_table.php.stub b/migrations/create_activity_log_table.php.stub index 4f0f9432..d2743d38 100644 --- a/migrations/create_activity_log_table.php.stub +++ b/migrations/create_activity_log_table.php.stub @@ -15,16 +15,11 @@ class CreateActivityLogTable extends Migration $table->bigIncrements('id'); $table->string('log_name')->nullable(); $table->text('description'); - $table->unsignedBigInteger('subject_id')->nullable(); - $table->string('subject_type')->nullable(); - $table->unsignedBigInteger('causer_id')->nullable(); - $table->string('causer_type')->nullable(); + $table->nullableMorphs('subject', 'subject'); + $table->nullableMorphs('causer', 'causer'); $table->json('properties')->nullable(); $table->timestamps(); - $table->index('log_name'); - $table->index(['subject_id', 'subject_type'], 'subject'); - $table->index(['causer_id', 'causer_type'], 'causer'); }); } From 0b3fbbe8dae6e42b4114eda087ad43613724518e Mon Sep 17 00:00:00 2001 From: Aleksandr Kopelevich Date: Mon, 30 Mar 2020 14:24:17 +0300 Subject: [PATCH 03/34] Update FUNDING.yml --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 636ef538..fe5143b5 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ +github: spatie custom: https://spatie.be/open-source/support-us From 9d938eef48d099ee64fd7a53c033498e43566bb8 Mon Sep 17 00:00:00 2001 From: Karsten Date: Thu, 9 Apr 2020 23:15:21 +0200 Subject: [PATCH 04/34] Added .env config option --- docs/installation-and-setup.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/installation-and-setup.md b/docs/installation-and-setup.md index d9468d05..fa4b5702 100644 --- a/docs/installation-and-setup.md +++ b/docs/installation-and-setup.md @@ -11,6 +11,10 @@ composer require spatie/laravel-activitylog The package will automatically register the service provider. +Add ACTIVITY_LOGGER_DB_CONNECTION to .env. + +Clear the config cache. + You can publish the migration with: ```bash php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations" From 713bae42a25df760a38b29e2aa3a4bd64bed5e22 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 13:40:34 +0200 Subject: [PATCH 05/34] Create stale-issues.yml --- .github/workflows/stale-issues.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/workflows/stale-issues.yml diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml new file mode 100644 index 00000000..95da63dc --- /dev/null +++ b/.github/workflows/stale-issues.yml @@ -0,0 +1,16 @@ +name: "Close stale issues" +on: + schedule: + - cron: "0 0 * * *" + +jobs: + stale: + runs-on: ubuntu-latest + steps: + - uses: actions/stale@v1.1.0 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + stale-issue-message: 'Stale issue message' + stale-pr-message: 'This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days' + stale-issue-label: 'stale' + exempt-issue-labels: 'bug,enhancement,help wanted,next release,revisit for next major version' From 0419a8c0c917925511bc8660c3cfd29c58b1f937 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 13:41:25 +0200 Subject: [PATCH 06/34] Update stale-issues.yml --- .github/workflows/stale-issues.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml index 95da63dc..869dc8cd 100644 --- a/.github/workflows/stale-issues.yml +++ b/.github/workflows/stale-issues.yml @@ -10,7 +10,6 @@ jobs: - uses: actions/stale@v1.1.0 with: repo-token: ${{ secrets.GITHUB_TOKEN }} - stale-issue-message: 'Stale issue message' - stale-pr-message: 'This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days' + stale-issue-message: 'This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days' stale-issue-label: 'stale' exempt-issue-labels: 'bug,enhancement,help wanted,next release,revisit for next major version' From a434d36a36d8eecec50bf56b544020b54e029dd9 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 13:41:43 +0200 Subject: [PATCH 07/34] Update stale-issues.yml --- .github/workflows/stale-issues.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml index 869dc8cd..5e204b7e 100644 --- a/.github/workflows/stale-issues.yml +++ b/.github/workflows/stale-issues.yml @@ -1,7 +1,7 @@ name: "Close stale issues" on: schedule: - - cron: "0 0 * * *" + - cron: "0 * * * *" jobs: stale: From 30cc089fab9a811c8a82b6f3103abb49867ec2e6 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 14:15:20 +0200 Subject: [PATCH 08/34] Update stale-issues.yml --- .github/workflows/stale-issues.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml index 5e204b7e..865c438e 100644 --- a/.github/workflows/stale-issues.yml +++ b/.github/workflows/stale-issues.yml @@ -13,3 +13,6 @@ jobs: stale-issue-message: 'This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days' stale-issue-label: 'stale' exempt-issue-labels: 'bug,enhancement,help wanted,next release,revisit for next major version' + days-before-stale: 21 + days-before-close: 7 + debug-only: true From e171138c48f10944414b6047b6e272e12bbffcf8 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 14:18:13 +0200 Subject: [PATCH 09/34] Update stale-issues.yml --- .github/workflows/stale-issues.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml index 865c438e..b7dc737b 100644 --- a/.github/workflows/stale-issues.yml +++ b/.github/workflows/stale-issues.yml @@ -1,7 +1,7 @@ name: "Close stale issues" on: schedule: - - cron: "0 * * * *" + - cron: "*/5 * * * *" jobs: stale: From 0fa6446f66d8d45edae74aa5a4f2079018ae89c3 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 14:28:01 +0200 Subject: [PATCH 10/34] Update stale-issues.yml --- .github/workflows/stale-issues.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml index b7dc737b..5a7fc470 100644 --- a/.github/workflows/stale-issues.yml +++ b/.github/workflows/stale-issues.yml @@ -7,7 +7,7 @@ jobs: stale: runs-on: ubuntu-latest steps: - - uses: actions/stale@v1.1.0 + - uses: actions/stale@v2.0.0 with: repo-token: ${{ secrets.GITHUB_TOKEN }} stale-issue-message: 'This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days' @@ -15,4 +15,3 @@ jobs: exempt-issue-labels: 'bug,enhancement,help wanted,next release,revisit for next major version' days-before-stale: 21 days-before-close: 7 - debug-only: true From 2729b80bee51b07b88611bc161349686af4bebda Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 14:33:56 +0200 Subject: [PATCH 11/34] Update stale-issues.yml --- .github/workflows/stale-issues.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/stale-issues.yml b/.github/workflows/stale-issues.yml index 5a7fc470..1483c370 100644 --- a/.github/workflows/stale-issues.yml +++ b/.github/workflows/stale-issues.yml @@ -1,7 +1,7 @@ name: "Close stale issues" on: schedule: - - cron: "*/5 * * * *" + - cron: "0 0 * * *" jobs: stale: From e895bde2a3bc9a9936bfc5119b30a7154af6f9b9 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 4 May 2020 14:36:47 +0200 Subject: [PATCH 12/34] Create composer-normalize.yml --- .github/workflows/composer-normalize.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/composer-normalize.yml diff --git a/.github/workflows/composer-normalize.yml b/.github/workflows/composer-normalize.yml new file mode 100644 index 00000000..560c42b1 --- /dev/null +++ b/.github/workflows/composer-normalize.yml @@ -0,0 +1,22 @@ +name: normalize composer.json + +on: + push: + paths: + - 'composer.json' + +jobs: + normalize: + runs-on: ubuntu-latest + steps: + - name: Git checkout + uses: actions/checkout@v2 + + - name: normalize composer.json + run: | + composer global require ergebnis/composer-normalize + composer normalize + + - uses: stefanzweifel/git-auto-commit-action@v4.0.0 + with: + commit_message: normalize composer.json From e15617a54cf3bf9ca7668e473e62f109fef353df Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 19 May 2020 16:00:46 +0200 Subject: [PATCH 13/34] #733 - do not call fresh() if retrieved event is processed --- src/Traits/DetectsChanges.php | 10 +++++++--- tests/LogsActivityTest.php | 15 +++++++++++++++ tests/Models/Issue733.php | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/Models/Issue733.php diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 75e7cfa6..2ed8436e 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -91,9 +91,13 @@ public function attributeValuesToBeLogged(string $processingEvent): array } $properties['attributes'] = static::logChanges( - $this->exists - ? $this->fresh() ?? $this - : $this + $processingEvent == 'retrieved' + ? $this + : ( + $this->exists + ? $this->fresh() ?? $this + : $this + ) ); if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index b6100c1e..ce51ccf8 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Collection; use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Test\Models\Article; +use Spatie\Activitylog\Test\Models\Issue733; use Spatie\Activitylog\Test\Models\User; use Spatie\Activitylog\Traits\LogsActivity; @@ -423,6 +424,20 @@ public function it_will_submit_a_log_with_json_changes() $this->assertSame($expectedChanges, $changes); } + /** @test */ + public function it_will_log_the_retrieval_of_the_model() + { + $article = Issue733::create(['name' => 'my name']); + + $retrieved = Issue733::whereKey($article->getKey())->first(); + $this->assertTrue($article->is($retrieved)); + + $this->assertInstanceOf(get_class($article), $this->getLastActivity()->subject); + $this->assertTrue($article->is($this->getLastActivity()->subject)); + $this->assertEquals('retrieved', $this->getLastActivity()->description); + $this->assertEquals('retrieved', $this->getLastActivity()->event); + } + public function loginWithFakeUser() { $user = new $this->user(); diff --git a/tests/Models/Issue733.php b/tests/Models/Issue733.php new file mode 100644 index 00000000..e2cab372 --- /dev/null +++ b/tests/Models/Issue733.php @@ -0,0 +1,18 @@ + Date: Tue, 19 May 2020 16:07:02 +0200 Subject: [PATCH 14/34] fix test --- tests/LogsActivityTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index ce51ccf8..c75d3f92 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -432,10 +432,12 @@ public function it_will_log_the_retrieval_of_the_model() $retrieved = Issue733::whereKey($article->getKey())->first(); $this->assertTrue($article->is($retrieved)); - $this->assertInstanceOf(get_class($article), $this->getLastActivity()->subject); - $this->assertTrue($article->is($this->getLastActivity()->subject)); - $this->assertEquals('retrieved', $this->getLastActivity()->description); - $this->assertEquals('retrieved', $this->getLastActivity()->event); + $activity = $this->getLastActivity(); + + $this->assertInstanceOf(get_class($article), $activity->subject); + $this->assertTrue($article->is($activity->subject)); + $this->assertEquals('retrieved', $activity->description); + $this->assertEquals('retrieved', $activity->event); } public function loginWithFakeUser() From 990c67579c3bc68ed9ca06ef0547d5fc81ad69e8 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 19 May 2020 16:08:12 +0200 Subject: [PATCH 15/34] event attribute is not available on current master --- tests/LogsActivityTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index c75d3f92..41847936 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -437,7 +437,6 @@ public function it_will_log_the_retrieval_of_the_model() $this->assertInstanceOf(get_class($article), $activity->subject); $this->assertTrue($article->is($activity->subject)); $this->assertEquals('retrieved', $activity->description); - $this->assertEquals('retrieved', $activity->event); } public function loginWithFakeUser() From f1261c461ee4a92fd1e72012b516147ba28a91bb Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 19 May 2020 16:31:33 +0200 Subject: [PATCH 16/34] v3.14.2 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5a32824..adb5a3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `spatie/laravel-activitylog` will be documented in this file +## 3.14.2 - 2020-05-19 + +- fix `retrieved` event logging + ## 3.14.1 - 2020-03-23 - revert breaking changes in `v3.14.0` From 8878c9739f72361b8ae807f2b7efc237bf5b6e81 Mon Sep 17 00:00:00 2001 From: Adriaan Marain Date: Wed, 27 May 2020 15:13:57 +0200 Subject: [PATCH 17/34] Update README with new "Support us" section --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 25c46e62..149453ba 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,11 @@ Calling `$activity->changes()` will return this array: ## Support us -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). +Learn how to create a package like this one, by watching our premium video course: + +[![Laravel Package training](https://spatie.be/github/package-training.jpg)](https://laravelpackage.training) + +We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). @@ -80,7 +84,6 @@ Find yourself stuck using the package? Found a bug? Do you have general question If you've found a security issue please mail [freek@spatie.be](mailto:freek@spatie.be) instead of using the issue tracker. - ## Installation You can install the package via composer: @@ -173,7 +176,6 @@ Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes. Please see [UPGRADING](UPGRADING.md) for details. - ## Testing ``` bash From 717cfefd975560bade3434ae5227b6af745af9df Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Thu, 28 May 2020 12:13:08 +0200 Subject: [PATCH 18/34] add issue templates --- .github/ISSUE_TEMPLATE/bug-report.md | 36 ++++++++++++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 15 ++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/config.yml diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md new file mode 100644 index 00000000..bf212364 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -0,0 +1,36 @@ +--- +name: "🐛 Bug report" +about: Create a bug report to help us improve +title: '' +labels: bug +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Things needed to reproduce the error. +eg: Configuration, Migration, Model, Controller/Command + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Versions (please complete the following information)** +- PHP: +- Database: +- Laravel: +- Package: + +**Additional context** +Add any other context about the problem here. + +**Exception** +The thrown exception message. + +**Stack Trace** +The full stack trace of the thrown exception. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..e168e29c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,15 @@ +blank_issues_enabled: false +contact_links: + - name: 🚀 Feature Request + url: https://github.com/spatie/laravel-activitylog/discussions/new?category_id=4507850 + about: Share ideas for new features + - name: 🙏 Ask a Question + url: https://github.com/spatie/laravel-activitylog/discussions/new?category_id=4507849 + about: Ask the community for help + - name: 💖 Share some love + url: https://github.com/spatie/laravel-activitylog/discussions/new?category_id=4507852 + about: Share gratitude with the team + - name: 🚨 Report Security Vulnerability + url: mailto:freek@spatie.be + about: Please report security vulnerabilities by email. + From 6c5b52aace9a1294566ab6199d103926880996d9 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Thu, 28 May 2020 12:15:50 +0200 Subject: [PATCH 19/34] add issue templates --- .github/ISSUE_TEMPLATE/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index e168e29c..11012660 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -10,6 +10,6 @@ contact_links: url: https://github.com/spatie/laravel-activitylog/discussions/new?category_id=4507852 about: Share gratitude with the team - name: 🚨 Report Security Vulnerability - url: mailto:freek@spatie.be - about: Please report security vulnerabilities by email. + url: https://github.com/spatie/laravel-activitylog#security + about: Please report security vulnerabilities by email to freek@spatie.be From 8b1632197cdd53cf8a1e0d75a40cb26986da1b62 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Thu, 28 May 2020 12:39:11 +0200 Subject: [PATCH 20/34] uppercase name --- .github/ISSUE_TEMPLATE/bug-report.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md index bf212364..501d1bc5 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -1,5 +1,5 @@ --- -name: "🐛 Bug report" +name: "🐛 Bug Report" about: Create a bug report to help us improve title: '' labels: bug From b07e562babd3d88d23350f8ce5453e49add4c33c Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 7 Jul 2020 19:29:28 +0200 Subject: [PATCH 21/34] run test only on foreign PR --- .github/workflows/run-tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index ed218134..53283a1f 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,6 +8,7 @@ on: jobs: test: + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository runs-on: ${{ matrix.os }} strategy: fail-fast: false From fc93c52d53fbeded2174a59d29afa9990b456086 Mon Sep 17 00:00:00 2001 From: Musa <40173603+voyula@users.noreply.github.com> Date: Sun, 16 Aug 2020 04:39:26 +0300 Subject: [PATCH 22/34] Style Fixes --- docs/advanced-usage/disabling-logging.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/advanced-usage/disabling-logging.md b/docs/advanced-usage/disabling-logging.md index 7e7fff62..70191e09 100644 --- a/docs/advanced-usage/disabling-logging.md +++ b/docs/advanced-usage/disabling-logging.md @@ -16,7 +16,7 @@ If you want to enable logging again call `activity()->enableLogging()`. If you want to run a given code snippet without logs you can use the `withoutLogs()` method. ```php -activity()->withoutLogs(function(){ +activity()->withoutLogs(function () { // ... }); ``` From 01b152942cfc6584e7a5ac7169f83de4796910f5 Mon Sep 17 00:00:00 2001 From: Mark van den Broek Date: Tue, 8 Sep 2020 23:37:38 +0200 Subject: [PATCH 23/34] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 149453ba..09fced50 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![StyleCI](https://styleci.io/repos/61802818/shield)](https://styleci.io/repos/61802818) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog) -The `spatie/laravel-activitylog` package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. +The `spatie/laravel-activitylog` package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. The Package stores all activity in the `activity_log` table. Here's a demo of how you can use it: @@ -29,7 +29,7 @@ activity() ->causedBy($user) ->withProperties(['customProperty' => 'customValue']) ->log('Look, I logged something'); - + $lastLoggedActivity = Activity::all()->last(); $lastLoggedActivity->subject; //returns an instance of an eloquent model From 450adecfd2e1a5766d77ba9867937928d4c91955 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Wed, 9 Sep 2020 10:05:56 +0200 Subject: [PATCH 24/34] Add support for Laravel 8 --- CONTRIBUTING.md => .github/CONTRIBUTING.md | 0 .github/workflows/deploy-docs.yml | 19 ------ .github/workflows/php-cs-fixer.yml | 23 ++++++++ .github/workflows/run-tests.yml | 69 ++++++++++++---------- .gitignore | 2 + .php_cs.dist | 37 ++++++++++++ .scrutinizer.yml | 23 -------- .styleci.yml | 1 - README.md | 3 - composer.json | 8 +-- 10 files changed, 103 insertions(+), 82 deletions(-) rename CONTRIBUTING.md => .github/CONTRIBUTING.md (100%) delete mode 100644 .github/workflows/deploy-docs.yml create mode 100644 .github/workflows/php-cs-fixer.yml create mode 100644 .php_cs.dist delete mode 100644 .scrutinizer.yml delete mode 100644 .styleci.yml diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md diff --git a/.github/workflows/deploy-docs.yml b/.github/workflows/deploy-docs.yml deleted file mode 100644 index f787e5c0..00000000 --- a/.github/workflows/deploy-docs.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: deploy-docs - -on: - push: - paths: - - 'docs/**' - -jobs: - deploy: - - runs-on: ubuntu-latest - - name: Deploy docs - - steps: - - name: Netlify deploy - uses: wei/curl@v1 - with: - args: -X POST -d {} ${{ secrets.NETLIFY_DEPLOY_URL }}?trigger_title=laravel-activitylog diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml new file mode 100644 index 00000000..ee02d443 --- /dev/null +++ b/.github/workflows/php-cs-fixer.yml @@ -0,0 +1,23 @@ +name: Check & fix styling + +on: [ push ] + +jobs: + php-cs-fixer: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + + - name: Run PHP CS Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php_cs.dist --allow-risky=yes + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 53283a1f..b1feb249 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -4,7 +4,7 @@ on: push: pull_request: schedule: - - cron: '0 0 * * *' + - cron: '0 0 * * *' jobs: test: @@ -13,40 +13,45 @@ jobs: strategy: fail-fast: false matrix: - php: [7.4, 7.3, 7.2] - laravel: [7.*, 6.*] - dependency-version: [prefer-lowest, prefer-stable] - os: [ubuntu-latest, windows-latest] + php: [ 7.4, 7.3, 7.2 ] + laravel: [ 8.*, 7.*, 6.* ] + dependency-version: [ prefer-lowest, prefer-stable ] + os: [ ubuntu-latest, windows-latest ] include: - - laravel: 7.* - testbench: 5.* - - laravel: 6.* - testbench: 4.* + - laravel: 8.* + testbench: 6.* + - laravel: 7.* + testbench: 5.* + - laravel: 6.* + testbench: 4.* + exclude: + - laravel: 8.* + php: 7.2 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} steps: - - name: Checkout code - uses: actions/checkout@v1 - - - name: Cache dependencies - uses: actions/cache@v1 - with: - path: ~/.composer/cache/files - key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - - - name: Setup PHP - uses: shivammathur/setup-php@v1 - with: - php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick - coverage: none - - - name: Install dependencies - run: | - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - - - name: Execute tests - run: vendor/bin/phpunit + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick + coverage: none + + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + + - name: Execute tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index b6bd0e71..cab72a48 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ build composer.lock vendor .phpunit.result.cache +.php_cs.cache + diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 00000000..1c4e7d56 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,37 @@ +notPath('bootstrap/*') + ->notPath('storage/*') + ->notPath('resources/view/mail/*') + ->in([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return PhpCsFixer\Config::create() + ->setRules([ + '@PSR2' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sortAlgorithm' => 'alpha'], + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, + 'trailing_comma_in_multiline_array' => true, + 'phpdoc_scalar' => true, + 'unary_operator_spaces' => true, + 'binary_operator_spaces' => true, + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_var_without_name' => true, + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => true, + ] + ]) + ->setFinder($finder); diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 9e0ed447..00000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,23 +0,0 @@ -filter: - excluded_paths: [tests/*] - -checks: - php: - remove_extra_empty_lines: true - remove_php_closing_tag: true - remove_trailing_whitespace: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: true - order_alphabetically: true - fix_php_opening_tag: true - fix_linefeed: true - fix_line_ending: true - fix_identation_4spaces: true - fix_doc_comments: true - -tools: - external_code_coverage: - timeout: 120 - runs: 4 diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 0285f179..00000000 --- a/.styleci.yml +++ /dev/null @@ -1 +0,0 @@ -preset: laravel diff --git a/README.md b/README.md index 149453ba..75838e52 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,6 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/spatie/laravel-activitylog/run-tests?label=tests) -[![Code coverage](https://scrutinizer-ci.com/g/spatie/laravel-activitylog/badges/coverage.png)](https://scrutinizer-ci.com/g/spatie/laravel-activitylog) -[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-activitylog.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-activitylog) -[![StyleCI](https://styleci.io/repos/61802818/shield)](https://styleci.io/repos/61802818) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog) The `spatie/laravel-activitylog` package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. diff --git a/composer.json b/composer.json index 8a3567a2..788d2abd 100644 --- a/composer.json +++ b/composer.json @@ -31,13 +31,13 @@ ], "require": { "php": "^7.2", - "illuminate/config": "^6.0|^7.0", - "illuminate/database": "^6.0|^7.0", - "illuminate/support": "^6.0|^7.0" + "illuminate/config": "^6.0|^7.0|^8.0", + "illuminate/database": "^6.0|^7.0|^8.0", + "illuminate/support": "^6.0|^7.0|^8.0" }, "require-dev": { "ext-json": "*", - "orchestra/testbench": "^4.0|^5.0", + "orchestra/testbench": "^4.0|^5.0|^6.0", "phpunit/phpunit": "^8.0|^9.0" }, "autoload": { From d86017b69d619d6aa57b58262e2ec16b5280f184 Mon Sep 17 00:00:00 2001 From: rubenvanassche Date: Wed, 9 Sep 2020 08:06:23 +0000 Subject: [PATCH 25/34] Fix styling --- tests/ActivityLoggerTest.php | 1 + tests/DetectsChangesTest.php | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/ActivityLoggerTest.php b/tests/ActivityLoggerTest.php index 1112e242..5f96d6fc 100644 --- a/tests/ActivityLoggerTest.php +++ b/tests/ActivityLoggerTest.php @@ -390,6 +390,7 @@ public function it_will_disable_logs_for_a_callback_without_affecting_previous_s try { activity()->withoutLogs(function () { activity()->log('created'); + throw new Exception('OH NO'); }); } catch (Exception $ex) { diff --git a/tests/DetectsChangesTest.php b/tests/DetectsChangesTest.php index e60cedb6..e1800a58 100644 --- a/tests/DetectsChangesTest.php +++ b/tests/DetectsChangesTest.php @@ -1274,14 +1274,14 @@ public function it_will_return_null_for_missing_json_attribute() $expectedChanges = [ 'attributes' => [ - 'json' => [ + 'json' => [ 'data' => [ 'missing' => 'I wasn\'t here', ], ], ], 'old' => [ - 'json' => [ + 'json' => [ 'data' => [ 'missing' => null, ], @@ -1329,7 +1329,7 @@ public function it_will_return_an_array_for_sub_key_in_json_attribute() $expectedChanges = [ 'attributes' => [ - 'json' => [ + 'json' => [ 'data' => [ 'data_a' => 1, 'data_b' => 2, @@ -1340,7 +1340,7 @@ public function it_will_return_an_array_for_sub_key_in_json_attribute() ], ], 'old' => [ - 'json' => [ + 'json' => [ 'data' => [ 'data_a' => 1, 'data_b' => 2, @@ -1385,7 +1385,7 @@ public function it_will_access_further_than_level_one_json_attribute() $expectedChanges = [ 'attributes' => [ - 'json' => [ + 'json' => [ 'data' => [ 'can' => [ 'go' => [ @@ -1398,7 +1398,7 @@ public function it_will_access_further_than_level_one_json_attribute() ], ], 'old' => [ - 'json' => [ + 'json' => [ 'data' => [ 'can' => [ 'go' => [ From 317579c3ff6e8f6d5b0c328bede0830db72474e8 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Wed, 9 Sep 2020 10:12:38 +0200 Subject: [PATCH 26/34] Add support for Laravel 8 --- CHANGELOG.md | 4 ++++ README.md | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index adb5a3eb..a7effbc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `spatie/laravel-activitylog` will be documented in this file +## 3.14.3 - 2020-09-09 + +- Add support for Laravel 8 + ## 3.14.2 - 2020-05-19 - fix `retrieved` event logging diff --git a/README.md b/README.md index ed626b49..61f4eb39 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/spatie/laravel-activitylog/run-tests?label=tests) +![Check & fix styling](https://github.com/spatie/laravel-activitylog/workflows/Check%20&%20fix%20styling/badge.svg) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-activitylog.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-activitylog) The `spatie/laravel-activitylog` package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. From 91fc672e626214a4eb2db24d11953633e9b99c12 Mon Sep 17 00:00:00 2001 From: Reza Tayebi <31609638+rezat1994@users.noreply.github.com> Date: Mon, 14 Sep 2020 13:17:34 +0200 Subject: [PATCH 27/34] allow to log multi level related model attributes #772 --- src/Traits/DetectsChanges.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 2ed8436e..89e1a349 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -176,17 +176,21 @@ public static function logChanges(Model $model): array protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array { - if (substr_count($attribute, '.') > 1) { - throw CouldNotLogChanges::invalidAttribute($attribute); - } + $relatedModelNames = explode('.', $attribute); + $relatedAttribute = array_pop($relatedModelNames); + + $attributeName = []; + $relatedModel = $model; - [$relatedModelName, $relatedAttribute] = explode('.', $attribute); + do { + $attributeName[] = $relatedModelName = Str::camel(array_shift($relatedModelNames)); - $relatedModelName = Str::camel($relatedModelName); + $relatedModel = $relatedModel->$relatedModelName ?? $relatedModel->$relatedModelName(); + } while (!empty($relatedModelNames)); - $relatedModel = $model->$relatedModelName ?? $model->$relatedModelName(); + $attributeName[] = $relatedAttribute; - return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute ?? null]; + return [implode('.', $attributeName) => $relatedModel->$relatedAttribute ?? null]; } protected static function getModelAttributeJsonValue(Model $model, string $attribute) From 0356da93134bb1b3291280e4105f7f6aa5ad90ee Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 14 Sep 2020 13:18:30 +0200 Subject: [PATCH 28/34] add testcase for new multi level related model attribute logging --- tests/DetectsChangesTest.php | 36 ++++++++++++++++++++++++++++++++++++ tests/Models/User.php | 5 +++++ 2 files changed, 41 insertions(+) diff --git a/tests/DetectsChangesTest.php b/tests/DetectsChangesTest.php index e1800a58..348e4d42 100644 --- a/tests/DetectsChangesTest.php +++ b/tests/DetectsChangesTest.php @@ -318,6 +318,42 @@ public function it_can_store_the_dirty_changes_when_updating_a_related_model() $this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray()); } + /** @test */ + public function it_can_store_the_changes_when_saving_including_multi_level_related_model() + { + $articleClass = new class() extends Article { + public static $logAttributes = ['name', 'text', 'user.latest_article.name']; + + use LogsActivity; + }; + + $user = User::create([ + 'name' => 'a name', + ]); + + $articleClass::create([ + 'name' => 'name #1', + 'text' => 'text #1', + 'user_id' => $user->id, + ]); + + $articleClass::create([ + 'name' => 'name #2', + 'text' => 'text #2', + 'user_id' => $user->id, + ]); + + $expectedChanges = [ + 'attributes' => [ + 'name' => 'name #2', + 'text' => 'text #2', + 'user.latestArticle.name' => 'name #1', + ], + ]; + + $this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray()); + } + /** @test */ public function it_will_store_no_changes_when_not_logging_attributes() { diff --git a/tests/Models/User.php b/tests/Models/User.php index d39d0a93..7a75d7f2 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -51,4 +51,9 @@ public function articles() { return $this->hasMany(Article::class); } + + public function latestArticle() + { + return $this->hasOne(Article::class)->latest()->limit(1); + } } From 197b492bec17a0acb13f3bf686ff1b0c15eaeabd Mon Sep 17 00:00:00 2001 From: Gummibeer Date: Mon, 14 Sep 2020 11:18:56 +0000 Subject: [PATCH 29/34] Fix styling --- src/Traits/DetectsChanges.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 89e1a349..4459b766 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -5,7 +5,6 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Str; -use Spatie\Activitylog\Exceptions\CouldNotLogChanges; trait DetectsChanges { @@ -186,7 +185,7 @@ protected static function getRelatedModelAttributeValue(Model $model, string $at $attributeName[] = $relatedModelName = Str::camel(array_shift($relatedModelNames)); $relatedModel = $relatedModel->$relatedModelName ?? $relatedModel->$relatedModelName(); - } while (!empty($relatedModelNames)); + } while (! empty($relatedModelNames)); $attributeName[] = $relatedAttribute; From 5b019b152d23400da99be0be742c250970204bc4 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 14 Sep 2020 13:27:06 +0200 Subject: [PATCH 30/34] v3.15.0 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7effbc0..40463558 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `spatie/laravel-activitylog` will be documented in this file +## 3.15.0 - 2020-09-14 + +- Add multiple/chained relation attribute logging support - [#784](https://github.com/spatie/laravel-activitylog/pull/784) + ## 3.14.3 - 2020-09-09 - Add support for Laravel 8 From f625f426a52b8c5179a8e25bd85c811e8ffcb83c Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 15 Sep 2020 10:16:36 +0200 Subject: [PATCH 31/34] normalize composer.json --- composer.json | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 788d2abd..5f6223a7 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,6 @@ { "name": "spatie/laravel-activitylog", "description": "A very simple activity logger to monitor the users of your website or application", - "homepage": "https://github.com/spatie/activitylog", "keywords": [ "spatie", "log", @@ -9,6 +8,8 @@ "activity", "laravel" ], + "homepage": "https://github.com/spatie/activitylog", + "license": "MIT", "authors": [ { "name": "Freek Van der Herten", @@ -31,14 +32,24 @@ ], "require": { "php": "^7.2", - "illuminate/config": "^6.0|^7.0|^8.0", - "illuminate/database": "^6.0|^7.0|^8.0", - "illuminate/support": "^6.0|^7.0|^8.0" + "illuminate/config": "^6.0 || ^7.0 || ^8.0", + "illuminate/database": "^6.0 || ^7.0 || ^8.0", + "illuminate/support": "^6.0 || ^7.0 || ^8.0" }, "require-dev": { "ext-json": "*", - "orchestra/testbench": "^4.0|^5.0|^6.0", - "phpunit/phpunit": "^8.0|^9.0" + "orchestra/testbench": "^4.0 || ^5.0 || ^6.0", + "phpunit/phpunit": "^8.0 || ^9.0" + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "Spatie\\Activitylog\\ActivitylogServiceProvider" + ] + } }, "autoload": { "psr-4": { @@ -53,20 +64,9 @@ "Spatie\\Activitylog\\Test\\": "tests" } }, + "minimum-stability": "dev", + "prefer-stable": true, "scripts": { "test": "vendor/bin/phpunit" - }, - "license": "MIT", - "extra": { - "laravel": { - "providers": [ - "Spatie\\Activitylog\\ActivitylogServiceProvider" - ] - } - }, - "config": { - "sort-packages": true - }, - "minimum-stability": "dev", - "prefer-stable": true + } } From 9143e22d270f9c05e57e24812756193cfca3860f Mon Sep 17 00:00:00 2001 From: Yash Date: Wed, 16 Sep 2020 17:02:25 +0200 Subject: [PATCH 32/34] replace #721 fix #712 --- src/Traits/DetectsChanges.php | 13 ++++- tests/DetectsChangesTest.php | 100 ++++++++++++++++++++++++++++++++-- 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 4459b766..91861c85 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -182,7 +182,7 @@ protected static function getRelatedModelAttributeValue(Model $model, string $at $relatedModel = $model; do { - $attributeName[] = $relatedModelName = Str::camel(array_shift($relatedModelNames)); + $attributeName[] = $relatedModelName = static::getRelatedModelRelationName($relatedModel, array_shift($relatedModelNames)); $relatedModel = $relatedModel->$relatedModelName ?? $relatedModel->$relatedModelName(); } while (! empty($relatedModelNames)); @@ -192,6 +192,17 @@ protected static function getRelatedModelAttributeValue(Model $model, string $at return [implode('.', $attributeName) => $relatedModel->$relatedAttribute ?? null]; } + protected static function getRelatedModelRelationName(Model $model, string $relation): string + { + return Arr::first([ + $relation, + Str::snake($relation), + Str::camel($relation), + ], function(string $method) use ($model): bool { + return method_exists($model, $method); + }, $relation); + } + protected static function getModelAttributeJsonValue(Model $model, string $attribute) { $path = explode('->', $attribute); diff --git a/tests/DetectsChangesTest.php b/tests/DetectsChangesTest.php index 348e4d42..b01fee39 100644 --- a/tests/DetectsChangesTest.php +++ b/tests/DetectsChangesTest.php @@ -237,11 +237,11 @@ public function it_can_store_the_changes_when_updating_a_related_model() public function it_can_store_the_changes_when_updating_a_snake_case_related_model() { $articleClass = new class() extends Article { - public static $logAttributes = ['name', 'text', 'snake_user.name']; + public static $logAttributes = ['name', 'text', 'snakeUser.name']; use LogsActivity; - public function snakeUser() + public function snake_user() { return $this->belongsTo(User::class, 'user_id'); } @@ -267,12 +267,104 @@ public function snakeUser() 'attributes' => [ 'name' => 'name', 'text' => 'text', - 'snakeUser.name' => 'another name', + 'snake_user.name' => 'another name', ], 'old' => [ 'name' => 'name', 'text' => 'text', - 'snakeUser.name' => 'a name', + 'snake_user.name' => 'a name', + ], + ]; + + $this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray()); + } + + /** @test */ + public function it_can_store_the_changes_when_updating_a_camel_case_related_model() + { + $articleClass = new class() extends Article { + public static $logAttributes = ['name', 'text', 'camel_user.name']; + + use LogsActivity; + + public function camelUser() + { + return $this->belongsTo(User::class, 'user_id'); + } + }; + + $user = User::create([ + 'name' => 'a name', + ]); + + $anotherUser = User::create([ + 'name' => 'another name', + ]); + + $article = $articleClass::create([ + 'name' => 'name', + 'text' => 'text', + 'user_id' => $user->id, + ]); + + $article->user()->associate($anotherUser)->save(); + + $expectedChanges = [ + 'attributes' => [ + 'name' => 'name', + 'text' => 'text', + 'camelUser.name' => 'another name', + ], + 'old' => [ + 'name' => 'name', + 'text' => 'text', + 'camelUser.name' => 'a name', + ], + ]; + + $this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray()); + } + + /** @test */ + public function it_can_store_the_changes_when_updating_a_custom_case_related_model() + { + $articleClass = new class() extends Article { + public static $logAttributes = ['name', 'text', 'Custom_Case_User.name']; + + use LogsActivity; + + public function Custom_Case_User() + { + return $this->belongsTo(User::class, 'user_id'); + } + }; + + $user = User::create([ + 'name' => 'a name', + ]); + + $anotherUser = User::create([ + 'name' => 'another name', + ]); + + $article = $articleClass::create([ + 'name' => 'name', + 'text' => 'text', + 'user_id' => $user->id, + ]); + + $article->user()->associate($anotherUser)->save(); + + $expectedChanges = [ + 'attributes' => [ + 'name' => 'name', + 'text' => 'text', + 'Custom_Case_User.name' => 'another name', + ], + 'old' => [ + 'name' => 'name', + 'text' => 'text', + 'Custom_Case_User.name' => 'a name', ], ]; From ec0df6ce9c64033865a80e79eb932a0e92c3b71e Mon Sep 17 00:00:00 2001 From: Gummibeer Date: Wed, 16 Sep 2020 15:03:03 +0000 Subject: [PATCH 33/34] Fix styling --- src/Traits/DetectsChanges.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 91861c85..4e7a4288 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -198,7 +198,7 @@ protected static function getRelatedModelRelationName(Model $model, string $rela $relation, Str::snake($relation), Str::camel($relation), - ], function(string $method) use ($model): bool { + ], function (string $method) use ($model): bool { return method_exists($model, $method); }, $relation); } From 9790bfb879c128155080d93518a0928f1d6212f6 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Wed, 16 Sep 2020 17:07:24 +0200 Subject: [PATCH 34/34] fix #711 --- docs/installation-and-setup.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/installation-and-setup.md b/docs/installation-and-setup.md index fa4b5702..41a9701b 100644 --- a/docs/installation-and-setup.md +++ b/docs/installation-and-setup.md @@ -11,9 +11,9 @@ composer require spatie/laravel-activitylog The package will automatically register the service provider. -Add ACTIVITY_LOGGER_DB_CONNECTION to .env. +If you want your activities to be stored in a special database connection you can define `ACTIVITY_LOGGER_DB_CONNECTION` in your `.env` file. -Clear the config cache. +After you've configured everything you should clear the application config cache via `artisan config:clear`. You can publish the migration with: ```bash