Skip to content

Commit 4d046e5

Browse files
committed
Override the models the prune command looks for
1 parent 7795f80 commit 4d046e5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Support/Config.php

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

55
class Config
66
{
7+
protected static \Closure $callback;
8+
9+
public static function resolvePrunableModelsUsing(\Closure $callback)
10+
{
11+
static::$callback = $callback;
12+
}
13+
714
public static function getPrunableModels(): array
815
{
16+
if(isset(static::$callback)) {
17+
return call_user_func(static::$callback);
18+
}
919
return config('prunable-fields.models', []);
1020
}
1121
}

tests/MassPrunableFieldsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@
5656
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === MassPrunableUser::class
5757
);
5858
})->with('user_with_mass_prunable_fields');
59+
60+
test('should allow the prunable models to be overridden at runtime', function(MassPrunableUser $model) {
61+
\Maize\PrunableFields\Support\Config::resolvePrunableModelsUsing(fn() => [MassPrunableUser::class]);
62+
63+
Event::fake();
64+
65+
pruneFields([]);
66+
67+
Event::assertDispatched(
68+
ModelsFieldsPruned::class,
69+
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === MassPrunableUser::class
70+
);
71+
72+
})->with('user_with_mass_prunable_fields');

0 commit comments

Comments
 (0)