Make search_form_* helpers honour per-search search_key (#1118)#1676
Open
ryoya1122 wants to merge 1 commit into
Open
Make search_form_* helpers honour per-search search_key (#1118)#1676ryoya1122 wants to merge 1 commit into
ryoya1122 wants to merge 1 commit into
Conversation
…h search_key (activerecord-hackery#1118) `sort_link` and `sort_url` already read the search key from the search's context (`search.context.search_key`). The form-building helpers were instead falling back to the global `Ransack.options[:search_key]` in `finalize_form_options` / `finalize_form_with_options`, which meant that passing `search_key:` to `Model.ransack` was silently ignored: inputs were emitted under `q[...]` regardless of the override. The two private finalisers now take the `search` object and use `search.context.search_key`. `Context#search_key` already falls back to `Ransack.options[:search_key]` when no per-search override is given, so the global default continues to apply for the unconfigured case.
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.
Closes #1118.
Problem
When a
search_key:is passed toModel.ransack, the form-building helpers ignore it. The inputs are emitted under the global default key (q[...]) regardless of the override:The same applies to
search_form_withandturbo_search_form_for.sort_linkandsort_urlalready read the key from the search's context, so the form path is the outlier.Cause
The two private finalisers used the global default instead of asking the search:
The reporter linked both lines in #1118.
Fix
finalize_form_options/finalize_form_with_optionsnow take thesearchand readsearch.context.search_key, the same sourcesort_link/sort_urlalready use (seelib/ransack/helpers/form_helper.rb#L200and#L249).Context#search_keyis initialised asoptions[:search_key] || Ransack.options[:search_key], so the global default still applies when no per-search override is given.Compatibility
Model.ransackcallPerson.ransack(...)q[...](global default)q[...](unchanged)Person.ransack(...)afterRansack.configure { ... :example }example[...](global)example[...](unchanged)Person.ransack(..., search_key: :people_search)q[...]❌ (#1118)people_search[...]✓options[:as]/options[:scope]explicitly set by callerThe
finalize_form_*methods areprivate, so the signature change is internal.Tests
Two new specs in
spec/ransack/helpers/form_helper_spec.rb, alongside the existing global-default tests, both red before the fix and green after:#search_form_for with per-search search_key-> expectspeople_search_name_eq#search_form_with with per-search search_key-> expectspeople_search[name_eq]Full suite:
514 examples, 0 failures, 1 pendingonv5.0.0+ this branch (SQLite, ActiveRecord 7.2.3.1, Ruby 3.4.9).Targets
v5.0.0per #1640.