mitydigital / statamic-scheduled-cache-invalidator

MIT License
2 stars 3 forks source link

Support the addition of a query scope #2

Closed ryanmitchell closed 8 months ago

ryanmitchell commented 8 months ago

Requires https://github.com/statamic/cms/pull/5927

This PR adds a config statamic-scheduled-cache-invalidator.query_scope that accepts a snake cased query scope defined by the developer. This allows the query to be extended with other conditions, for example an end date.

The query scope receives both collection and now in the values param, which it may use to determine what restrictions to place.

martyf commented 8 months ago

Thanks Ryan - in the event of a user wanting to use multiple scopes (i.e. different rules for different collections), would it make sense for the config param to be an array, with the key the collection handle, and the value the scope as you've done?

Such as:

'query_scopes' => [
    'blog' => 'my_blog_scope',
    'news' => 'my_news_scope',
],

The only other thought would be to also allow passing the class name, and having the addon translate that accordingly. Such as:

'query_scopes' => [
    'blog' => \App\Scopes\MyBlogScope::class,
    'news' => \App\Scopes\MyNewsScope::class,
],

Then calling with:

app($scope)->apply($query, $feedConfig);

I went down this path with Feedamic (see the latest update) as I felt it was neater - could just be my personal opinion on that though 😉 I like this because it means within the IDE I can click on the scope to jump to it.

Thoughts?

ryanmitchell commented 8 months ago

Yep makes sense, and avoids the need to wait for that PR. Updated!

martyf commented 8 months ago

You're a legend, thanks @ryanmitchell!

martyf commented 8 months ago

Just a note @ryanmitchell, after merging I tweaked the behaviour for "all" vs collection-specific.

For all:

'query_scopes' => \Path\To\Scope::class

For collections:

'query_scopes' => [
    'collection_a' => \Path\To\Scope::class,
    'collection_b' => \Path\To\DifferentScope::class
],

Just in case for some bizarre reason someone has a collection called "all". Me being glass half empty here ha