Repository navigation
Add default_filters option - #726
Conversation
shunjilin
left a comment
There was a problem hiding this comment.
Thank you for this PR, the implementation seems sound and I have a similar use case for setting defaults on the backend as well. 🙏
|
@timothyvanderaerden Thanks for opening the PR! There are two issues:
All other defaults end up in the Flop struct, but here I'm not sure. I've added this to 0.30.0, will get back to it when I start with that version. |
Both issues are addressed in e028596:
Let me know if this is the outcome you had in mind, or if you'd prefer a different approach. |
|
Yes, thank you. I'll leave this PR open for a bit, I'm planning to release one patch version before the next minor. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #726 +/- ##
==========================================
+ Coverage 89.25% 89.37% +0.12%
==========================================
Files 16 16
Lines 1163 1459 +296
==========================================
+ Hits 1038 1304 +266
- Misses 125 155 +30 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
default_filter optiondefault_filters option
|
Thank you! |
Adds a
:default_filtersoption that can be set on a schema or passed to aquery function. It takes a list of
Flop.Filterstructs thatFlop.validate/2applies when the parameters contain no filter with a value for the respective
field.
Behavior
with a value for a field, the defaults for that field are not applied,
regardless of the operator used by the client filter. Filters for other
fields are combined with the defaults as usual.
are valid.
value of
"8"for an integer field results in a value of8.compiled; an invalid default raises
Flop.InvalidConfigError. Defaults on:binary_idfields and on fields that take their type from another schemamodule are cast at runtime, since their type depends on the repo or on
another module. Default filters passed at the call site are cast at runtime.
Invalid defaults cast at runtime raise an
ArgumentError.Flop.validate/2raises if a default filter refers to a field that is notfilterable, e.g. because the filterable fields were narrowed with the
:filterableoption.replace_invalid_params: true), so a removed filter does not suppress thedefault for its field.
max_filterslimit. Filters thatare identical to a default filter (e.g. resubmitted from a validated
Flopstruct) don't count either. The comparison is made per filter, so multiple
defaults on the same field, e.g. a range, keep working when the client
changes one bound.
default_filters: falseat the call site.:default_order, the option names fields, so it cannot be set in theapplication environment or on a backend module.
Flop.Filter.new/1can be used to build the list from a map of field names tovalues, applying the default
:==operator:Notes
filtering: false, filter parameters are ignored, but default filtersare still applied, mirroring how the default order is applied with
ordering: false.Flopstruct, they alsoshow up in
Flop.Meta.flop.Flop.Schemareads thefield types from Ecto's
@ecto_fieldsattribute, since__schema__/2is notavailable yet while the module is compiled.