From f23e69a6fa6faa8e9af1059b31dafdbfde1e405b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:13:51 +0000 Subject: [PATCH 1/3] Initial plan From 9b8c0f934fc1cf3e88bc4db69ef0b95ae4441fcf Mon Sep 17 00:00:00 2001 From: Sean <11340230+scarroll32@users.noreply.github.com> Date: Wed, 24 Sep 2025 23:15:02 +0200 Subject: [PATCH 2/3] Add search_form_with helper --- lib/ransack/helpers/form_helper.rb | 27 +++++++++ spec/ransack/helpers/form_helper_spec.rb | 74 ++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 08d69bb18..ce2665f5b 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -14,6 +14,27 @@ def search_form_for(record, options = {}, &proc) form_for(record, options, &proc) end + # +search_form_with+ + # + # <%= search_form_with(model: @q) do |f| %> + # + def search_form_with(record_or_options = {}, options = {}, &proc) + if record_or_options.is_a?(Hash) && record_or_options.key?(:model) + # Called with keyword arguments: search_form_with(model: @q) + options = record_or_options + record = options.delete(:model) + else + # Called with positional arguments: search_form_with(@q) + record = record_or_options + end + + search = extract_search_and_set_url(record, options, 'search_form_with') + options[:html] ||= {} + html_options = build_html_options(search, options, :get) + finalize_form_with_options(options, html_options) + form_with(model: search, **options, &proc) + end + # +turbo_search_form_for+ # # <%= turbo_search_form_for(@q) do |f| %> @@ -108,6 +129,12 @@ def finalize_form_options(options, html_options) options[:builder] ||= FormBuilder end + def finalize_form_with_options(options, html_options) + options[:scope] ||= Ransack.options[:search_key] + options[:html].reverse_merge!(html_options) + options[:builder] ||= FormBuilder + end + def options_for(record) record.map { |r| parse_record(r) } end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 7bb016227..f0b78d407 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -859,6 +859,58 @@ module Helpers it { should match /example_name_eq/ } end + describe '#search_form_with with default format' do + subject { @controller.view_context + .search_form_with(model: Person.ransack) {} } + it { should match /action="\/people"/ } + end + + describe '#search_form_with with pdf format' do + subject { + @controller.view_context + .search_form_with(model: Person.ransack, format: :pdf) {} + } + it { should match /action="\/people.pdf"/ } + end + + describe '#search_form_with with json format' do + subject { + @controller.view_context + .search_form_with(model: Person.ransack, format: :json) {} + } + it { should match /action="\/people.json"/ } + end + + describe '#search_form_with with an array of routes' do + subject { + @controller.view_context + .search_form_with(model: [:admin, Comment.ransack]) {} + } + it { should match /action="\/admin\/comments"/ } + end + + describe '#search_form_with with custom default search key' do + before do + Ransack.configure { |c| c.search_key = :example } + end + after do + Ransack.configure { |c| c.search_key = :q } + end + subject { + @controller.view_context + .search_form_with(model: Person.ransack) { |f| f.text_field :name_eq } + } + it { should match /example\[name_eq\]/ } + end + + describe '#search_form_with without Ransack::Search object' do + it 'raises ArgumentError' do + expect { + @controller.view_context.search_form_with(model: "not a search object") {} + }.to raise_error(ArgumentError, 'No Ransack::Search object was provided to search_form_with!') + end + end + describe '#turbo_search_form_for with default options' do subject { @controller.view_context @@ -994,6 +1046,28 @@ module Helpers helper.send(:extract_search_and_set_url, "invalid", options, 'turbo_search_form_for') }.to raise_error(ArgumentError, 'No Ransack::Search object was provided to turbo_search_form_for!') end + + it 'extracts search from Ransack::Search object for search_form_with' do + options = {} + result = helper.send(:extract_search_and_set_url, search, options, 'search_form_with') + expect(result).to eq(search) + expect(options[:url]).to match(/people/) + end + + it 'extracts search from array with Search object for search_form_with' do + options = {} + comment_search = Comment.ransack + result = helper.send(:extract_search_and_set_url, [:admin, comment_search], options, 'search_form_with') + expect(result).to eq(comment_search) + expect(options[:url]).to match(/admin/) + end + + it 'raises error for invalid record with correct method name for search_form_with' do + options = {} + expect { + helper.send(:extract_search_and_set_url, "invalid", options, 'search_form_with') + }.to raise_error(ArgumentError, 'No Ransack::Search object was provided to search_form_with!') + end end end end From 733af07e69b688e46cc4d127f9de5ec21735b048 Mon Sep 17 00:00:00 2001 From: Sean Date: Wed, 24 Sep 2025 23:17:10 +0200 Subject: [PATCH 3/3] Update lib/ransack/helpers/form_helper.rb Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lib/ransack/helpers/form_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index ce2665f5b..90665da84 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -27,7 +27,6 @@ def search_form_with(record_or_options = {}, options = {}, &proc) # Called with positional arguments: search_form_with(@q) record = record_or_options end - search = extract_search_and_set_url(record, options, 'search_form_with') options[:html] ||= {} html_options = build_html_options(search, options, :get)