From 9ddbba87bd810159ae2b49c03fdb28472f8b0fe4 Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Sun, 16 Oct 2016 18:09:28 +0530 Subject: [PATCH 01/32] Added autoAccept parameter and revision limit by key --- src/Venturecraft/Revisionable/Revision.php | 12 ++++++++++ .../Revisionable/RevisionableTrait.php | 24 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Venturecraft/Revisionable/Revision.php b/src/Venturecraft/Revisionable/Revision.php index acbc20cc..884541fe 100644 --- a/src/Venturecraft/Revisionable/Revision.php +++ b/src/Venturecraft/Revisionable/Revision.php @@ -20,6 +20,8 @@ class Revision extends Eloquent */ public $table = 'revisions'; + protected $dates = ['accepted_at']; + /** * @var array */ @@ -288,4 +290,14 @@ public function format($key, $value) return $value; } } + + public function scopeOnlyPending($q) + { + $q->whereNull('accepted_at'); + } + + public function scopeOnlyAccepted($q) + { + $q->whereNotNull('accepted_at'); + } } diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index f298dcf7..d03fb8a3 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -93,6 +93,17 @@ public function revisionHistory() return $this->morphMany('\Venturecraft\Revisionable\Revision', 'revisionable'); } + /** + * Restrict the result to include only records which has pending revision + * history which are not accepted yet. + */ + public function scopeHasPendingRevisionHistory($query) + { + $query->whereHas('revisionHistory', function($q){ + $q->whereNull('accepted_at'); + }); + } + /** * Generates a list of the last $limit revisions made to any objects of the class it is being called from. * @@ -144,6 +155,10 @@ public function preSave() $this->dirtyData = $this->getDirty(); $this->updating = $this->exists; + + if($this->autoAccept == false){ + $this->attributes = $this->original; + } } } @@ -184,12 +199,19 @@ public function postSave() 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), + 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) ); } if (count($revisions) > 0) { if($LimitReached && $RevisionCleanup){ - $toDelete = $this->revisionHistory()->orderBy('id','asc')->limit(count($revisions))->get(); + $columns = collect($revisions)->pluck('key'); + $toDelete = $this->revisionHistory() + ->whereIn('key', $columns) + ->orderBy('id','asc') + ->limit(count($revisions)) + ->get(); + foreach($toDelete as $delete){ $delete->delete(); } From 2a0021153fa6e89d89bed1e2729fe90f88f4a942 Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Mon, 17 Oct 2016 20:41:29 +0530 Subject: [PATCH 02/32] Fixed revision issue while creation for autoAccept --- src/Venturecraft/Revisionable/RevisionableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index d03fb8a3..9f64eb49 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -156,7 +156,7 @@ public function preSave() $this->dirtyData = $this->getDirty(); $this->updating = $this->exists; - if($this->autoAccept == false){ + if($this->updating && $this->autoAccept == false){ $this->attributes = $this->original; } } From 03da0d91b07eb672551ec1d3bc7e3e4b9e720f2f Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Thu, 20 Oct 2016 00:00:51 +0530 Subject: [PATCH 03/32] Fix the code to work for first time record creation --- src/Venturecraft/Revisionable/RevisionableTrait.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 9f64eb49..5b2c2a55 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -129,10 +129,16 @@ public function preSave() $this->originalData = $this->original; $this->updatedData = $this->attributes; + $this->updating = $this->exists; // we can only safely compare basic items, // so for now we drop any object based items, like DateTime foreach ($this->updatedData as $key => $val) { + if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ + if(isset($this->originalData[$key])) + $this->attributes[$key] = $this->originalData[$key]; + } + if (gettype($val) == 'object' && !method_exists($val, '__toString')) { unset($this->originalData[$key]); unset($this->updatedData[$key]); @@ -154,11 +160,6 @@ public function preSave() unset($this->attributes['keepRevisionOf']); $this->dirtyData = $this->getDirty(); - $this->updating = $this->exists; - - if($this->updating && $this->autoAccept == false){ - $this->attributes = $this->original; - } } } From 5b28a0c55a7364ffc705aaf0ade336481d96682d Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Fri, 21 Oct 2016 17:15:06 +0530 Subject: [PATCH 04/32] Not null should not be part of the revision --- src/Venturecraft/Revisionable/RevisionableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 5b2c2a55..82ede7f6 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -135,7 +135,7 @@ public function preSave() // so for now we drop any object based items, like DateTime foreach ($this->updatedData as $key => $val) { if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ - if(isset($this->originalData[$key])) + if(isset($this->originalData[$key]) and ($this->originalData[$key] != null OR $this->originalData[$key] != '')) $this->attributes[$key] = $this->originalData[$key]; } From 4b34a110d84925f7d87568dd68f9a77f51498dc9 Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Fri, 21 Oct 2016 18:10:15 +0530 Subject: [PATCH 05/32] Moving code from one method to other --- src/Venturecraft/Revisionable/RevisionableTrait.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 82ede7f6..a3d1baf3 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -134,11 +134,6 @@ public function preSave() // we can only safely compare basic items, // so for now we drop any object based items, like DateTime foreach ($this->updatedData as $key => $val) { - if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ - if(isset($this->originalData[$key]) and ($this->originalData[$key] != null OR $this->originalData[$key] != '')) - $this->attributes[$key] = $this->originalData[$key]; - } - if (gettype($val) == 'object' && !method_exists($val, '__toString')) { unset($this->originalData[$key]); unset($this->updatedData[$key]); @@ -202,6 +197,13 @@ public function postSave() 'updated_at' => new \DateTime(), 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) ); + if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ + if(isset($this->originalData[$key]) and ($this->originalData[$key] != null OR $this->originalData[$key] != '')){ + \Log::debug('Changing value for key '.$key); + $this->attributes[$key] = $this->originalData[$key]; + } + } + } if (count($revisions) > 0) { From 3c988830d54f91347cd998b3d288fc3fb278480a Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Sat, 22 Oct 2016 16:22:28 +0530 Subject: [PATCH 06/32] if its new record, then its probably we don't want to store it --- src/Venturecraft/Revisionable/RevisionableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index a3d1baf3..a640ddd4 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -320,7 +320,7 @@ private function changedRevisionableFields() // check that the field is revisionable, and double check // that it's actually new data in case dirty is, well, clean if ($this->isRevisionable($key) && !is_array($value)) { - if (!isset($this->originalData[$key]) || $this->originalData[$key] != $this->updatedData[$key]) { + if (isset($this->originalData[$key]) || $this->originalData[$key] != $this->updatedData[$key]) { $changes_to_record[$key] = $value; } } else { From b5c1c4db5db3625ee9bf0452b3b1c80cbf591786 Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Sat, 22 Oct 2016 16:48:34 +0530 Subject: [PATCH 07/32] Rollback some error --- src/Venturecraft/Revisionable/RevisionableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index a640ddd4..a3d1baf3 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -320,7 +320,7 @@ private function changedRevisionableFields() // check that the field is revisionable, and double check // that it's actually new data in case dirty is, well, clean if ($this->isRevisionable($key) && !is_array($value)) { - if (isset($this->originalData[$key]) || $this->originalData[$key] != $this->updatedData[$key]) { + if (!isset($this->originalData[$key]) || $this->originalData[$key] != $this->updatedData[$key]) { $changes_to_record[$key] = $value; } } else { From 394d9e02c41f1f26b35b6a6b05870b15c190a494 Mon Sep 17 00:00:00 2001 From: Daksh Mehta Date: Sun, 23 Oct 2016 00:08:53 +0530 Subject: [PATCH 08/32] Rollback code for autoAccept --- .../Revisionable/RevisionableTrait.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index a3d1baf3..c3dcb195 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -128,6 +128,7 @@ public function preSave() // if there's no revisionEnabled. Or if there is, if it's true $this->originalData = $this->original; + $this->updatedData = $this->attributes; $this->updating = $this->exists; @@ -155,6 +156,17 @@ public function preSave() unset($this->attributes['keepRevisionOf']); $this->dirtyData = $this->getDirty(); + + $changes_to_record = $this->changedRevisionableFields(); + foreach ($changes_to_record as $key => $value) { + if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ + if(isset($this->originalData[$key])){ + \Log::debug('Changing value for key '.$key); + $this->attributes[$key] = $this->originalData[$key]; + } + } + } + } } @@ -197,13 +209,6 @@ public function postSave() 'updated_at' => new \DateTime(), 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) ); - if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ - if(isset($this->originalData[$key]) and ($this->originalData[$key] != null OR $this->originalData[$key] != '')){ - \Log::debug('Changing value for key '.$key); - $this->attributes[$key] = $this->originalData[$key]; - } - } - } if (count($revisions) > 0) { From 486c897a8b465a893a231e79bc9d9c1b01f2317d Mon Sep 17 00:00:00 2001 From: Chaitali Mehta Date: Fri, 30 Mar 2018 10:23:38 +0530 Subject: [PATCH 09/32] AutoAccept added for revision creations enabled --- src/Venturecraft/Revisionable/RevisionableTrait.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index c3dcb195..412b77c6 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -256,6 +256,8 @@ public function postCreate() 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), + 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) + ); $revision = new Revision; From 1123eed32181ed55be5c28b9ee2000f73ba8cf7b Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 15:27:55 -0600 Subject: [PATCH 10/32] src is a psr directory, so get migrations out of there. --- .../2013_04_09_062329_create_revisions_table.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {src/migrations => migrations}/2013_04_09_062329_create_revisions_table.php (100%) diff --git a/src/migrations/2013_04_09_062329_create_revisions_table.php b/migrations/2013_04_09_062329_create_revisions_table.php similarity index 100% rename from src/migrations/2013_04_09_062329_create_revisions_table.php rename to migrations/2013_04_09_062329_create_revisions_table.php From 6e47c131a8792ce9d092898bd117f6f6aea40e88 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 15:29:17 -0600 Subject: [PATCH 11/32] namespace is directed at src by composer.json's autoload, not by file structure --- src/{Venturecraft/Revisionable => }/FieldFormatter.php | 0 src/{Venturecraft/Revisionable => }/Revision.php | 0 src/{Venturecraft/Revisionable => }/Revisionable.php | 0 src/{Venturecraft/Revisionable => }/RevisionableTrait.php | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/{Venturecraft/Revisionable => }/FieldFormatter.php (100%) rename src/{Venturecraft/Revisionable => }/Revision.php (100%) rename src/{Venturecraft/Revisionable => }/Revisionable.php (100%) rename src/{Venturecraft/Revisionable => }/RevisionableTrait.php (100%) diff --git a/src/Venturecraft/Revisionable/FieldFormatter.php b/src/FieldFormatter.php similarity index 100% rename from src/Venturecraft/Revisionable/FieldFormatter.php rename to src/FieldFormatter.php diff --git a/src/Venturecraft/Revisionable/Revision.php b/src/Revision.php similarity index 100% rename from src/Venturecraft/Revisionable/Revision.php rename to src/Revision.php diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Revisionable.php similarity index 100% rename from src/Venturecraft/Revisionable/Revisionable.php rename to src/Revisionable.php diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/RevisionableTrait.php similarity index 100% rename from src/Venturecraft/Revisionable/RevisionableTrait.php rename to src/RevisionableTrait.php From 2b9d6036d35d8d7b78ad4967102d9f883998ccc6 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 15:30:51 -0600 Subject: [PATCH 12/32] psr 4 compliant --- composer.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 52677d68..f6998924 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,7 @@ "illuminate/support": "~4.0|~5.0|~5.1" }, "autoload": { - "classmap": [ - "src/migrations" - ], - "psr-0": { + "psr-4": { "Venturecraft\\Revisionable": "src/" } } From 99df5e0d8a93a08a0f1a3969b315bf6f9c6b1bd8 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 15:35:54 -0600 Subject: [PATCH 13/32] adds laravel service provider and composer based auto-loader --- composer.json | 7 +++++++ src/ServiceProvider.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/ServiceProvider.php diff --git a/composer.json b/composer.json index f6998924..f37b6aac 100644 --- a/composer.json +++ b/composer.json @@ -22,5 +22,12 @@ "psr-4": { "Venturecraft\\Revisionable": "src/" } + }, + "extra": { + "laravel": { + "providers": [ + "Venturecraft\\Revisionable\\ServiceProvider" + ] + } } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 00000000..eceabc75 --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,32 @@ +loadMigrationsFrom(__DIR__ . '/../migrations'); + } + + /** + * Register the application services. + * + * @return void + */ + public function register(){ + + } + +} \ No newline at end of file From 84f6e8489e244bb4d496eac5b0e21220b6f2403f Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 15:41:42 -0600 Subject: [PATCH 14/32] psr-4 namespace should end with separator --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f37b6aac..66fe2945 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ }, "autoload": { "psr-4": { - "Venturecraft\\Revisionable": "src/" + "Venturecraft\\Revisionable\\": "src/" } }, "extra": { From 77654c781033892eb3ddcc054a09563beadaf938 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 15:46:04 -0600 Subject: [PATCH 15/32] update readme because migration path is not needed. psr-4/ServiceProvider is autoloaded and provides migration to artisan --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 0d7d3aaa..18f6fc20 100644 --- a/readme.md +++ b/readme.md @@ -40,10 +40,10 @@ Run composer update to download the package php composer.phar update ``` -Finally, you'll also need to run migration on the package (Laravel 5.x) +Finally, you'll also need to run migration on the package (Laravel 5.5^) ``` -php artisan migrate --path=vendor/venturecraft/revisionable/src/migrations +php artisan migrate ``` For Laravel 4.x users: From 97f62fb1dd787b676b9749d4c49450f15777b89a Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 16:36:03 -0600 Subject: [PATCH 16/32] fixes config for psr4 --- .../revisionable.php => config/main.php | 0 src/Revision.php | 4 +- src/Revisionable.php | 2 +- src/ServiceProvider.php | 4 ++ .../RevisionableServiceProvider.php | 42 ------------------- 5 files changed, 7 insertions(+), 45 deletions(-) rename src/config/revisionable.php => config/main.php (100%) delete mode 100644 src/Venturecraft/Revisionable/RevisionableServiceProvider.php diff --git a/src/config/revisionable.php b/config/main.php similarity index 100% rename from src/config/revisionable.php rename to config/main.php diff --git a/src/Revision.php b/src/Revision.php index 4b856207..33aef71a 100644 --- a/src/Revision.php +++ b/src/Revision.php @@ -235,10 +235,10 @@ public function userResponsible() ) { return $class::findUserById($this->user_id); } else { - $user_model = app('config')->get('auth.model'); + $user_model = config('auth.model'); if (empty($user_model)) { - $user_model = app('config')->get('auth.providers.users.model'); + $user_model = config('auth.providers.users.model'); if (empty($user_model)) { return false; } diff --git a/src/Revisionable.php b/src/Revisionable.php index 82348834..099b9f43 100644 --- a/src/Revisionable.php +++ b/src/Revisionable.php @@ -79,7 +79,7 @@ public static function boot() */ public static function newModel() { - $model = \Config::get('revisionable.model', 'Venturecraft\Revisionable\Revision'); + $model = config(ServiceProvider::SHORT_NAME .'.model'); return new $model; } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index eceabc75..f6591d8d 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -18,6 +18,10 @@ class ServiceProvider extends BaseServiceProvider{ */ public function boot(){ $this->loadMigrationsFrom(__DIR__ . '/../migrations'); + $this->publishes([__DIR__ . '/../config/main.php' => config_path(SELF::SHORT_NAME . '.php')]); + $this->mergeConfigFrom( + __DIR__ . '/../config/main.php', SELF::SHORT_NAME + ); } /** diff --git a/src/Venturecraft/Revisionable/RevisionableServiceProvider.php b/src/Venturecraft/Revisionable/RevisionableServiceProvider.php deleted file mode 100644 index fb9fec26..00000000 --- a/src/Venturecraft/Revisionable/RevisionableServiceProvider.php +++ /dev/null @@ -1,42 +0,0 @@ -publishes([ - __DIR__ . '/../../config/revisionable.php' => config_path('revisionable.php'), - ], 'config'); - - $this->publishes([ - __DIR__ . '/../../migrations/' => database_path('migrations'), - ], 'migrations'); - } - - /** - * Register the application services. - * - * @return void - */ - public function register() - { - } - - /** - * Get the services provided by the provider. - * - * @return string[] - */ - public function provides() - { - } -} From f0f6c4ac38b940a4b44cbe09df442487ea163c2a Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 16:37:10 -0600 Subject: [PATCH 17/32] removes shit file --- .styleci.yml | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .styleci.yml diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 247a09c5..00000000 --- a/.styleci.yml +++ /dev/null @@ -1 +0,0 @@ -preset: psr2 From b3a70d2af9aa77a58c7cbab1e6d6ffc0785e3792 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 16:50:29 -0600 Subject: [PATCH 18/32] check empty --- src/RevisionableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index 35a47df6..d9d2836f 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -160,7 +160,7 @@ public function preSave() $changes_to_record = $this->changedRevisionableFields(); foreach ($changes_to_record as $key => $value) { - if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ + if($this->updating && $this->autoAccept == false && !empty($this->keepRevisionOf) && in_array($key, $this->keepRevisionOf)){ if(isset($this->originalData[$key])){ \Log::debug('Changing value for key '.$key); $this->attributes[$key] = $this->originalData[$key]; From 26a67ac0e4fbecbcaee7865787cc31bd5cf05a8c Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 1 May 2018 17:03:26 -0600 Subject: [PATCH 19/32] adds missing migration --- ...revisionable_add_accepted_at_timestamp.php | 33 +++++++++++++++++++ src/RevisionableTrait.php | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 migrations/2018_05_01_114345_revisionable_add_accepted_at_timestamp.php diff --git a/migrations/2018_05_01_114345_revisionable_add_accepted_at_timestamp.php b/migrations/2018_05_01_114345_revisionable_add_accepted_at_timestamp.php new file mode 100644 index 00000000..38c56c8b --- /dev/null +++ b/migrations/2018_05_01_114345_revisionable_add_accepted_at_timestamp.php @@ -0,0 +1,33 @@ +timestamp('accepted_at')->after('new_value'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('revisions', function (Blueprint $table) { + // + }); + } +} diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index d9d2836f..691e61af 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -160,7 +160,7 @@ public function preSave() $changes_to_record = $this->changedRevisionableFields(); foreach ($changes_to_record as $key => $value) { - if($this->updating && $this->autoAccept == false && !empty($this->keepRevisionOf) && in_array($key, $this->keepRevisionOf)){ + if($this->updating && !empty($this->autoAccept) && $this->autoAccept == false && !empty($this->keepRevisionOf) && in_array($key, $this->keepRevisionOf)){ if(isset($this->originalData[$key])){ \Log::debug('Changing value for key '.$key); $this->attributes[$key] = $this->originalData[$key]; From e1844257b2d27ad3b26c342dcb66a6d7ee8395d0 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Wed, 2 May 2018 00:23:21 -0600 Subject: [PATCH 20/32] adds history view --- resources/views/history.blade.php | 7 +++++++ src/ServiceProvider.php | 3 +++ 2 files changed, 10 insertions(+) create mode 100644 resources/views/history.blade.php diff --git a/resources/views/history.blade.php b/resources/views/history.blade.php new file mode 100644 index 00000000..6a3ec64d --- /dev/null +++ b/resources/views/history.blade.php @@ -0,0 +1,7 @@ +@foreach($resource->revisionHistory as $history) + @if($history->key == 'created_at' && !$history->old_value) +
  • {{ $history->userResponsible()->first_name }} created this resource at {{ $history->newValue() }}
  • + @else +
  • {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}
  • + @endif +@endforeach \ No newline at end of file diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index f6591d8d..553ed85d 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -22,6 +22,9 @@ public function boot(){ $this->mergeConfigFrom( __DIR__ . '/../config/main.php', SELF::SHORT_NAME ); + + $this->loadViewsFrom(__DIR__ . '/../resources/views', self::SHORT_NAME); + $this->publishes([__DIR__ . '/../resources/views' => resource_path('views/vendor/' . self::SHORT_NAME)], 'views'); } /** From 263022554038aea9aa86d6c1201956f07a2a20b6 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Wed, 2 May 2018 00:24:21 -0600 Subject: [PATCH 21/32] adds history view --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d0d79073..818ca134 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, { "name": "Tarek Adam", - "email": "austin.stierler@gmail.com", + "email": "tarek.adam@gmail.com", "role": "Contributor" } ], From cf893c4c5ccc3ff5c69f6e82722f755245ff0250 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Wed, 2 May 2018 01:40:10 -0600 Subject: [PATCH 22/32] adds view --- config/main.php | 14 ++++++++------ resources/lang/en/validation.php | 6 ++++++ resources/views/history.blade.php | 6 +++--- routes/main.php | 13 +++++++++++++ src/Controller.php | 20 ++++++++++++++++++++ src/ServiceProvider.php | 9 +++++++++ 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 resources/lang/en/validation.php create mode 100644 routes/main.php create mode 100644 src/Controller.php diff --git a/config/main.php b/config/main.php index 4f6666b8..afa9ad89 100644 --- a/config/main.php +++ b/config/main.php @@ -1,10 +1,12 @@ Venturecraft\Revisionable\Revision::class, + /* + |-------------------------------------------------------------------------- + | Revision Model + |-------------------------------------------------------------------------- + */ + 'model' => Venturecraft\Revisionable\Revision::class, + 'route-prefix' => 'log', + 'middleware' => ['web', 'auth'], ]; diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php new file mode 100644 index 00000000..3b524522 --- /dev/null +++ b/resources/lang/en/validation.php @@ -0,0 +1,6 @@ + 'Invalid model.', +]; diff --git a/resources/views/history.blade.php b/resources/views/history.blade.php index 6a3ec64d..b2bc32d0 100644 --- a/resources/views/history.blade.php +++ b/resources/views/history.blade.php @@ -1,7 +1,7 @@ -@foreach($resource->revisionHistory as $history) +@foreach($model->revisionHistory as $history) @if($history->key == 'created_at' && !$history->old_value) -
  • {{ $history->userResponsible()->first_name }} created this resource at {{ $history->newValue() }}
  • +
    {{ $history->userResponsible()->first_name }} created this resource at {{ $history->newValue() }}
    @else -
  • {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}
  • +
    {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}
    @endif @endforeach \ No newline at end of file diff --git a/routes/main.php b/routes/main.php new file mode 100644 index 00000000..91441328 --- /dev/null +++ b/routes/main.php @@ -0,0 +1,13 @@ + 'Venturecraft\Revisionable', + 'as' => Venturecraft\Revisionable\ServiceProvider::SHORT_NAME . '::', + 'prefix' => config(Venturecraft\Revisionable\ServiceProvider::SHORT_NAME . '.route-prefix'), + 'middleware' => config(Venturecraft\Revisionable\ServiceProvider::SHORT_NAME . '.middleware')]; + +Route::group($config, function (){ + Route::get('{revisionable}/history', [ + 'as' => 'model-history', + 'uses' => 'Controller@modelHistory' + ]); +}); \ No newline at end of file diff --git a/src/Controller.php b/src/Controller.php new file mode 100644 index 00000000..6b84ee89 --- /dev/null +++ b/src/Controller.php @@ -0,0 +1,20 @@ +withModel($model); + } + +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 553ed85d..7820df8d 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,6 +1,9 @@ loadViewsFrom(__DIR__ . '/../resources/views', self::SHORT_NAME); $this->publishes([__DIR__ . '/../resources/views' => resource_path('views/vendor/' . self::SHORT_NAME)], 'views'); + + $this->loadRoutesFrom(__DIR__ . '/../routes/main.php'); + + $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', self::SHORT_NAME); + $this->publishes([__DIR__ . '/../resources/lang' => resource_path('lang/vendor/' . self::SHORT_NAME)], 'lang'); + } /** From b0459329736510cddaeef02e1bfabd55190d346b Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Wed, 2 May 2018 01:42:33 -0600 Subject: [PATCH 23/32] logic --- src/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller.php b/src/Controller.php index 6b84ee89..8f6d18dc 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -12,7 +12,7 @@ public function modelHistory($revisionable){ $key = array_pop($parts); $model = implode('\\', $parts); - $model = $model::find($key); + $model = $model::findOrFail($key); return view(ServiceProvider::SHORT_NAME . '::history')->withModel($model); } From f67b50a9d6df14d07e198650afe7c571ee69bd35 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Sun, 13 May 2018 11:21:34 -0600 Subject: [PATCH 24/32] tweaks --- src/RevisionableTrait.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index 691e61af..7de89754 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -85,6 +85,11 @@ public static function bootRevisionableTrait() }); } + public function getRevisionableRouteParamAttribute(){ + return strtolower(str_replace('\\', '-',static::class) .'-'. $this->getKey()); + } + + /** * @return mixed */ From 89f0590676089dfa9e41103b50444fc2cedae9c1 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Mon, 22 Oct 2018 16:17:01 -0600 Subject: [PATCH 25/32] tweaks --- resources/views/history.blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/history.blade.php b/resources/views/history.blade.php index b2bc32d0..11410d93 100644 --- a/resources/views/history.blade.php +++ b/resources/views/history.blade.php @@ -1,7 +1,7 @@ @foreach($model->revisionHistory as $history) - @if($history->key == 'created_at' && !$history->old_value) -
    {{ $history->userResponsible()->first_name }} created this resource at {{ $history->newValue() }}
    + @if($history->key == 'created_at' and !$history->old_value) +
    {{ (!empty($history->userResponsible()))? $history->userResponsible()->first_name . ' created' : 'system created' }} this resource at {{ $history->newValue() }}
    @else
    {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}
    @endif -@endforeach \ No newline at end of file +@endforeach From fd62545327fe835ae1c8def9a4ec733a063711d0 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Mon, 22 Oct 2018 16:17:26 -0600 Subject: [PATCH 26/32] tweaks --- resources/views/history.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/history.blade.php b/resources/views/history.blade.php index 11410d93..552e36ce 100644 --- a/resources/views/history.blade.php +++ b/resources/views/history.blade.php @@ -1,6 +1,6 @@ @foreach($model->revisionHistory as $history) @if($history->key == 'created_at' and !$history->old_value) -
    {{ (!empty($history->userResponsible()))? $history->userResponsible()->first_name . ' created' : 'system created' }} this resource at {{ $history->newValue() }}
    +
    {{ (!empty($history->userResponsible()))? $history->userResponsible()->first_name : 'system' }} created this resource at {{ $history->newValue() }}
    @else
    {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}
    @endif From 3cd6a6c26c2e17a1c1801c0a23759af6e95193b5 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Tue, 15 Oct 2019 12:06:36 -0600 Subject: [PATCH 27/32] removes compose requirements --- composer.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/composer.json b/composer.json index 818ca134..5838c8a6 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,6 @@ "source": "https://github.com/VentureCraft/revisionable" }, "require": { - "php": ">=5.4.0", - "illuminate/support": "~4.0|~5.0|~5.1" }, "autoload": { "psr-4": { From 71d7376411bdc32592392ddb766e40ae9eec8e9a Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Fri, 18 Oct 2019 00:33:03 -0600 Subject: [PATCH 28/32] L6 --- src/Revisionable.php | 3 ++- src/RevisionableTrait.php | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Revisionable.php b/src/Revisionable.php index 099b9f43..b9040b7d 100644 --- a/src/Revisionable.php +++ b/src/Revisionable.php @@ -1,6 +1,7 @@ $this->getMorphClass(), 'revisionable_id' => $this->getKey(), 'key' => $key, - 'old_value' => array_get($this->originalData, $key), + 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index 7de89754..acd18932 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -7,6 +7,8 @@ * */ +use Illuminate\Support\Arr; + /** * Class RevisionableTrait * @package Venturecraft\Revisionable @@ -163,7 +165,7 @@ public function preSave() $this->dirtyData = $this->getDirty(); - $changes_to_record = $this->changedRevisionableFields(); + $changes_to_record = $this->changedRevisionableFields(); foreach ($changes_to_record as $key => $value) { if($this->updating && !empty($this->autoAccept) && $this->autoAccept == false && !empty($this->keepRevisionOf) && in_array($key, $this->keepRevisionOf)){ if(isset($this->originalData[$key])){ @@ -208,7 +210,7 @@ public function postSave() 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), 'key' => $key, - 'old_value' => array_get($this->originalData, $key), + 'old_value' => Arr::get($this->originalData, $key), 'new_value' => $this->updatedData[$key], 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), @@ -225,14 +227,14 @@ public function postSave() ->orderBy('id','asc') ->limit(count($revisions)) ->get(); - + foreach($toDelete as $delete){ $delete->delete(); } } $revision = Revisionable::newModel(); \DB::table($revision->getTable())->insert($revisions); - \Event::fire('revisionable.saved', array('model' => $this, 'revisions' => $revisions)); + \Event::dispatch('revisionable.saved', array('model' => $this, 'revisions' => $revisions)); } } } From 9ce9b97a2c5b673df767c29542708a65f601255a Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Fri, 18 Oct 2019 00:45:36 -0600 Subject: [PATCH 29/32] refactor for L6 --- src/RevisionableTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index acd18932..a6c1f457 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -270,7 +270,7 @@ public function postCreate() $revision = Revisionable::newModel(); \DB::table($revision->getTable())->insert($revisions); - \Event::fire('revisionable.created', array('model' => $this, 'revisions' => $revisions)); + \Event::dispatch('revisionable.created', array('model' => $this, 'revisions' => $revisions)); } } @@ -296,7 +296,7 @@ public function postDelete() ); $revision = Revisionable::newModel(); \DB::table($revision->getTable())->insert($revisions); - \Event::fire('revisionable.deleted', array('model' => $this, 'revisions' => $revisions)); + \Event::dispatch('revisionable.deleted', array('model' => $this, 'revisions' => $revisions)); } } From 034873367989de6a0117c676b9f02d27ccf54d19 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Sat, 19 Oct 2019 16:58:43 -0600 Subject: [PATCH 30/32] upgrade studly case --- src/Controller.php | 3 ++- src/Revision.php | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Controller.php b/src/Controller.php index 8f6d18dc..e767d6b1 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -3,12 +3,13 @@ namespace Venturecraft\Revisionable; use App\Http\Controllers\Controller as BaseController; +use Illuminate\Support\Str; class Controller extends BaseController{ // public function modelHistory($revisionable){ $parts = explode('-', $revisionable); - $parts = array_map(function ($item){ return studly_case($item); }, $parts); + $parts = array_map(function ($item){ return Str::studly($item); }, $parts); $key = array_pop($parts); $model = implode('\\', $parts); diff --git a/src/Revision.php b/src/Revision.php index 33aef71a..e5fee3d2 100644 --- a/src/Revision.php +++ b/src/Revision.php @@ -2,8 +2,9 @@ namespace Venturecraft\Revisionable; -use Illuminate\Database\Eloquent\Model as Eloquent; -use Illuminate\Support\Facades\Log; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str; + /** * Revision. @@ -13,7 +14,7 @@ * * (c) Venture Craft */ -class Revision extends Eloquent +class Revision extends Model { /** * @var string @@ -165,7 +166,7 @@ private function getValue($which = 'new') // Check if model use RevisionableTrait if(method_exists($item, 'identifiableName')) { // see if there's an available mutator - $mutator = 'get' . studly_case($this->key) . 'Attribute'; + $mutator = 'get' . Str::studly($this->key) . 'Attribute'; if (method_exists($item, $mutator)) { return $this->format($item->$mutator($this->key), $item->identifiableName()); } @@ -181,7 +182,7 @@ private function getValue($which = 'new') // if there was an issue // or, if it's a normal value - $mutator = 'get' . studly_case($this->key) . 'Attribute'; + $mutator = 'get' . Str::studly($this->key) . 'Attribute'; if (method_exists($main_model, $mutator)) { return $this->format($this->key, $main_model->$mutator($this->$which_value)); } From 12f823e51d8c53e2fa79beb96e96c1e85360cc2e Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Wed, 30 Oct 2019 12:08:12 -0600 Subject: [PATCH 31/32] updates revisionable --- config/main.php | 16 ++++++------- resources/views/history.blade.php | 9 ++++++- src/Controller.php | 9 +------ src/Revision.php | 2 +- src/Revisionable.php | 2 +- src/RevisionableResolver.php | 40 +++++++++++++++++++++++++++++++ src/RevisionableTrait.php | 2 +- src/ServiceProvider.php | 5 +++- 8 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 src/RevisionableResolver.php diff --git a/config/main.php b/config/main.php index afa9ad89..2ee9fd81 100644 --- a/config/main.php +++ b/config/main.php @@ -1,12 +1,12 @@ Venturecraft\Revisionable\Revision::class, - 'route-prefix' => 'log', - 'middleware' => ['web', 'auth'], + /* + |-------------------------------------------------------------------------- + | Revision Model + |-------------------------------------------------------------------------- + */ + 'route-prefix' => 'revisions', + 'middleware' => ['web', 'auth'], + 'revisionable-model-binding' => [] ]; diff --git a/resources/views/history.blade.php b/resources/views/history.blade.php index 552e36ce..98ff5692 100644 --- a/resources/views/history.blade.php +++ b/resources/views/history.blade.php @@ -2,6 +2,13 @@ @if($history->key == 'created_at' and !$history->old_value)
    {{ (!empty($history->userResponsible()))? $history->userResponsible()->first_name : 'system' }} created this resource at {{ $history->newValue() }}
    @else -
    {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}
    +
    {{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from + {{ $history->oldValue() }} + @if(is_null($history->oldValue())) + null + @endif + + to {{ $history->newValue() }} +
    @endif @endforeach diff --git a/src/Controller.php b/src/Controller.php index e767d6b1..c29c784e 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -8,14 +8,7 @@ class Controller extends BaseController{ // public function modelHistory($revisionable){ - $parts = explode('-', $revisionable); - $parts = array_map(function ($item){ return Str::studly($item); }, $parts); - $key = array_pop($parts); - $model = implode('\\', $parts); - - $model = $model::findOrFail($key); - - return view(ServiceProvider::SHORT_NAME . '::history')->withModel($model); + return view(ServiceProvider::SHORT_NAME . '::history')->withModel($revisionable); } } diff --git a/src/Revision.php b/src/Revision.php index e5fee3d2..fd76d37d 100644 --- a/src/Revision.php +++ b/src/Revision.php @@ -99,7 +99,7 @@ private function formatFieldName($key) */ public function oldValue() { - return $this->getValue('old'); + $this->getValue('old'); } diff --git a/src/Revisionable.php b/src/Revisionable.php index b9040b7d..20e30bed 100644 --- a/src/Revisionable.php +++ b/src/Revisionable.php @@ -89,7 +89,7 @@ public static function newModel() */ public function revisionHistory() { - return $this->morphMany(get_class(static::newModel()), 'revisionable'); + return $this->morphMany(Revision::class, 'revisionable'); } /** diff --git a/src/RevisionableResolver.php b/src/RevisionableResolver.php new file mode 100644 index 00000000..da383877 --- /dev/null +++ b/src/RevisionableResolver.php @@ -0,0 +1,40 @@ +route = $route; + $this->binding_lookup = config(\Venturecraft\Revisionable\ServiceProvider::SHORT_NAME . '.revisionable-model-binding'); + } + + public function bind($revisionable){ + list($alias, $id)= explode('-', $revisionable); + if(empty($this->binding_lookup[$alias])){ + abort(404, 'No binding instruction found.'); + } + + $class = $this->binding_lookup[$alias]; + return $class::findOrFail($id); + } + +} diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index a6c1f457..435494bc 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -97,7 +97,7 @@ public function getRevisionableRouteParamAttribute(){ */ public function revisionHistory() { - return $this->morphMany(get_class(Revisionable::newModel()), 'revisionable'); + return $this->morphMany(Revision::class, 'revisionable'); } /** diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7820df8d..0b7c5046 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,6 +2,7 @@ namespace Venturecraft\Revisionable; +use FuquIo\LaravelDisks\Support\DownloadableResolver; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Validator as BaseValidator; use Illuminate\Support\ServiceProvider as BaseServiceProvider; @@ -34,6 +35,8 @@ public function boot(){ $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', self::SHORT_NAME); $this->publishes([__DIR__ . '/../resources/lang' => resource_path('lang/vendor/' . self::SHORT_NAME)], 'lang'); + Route::bind('revisionable', RevisionableResolver::class); + } /** @@ -45,4 +48,4 @@ public function register(){ } -} \ No newline at end of file +} From 6acbe4cd2792316763dfcbc558a070103a0dcff4 Mon Sep 17 00:00:00 2001 From: Tarek Adam Date: Wed, 30 Oct 2019 13:20:12 -0600 Subject: [PATCH 32/32] fix revisionable shit --- src/Revision.php | 6 ++++++ src/Revisionable.php | 18 ++++++++++-------- src/RevisionableTrait.php | 12 ++++++------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Revision.php b/src/Revision.php index fd76d37d..d0909b0e 100644 --- a/src/Revision.php +++ b/src/Revision.php @@ -2,6 +2,7 @@ namespace Venturecraft\Revisionable; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; @@ -23,6 +24,7 @@ class Revision extends Model protected $dates = ['accepted_at']; + /** * @var array */ @@ -34,6 +36,10 @@ class Revision extends Model public function __construct(array $attributes = array()) { parent::__construct($attributes); + + if(empty($this->attributes['accepted_at'])){ + $this->attributes['accepted_at'] = Carbon::now(); + } } /** diff --git a/src/Revisionable.php b/src/Revisionable.php index 20e30bed..b09fb07c 100644 --- a/src/Revisionable.php +++ b/src/Revisionable.php @@ -75,14 +75,16 @@ public static function boot() }); } /** + * @deprecated + * * Instance the revision model * @return \Illuminate\Database\Eloquent\Model */ - public static function newModel() - { - $model = config(ServiceProvider::SHORT_NAME .'.model'); - return new $model; - } +// public static function newModel() +// { +// $model = config(ServiceProvider::SHORT_NAME .'.model'); +// return new $model; +// } /** * @return mixed @@ -163,7 +165,7 @@ public function postSave() } if (count($revisions) > 0) { - $revision = static::newModel(); + $revision = new Revision; \DB::table($revision->getTable())->insert($revisions); } } @@ -196,7 +198,7 @@ public function postCreate() 'updated_at' => new \DateTime(), ); - $revision = static::newModel(); + $revision = new Revision; \DB::table($revision->getTable())->insert($revisions); } } @@ -219,7 +221,7 @@ public function postDelete() 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); - $revision = static::newModel(); + $revision = new Revision; \DB::table($revision->getTable())->insert($revisions); } } diff --git a/src/RevisionableTrait.php b/src/RevisionableTrait.php index 435494bc..7590353b 100644 --- a/src/RevisionableTrait.php +++ b/src/RevisionableTrait.php @@ -120,7 +120,7 @@ public function scopeHasPendingRevisionHistory($query) */ public static function classRevisionHistory($limit = 100, $order = 'desc') { - $model = Revisionable::newModel(); + $model = new Revision; return $model->where('revisionable_type', get_called_class()) ->orderBy('updated_at', $order)->limit($limit)->get(); } @@ -215,7 +215,7 @@ public function postSave() 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), - 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) + 'accepted_at' => new \DateTime() ); } @@ -232,7 +232,7 @@ public function postSave() $delete->delete(); } } - $revision = Revisionable::newModel(); + $revision = new Revision; \DB::table($revision->getTable())->insert($revisions); \Event::dispatch('revisionable.saved', array('model' => $this, 'revisions' => $revisions)); } @@ -264,11 +264,11 @@ public function postCreate() 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), - 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) + 'accepted_at' => new \DateTime() ); - $revision = Revisionable::newModel(); + $revision = new Revision; \DB::table($revision->getTable())->insert($revisions); \Event::dispatch('revisionable.created', array('model' => $this, 'revisions' => $revisions)); } @@ -294,7 +294,7 @@ public function postDelete() 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), ); - $revision = Revisionable::newModel(); + $revision = new Revision; \DB::table($revision->getTable())->insert($revisions); \Event::dispatch('revisionable.deleted', array('model' => $this, 'revisions' => $revisions)); }