Skip to content

Accept strong parameters in the node builders; follow-ups to #1804 - #1805

Merged
scarroll32 merged 3 commits into
mainfrom
strong-params-in-builders
Nov 16, 2026
Merged

scarroll32 merged 3 commits into
mainfrom
strong-params-in-builders

Conversation

@scarroll32

@scarroll32 scarroll32 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

What this changes

Follow-ups found while reviewing #1804. This branch is stacked on that PR's commit, because every change here touches the lines it re-indents, so #1804 should merge first; until it does, its whitespace fix shows up in this diff too.

Strong parameters are accepted by the builders. Search#initialize unwraps ActionController::Parameters, but nodes built afterwards go through Node#indifferent_params, which only took objects responding to with_indifferent_access:

Person.ransack.build_grouping(ActionController::Parameters.new(name_eq: 'x'))
# => Ransack::InvalidSearchError: Invalid argument (ActionController::Parameters) supplied for a grouping
Person.ransack.build_sort(ActionController::Parameters.new(name: 'name', dir: 'desc'))
# => Ransack::InvalidSearchError: Invalid argument (ActionController::Parameters) supplied for a sort

Both now build the grouping or sort. Before #1763 the same calls raised NoMethodError, so this was never a regression. The unwrap does not widen authorization: a model's allowlist still applies, and whether a search of a model with no allowlist counts as permitted is still decided only by the params the search was created from. Both are covered by specs.

Error message grammar. indifferent_params built its message as supplied for a #{node_name}, which gave supplied for a attribute. Callers now pass the article, as scalar_param's already do. The other messages are unchanged.

Layout/DefEndAlignment is enabled. RuboCop runs with DisabledByDefault: true and nothing checked that end lines up with its def, so the misindentation #1804 fixes passed CI. This cop flags it on main and has no other offenses in lib or spec.

The blank line before the end of Ransack::Nodes::Node is removed.

lib/ransack/invalid_search_error.rb is removed. It was a one-line shim that required ransack/errors, where the error classes live.

Backward compatibility

Removing the shim breaks require 'ransack/invalid_search_error', which has worked since 4.3.0. Code that requires that file directly needs require 'ransack' or require 'ransack/errors' instead. Ransack::InvalidSearchError itself is unchanged. There is no entry for this in the upgrading guide yet, because it has no section for the next release.

Not changed

The setters (sorts=, groupings=, conditions=, attributes=, values=) still take only an Array or Hash, so strong parameters assigned to them directly are not unwrapped.

Checklist

  • Specs cover the change, and fail without it
  • bundle exec rake spec passes
  • Docs under docs/ are updated for any user-visible change
  • Anything that breaks backward compatibility is called out above

The five new strong-parameter examples and the new message assertion fail without the lib change. Full suite: 1174 examples, 0 failures on SQLite with Active Record 8.1.4.1; PostgreSQL and MySQL were not run locally.

🤖 Generated with Claude Code

scarroll32 and others added 3 commits November 16, 2026 15:57
`Search#initialize` unwraps `ActionController::Parameters`, but the nodes
built afterwards went through `Node#indifferent_params`, which only took
objects responding to `with_indifferent_access`. `build_grouping` and
`build_sort` given strong parameters raised `InvalidSearchError`. They are
now unwrapped there too. Whether the search counts as permitted is still
decided only by the params it was created from.

The callers now pass the article, as `scalar_param`'s do, so the message
reads "supplied for an attribute" rather than "a attribute". Also drops the
blank line left before the end of the class.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
RuboCop runs with every cop disabled by default and none of the enabled
ones checked that `end` lines up with its `def`, which is how the
misindented methods in Ransack::Nodes::Node got through.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The error classes live in ransack/errors, which `require 'ransack'` loads.
Code that requires `ransack/invalid_search_error` directly needs to require
`ransack` or `ransack/errors` instead.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@scarroll32
scarroll32 force-pushed the strong-params-in-builders branch from 65a6e32 to 472d4cc Compare November 16, 2026 14:57
@scarroll32
scarroll32 merged commit bd59068 into main Nov 16, 2026
10 checks passed
@scarroll32
scarroll32 deleted the strong-params-in-builders branch November 16, 2026 14:59
@scarroll32 scarroll32 mentioned this pull request Nov 20, 2026
2 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant