feat(concern): hand a call context to context-aware filters - #20
Merged
Conversation
- `Model.filterable(params, user_id: current_user.id)` — the context is an optional positional hash (not kwargs: **context would swallow the historical single-hash call `filterable(filters: ...)`), passed as given to every filter whose callable accepts a third argument; two-argument filters, all built-ins included, never see it - same dispatch for applied, so a context-aware filter reports consistently; context defaults to an empty hash - an unsatisfiable filter (missing context) stays a no-op, never a `none` — the filterable-by footgun flagged in the gem review
📊 Code Quality Report
🔍 3 new reek smells introduced — review below. 🔍 3 new reek smells introduced by this PR
🔍 All 78 reek smells on this PR
📊 Flog complexity breakdown📊 Flay duplication breakdown |
🛡️ RuboCop: 0 offenses (0)
✅ No RuboCop offenses. |
🛡️ Documentation Quality: 100.0% (0.0%)
✅ Documentation quality above threshold. Changed files
|
🛡️ Coverage Report: 100.0% (0.0%)
✅ All changes are tested. Changed files
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Last item of the gem-landscape backlog (the filterable-by pattern):
Model.filterable(params, user_id: current_user.id)hands a caller-provided context to custom filters, so per-user filters (mine=1) need no global state.Changes
filterable/applied_filters— deliberately not kwargs:**contextwould swallow the historical single-hash callfilterable(filters: ...)(Ruby keyword-capture pitfall)|filters, scope, context|, objectcall(f, s, c = {})); two-argument filters — all built-ins — are called exactly as before; same dispatch forapplied, so context-aware filters report consistently{}; an unsatisfiable filter stays a no-op, never anone(the filterable-by footgun flagged in the review)How to Test
bundle exec rakemodel.add_filter { |f, scope, ctx| scope.where(author_id: ctx[:user_id]) if f[:mine] }puismodel.filterable({ filters: { mine: '1' } }, user_id: 42)→ filtré ; sans contexte → no-op ; les appels existantsfilterable(filters: ...)inchangés (la suite entière le prouve)