Repository navigation
Skip Proxy#all memoization when params are given - #107
Conversation
d124c6d to
e390c8d
Compare
There was a problem hiding this comment.
🟡 Changes recommended
A remaining nit finding reports that the new test's setup fails to exercise the populated-memo branch the change introduces, leaving that key behavior untested.
1 open issue
What changed in this PR
This PR fixes issue #50, where Proxy#all memoized its first result and then ignored the query params of subsequent calls on the same proxy. Because #count reused that same memo and #find overwrote it, callers could get stale or wrong data (e.g. a later all(page: 2) returning page 1, or an all after find returning the found record). The fix preserves memoization only for the param-less case (used by #each, #count, and #respond_to?), while param-bearing calls query the server without touching or reusing the memo.
Changes:
Proxy#allnow only memoizes@resultwhen no params are given; with params it issues a freshklass.alleach time.Proxy#findno longer stores its result into@result, andProxy#countnow merges/forwards params (guarding against non-hash args like:all) instead of always reusing the memo.Proxy::Void#allis overridden to keep returning[]without a request, since the base#allwould otherwise hit the network when params are supplied.- Adds a new
spec/hawk/model/proxy_spec.rbcovering#all,#count, and void behavior.
| File | Description |
|---|---|
lib/hawk/model/proxy.rb |
Scopes #all memoization to the no-params case, stops #find from caching, forwards params in #count with a non-hash guard, and adds a Void#all override to avoid requests. |
spec/hawk/model/proxy_spec.rb |
New specs for param-aware #all/#count, the #find-then-#all fix, and void proxy behavior. |
One test-coverage note was left inline: the #count "params differ from the loaded result" test uses proxy.all(page: 1), which (correctly, per this change) does not populate @result, so the intended "populated memo + differing params" branch isn't actually exercised.
Review effort: Balanced
Proxy#all memoized its result in `@result` on first call, so a second call on the same proxy with different params returned the first result without making a request. #count had the same flaw, and #find overwrote `@result`, so a later #all returned the found record instead of a collection. The memoization exists to avoid duplicate requests when a proxy is enumerated, counted or probed with respond_to? repeatedly, and all of those call #all or #count without params. Keep it for that case and make #all and #count with params always query the server, leaving the memoized result untouched. #find no longer stores its result in `@result`. Void proxies keep returning their fixed empty result, with or without params. Fix #50 Co-Authored-By: Claude Opus <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
e390c8d to
4e9310f
Compare

Fix #50
Proxy#allmemoized the first result and ignored the params of later calls on the same proxy.#countreused the memo the same way, and#findoverwrote it, so a later#allreturned the found record.The memo exists so
#each,#countand#respond_to?do not repeat requests. All of them call#all/#countwithout params, so the memo stays for that case. With params,#alland#countquery the server and leave the memoized result untouched.#findno longer stores its result.Repeated
all(page: 2)calls now make a request each time.Verification:
bundle exec rspec(53 examples) andbundle exec rubocoppass. The Appraisals matrix and a real Kaminari/pagy integration were not run. No UI or AI change, so no screenshots.🤖 Generated with Claude Code