Skip to content

Commit

Permalink
Don’t use closures in config file so config:cache works
Browse files Browse the repository at this point in the history
shouldLogChange() should be used per-model to control more specific logging
  • Loading branch information
weotch committed Dec 21, 2017
1 parent 175b286 commit bd46e5e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
16 changes: 16 additions & 0 deletions classes/Models/Traits/Loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Bkwld\Decoy\Models\Traits;

// Deps
use App;
use Bkwld\Decoy\Models\Admin;
use Bkwld\Decoy\Models\Change;
use Bkwld\Decoy\Models\Image;
use Decoy;
use Illuminate\Database\Eloquent\Builder;

/**
Expand Down Expand Up @@ -57,6 +59,20 @@ public static function bootLoggable()
});
}

/**
* Should this model log it's changes. Defaults to true if the change
* happened while handling an admin request or via the console but not
* during a non-http unit test.
*
* @param string $action Like "deleted", "updated", etc
* @return boolean
*/
public function shouldLogChange($action)
{
return Decoy::handling()
|| (App::runningInConsole() && request()->path() == '/');
}

/**
* Show trashed models for matching change
*
Expand Down
15 changes: 9 additions & 6 deletions classes/Observers/Changes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Bkwld\Decoy\Observers;

use Event;
use Route;
use Config;
// Deps
use Bkwld\Decoy\Models;
use Bkwld\Decoy\Models\Change;
use Event;
use Route;

/**
* Create a log of all model changing events
Expand Down Expand Up @@ -67,12 +67,15 @@ public function handle($event, $payload)

// If `log_changes` was configed as a callable, see if this model event
// should not be logged
if ($check = Config::get('decoy.site.log_changes')) {
if ($check = config('decoy.site.log_changes')) {
if (is_bool($check) && !$check) {
return;
}
if (is_callable($check) && !call_user_func($check, $model, $action, $admin)) {
return;
if (is_callable($check)) {
\Log::error('Callable log_changes have been deprecated');
if (!call_user_func($check, $model, $action, $admin)) {
return;
}
}
} else {
return;
Expand Down
17 changes: 4 additions & 13 deletions config/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,11 @@
'auto_localize_root_models' => true,

/**
* Store an entry in the database of all model changes.
* Store an entry in the database of all model changes. Also see the
* shouldLogChange() function that can be overriden per-model
*
* @var boolean|callable
*
* If a function, it's signature is:
*
* @param Illuminate\Database\Eloquent\Model $model The model being touched
* @param string $action Generally a CRUD verb: "created", "updated", "deleted"
* @param Bkwld\Decoy\Models\Admin $admin The admin acting on the record
* @return boolean
* @var boolean
*/
// 'log_changes' => true,
'log_changes' => function ($model, $action, $admin_id) {
return Decoy::handling();
},
'log_changes' => true,

];
19 changes: 5 additions & 14 deletions example/config/decoy/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,11 @@
'auto_localize_root_models' => false,

/**
* Store an entry in the database of all model changes.
* Store an entry in the database of all model changes. Also see the
* shouldLogChange() function that can be overriden per-model
*
* @var boolean|callable
*
* If a function, it's signature is:
*
* @param Illuminate\Database\Eloquent\Model $model The model being touched
* @param string $action Generally a CRUD verb: "created", "updated", "deleted"
* @param Bkwld\Decoy\Models\Admin $admin The admin acting on the record
* @return boolean
* @var boolean
*/
// 'log_changes' => true,
'log_changes' => function ($model, $action, $admin_id) {
return true;
},

'log_changes' => true,

];

0 comments on commit bd46e5e

Please sign in to comment.