Skip to content

Skip Proxy#all memoization when params are given - #107

Merged
tagliala merged 1 commit into
masterfrom
fix-proxy-all-memoization
Oct 28, 2026
Merged

tagliala merged 1 commit into
masterfrom
fix-proxy-all-memoization

Conversation

@tagliala

Copy link
Copy Markdown
Member

Fix #50

Proxy#all memoized the first result and ignored the params of later calls on the same proxy. #count reused the memo the same way, and #find overwrote it, so a later #all returned the found record.

The memo exists so #each, #count and #respond_to? do not repeat requests. All of them call #all/#count without params, so the memo stays for that case. With params, #all and #count query the server and leave the memoized result untouched. #find no longer stores its result.

Repeated all(page: 2) calls now make a request each time.

Verification: bundle exec rspec (53 examples) and bundle exec rubocop pass. The Appraisals matrix and a real Kaminari/pagy integration were not run. No UI or AI change, so no screenshots.

🤖 Generated with Claude Code

@tagliala
tagliala force-pushed the fix-proxy-all-memoization branch from d124c6d to e390c8d Compare October 26, 2026 18:44
@tagliala
tagliala requested a balanced review from Copilot October 28, 2026 07:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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#all now only memoizes @result when no params are given; with params it issues a fresh klass.all each time.
  • Proxy#find no longer stores its result into @result, and Proxy#count now merges/forwards params (guarding against non-hash args like :all) instead of always reusing the memo.
  • Proxy::Void#all is overridden to keep returning [] without a request, since the base #all would otherwise hit the network when params are supplied.
  • Adds a new spec/hawk/model/proxy_spec.rb covering #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

Comment thread spec/hawk/model/proxy_spec.rb Resolved
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>
@tagliala
tagliala force-pushed the fix-proxy-all-memoization branch from e390c8d to 4e9310f Compare October 28, 2026 07:56
Copilot AI balanced review requested due to automatic review settings October 28, 2026 07:56
@tagliala
tagliala merged commit af1429a into master Oct 28, 2026
31 checks passed
@tagliala
tagliala deleted the fix-proxy-all-memoization branch October 28, 2026 07:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active issues remain, as the memoization fix is well-scoped and backed by new specs covering the param-aware behavior and void-proxy guarantees.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memoization of all may cause issues

2 participants