Skip to content

Commit de1939a

Browse files
author
Riccardo Dalla Via
authored
Merge pull request #2 from tobytwigger/override-config
Override the models the prune command looks for
2 parents 7da7a88 + d7cfdc5 commit de1939a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Support/Config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@
44

55
class Config
66
{
7+
/**
8+
* @var callable|null
9+
*/
10+
protected static $callback;
11+
12+
public static function resolvePrunableModelsUsing(?callable $callback): void
13+
{
14+
static::$callback = $callback;
15+
}
16+
717
public static function getPrunableModels(): array
818
{
19+
if (is_callable(static::$callback)) {
20+
return call_user_func(static::$callback);
21+
}
22+
923
return config('prunable-fields.models', []);
1024
}
1125
}

tests/PrunableFieldsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\Facades\Event;
44
use Illuminate\Support\Facades\Log;
55
use Maize\PrunableFields\Events\ModelsFieldsPruned;
6+
use Maize\PrunableFields\Support\Config;
67
use Maize\PrunableFields\Tests\Events\UserUpdatedEvent;
78
use Maize\PrunableFields\Tests\Models\PrunableUser;
89

@@ -67,3 +68,18 @@
6768
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === PrunableUser::class
6869
);
6970
})->with('user_with_prunable_fields');
71+
72+
test('should allow the prunable models to be overridden at runtime', function (PrunableUser $model) {
73+
Config::resolvePrunableModelsUsing(fn () => [PrunableUser::class]);
74+
75+
Event::fake();
76+
77+
pruneFields([]);
78+
79+
Event::assertDispatched(
80+
ModelsFieldsPruned::class,
81+
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === PrunableUser::class
82+
);
83+
84+
Config::resolvePrunableModelsUsing(null);
85+
})->with('user_with_prunable_fields');

0 commit comments

Comments
 (0)