Skip to content

Commit af55622

Browse files
Copilotscarroll32Copilot
authored
Add the helper search_form_with (#1599)
* Initial plan * Add search_form_with helper * Update lib/ransack/helpers/form_helper.rb Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Sean <[email protected]> Co-authored-by: Sean <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent aa553f0 commit af55622

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

lib/ransack/helpers/form_helper.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ def search_form_for(record, options = {}, &proc)
1414
form_for(record, options, &proc)
1515
end
1616

17+
# +search_form_with+
18+
#
19+
# <%= search_form_with(model: @q) do |f| %>
20+
#
21+
def search_form_with(record_or_options = {}, options = {}, &proc)
22+
if record_or_options.is_a?(Hash) && record_or_options.key?(:model)
23+
# Called with keyword arguments: search_form_with(model: @q)
24+
options = record_or_options
25+
record = options.delete(:model)
26+
else
27+
# Called with positional arguments: search_form_with(@q)
28+
record = record_or_options
29+
end
30+
search = extract_search_and_set_url(record, options, 'search_form_with')
31+
options[:html] ||= {}
32+
html_options = build_html_options(search, options, :get)
33+
finalize_form_with_options(options, html_options)
34+
form_with(model: search, **options, &proc)
35+
end
36+
1737
# +turbo_search_form_for+
1838
#
1939
# <%= turbo_search_form_for(@q) do |f| %>
@@ -108,6 +128,12 @@ def finalize_form_options(options, html_options)
108128
options[:builder] ||= FormBuilder
109129
end
110130

131+
def finalize_form_with_options(options, html_options)
132+
options[:scope] ||= Ransack.options[:search_key]
133+
options[:html].reverse_merge!(html_options)
134+
options[:builder] ||= FormBuilder
135+
end
136+
111137
def options_for(record)
112138
record.map { |r| parse_record(r) }
113139
end

spec/ransack/helpers/form_helper_spec.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,58 @@ module Helpers
859859
it { should match /example_name_eq/ }
860860
end
861861

862+
describe '#search_form_with with default format' do
863+
subject { @controller.view_context
864+
.search_form_with(model: Person.ransack) {} }
865+
it { should match /action="\/people"/ }
866+
end
867+
868+
describe '#search_form_with with pdf format' do
869+
subject {
870+
@controller.view_context
871+
.search_form_with(model: Person.ransack, format: :pdf) {}
872+
}
873+
it { should match /action="\/people.pdf"/ }
874+
end
875+
876+
describe '#search_form_with with json format' do
877+
subject {
878+
@controller.view_context
879+
.search_form_with(model: Person.ransack, format: :json) {}
880+
}
881+
it { should match /action="\/people.json"/ }
882+
end
883+
884+
describe '#search_form_with with an array of routes' do
885+
subject {
886+
@controller.view_context
887+
.search_form_with(model: [:admin, Comment.ransack]) {}
888+
}
889+
it { should match /action="\/admin\/comments"/ }
890+
end
891+
892+
describe '#search_form_with with custom default search key' do
893+
before do
894+
Ransack.configure { |c| c.search_key = :example }
895+
end
896+
after do
897+
Ransack.configure { |c| c.search_key = :q }
898+
end
899+
subject {
900+
@controller.view_context
901+
.search_form_with(model: Person.ransack) { |f| f.text_field :name_eq }
902+
}
903+
it { should match /example\[name_eq\]/ }
904+
end
905+
906+
describe '#search_form_with without Ransack::Search object' do
907+
it 'raises ArgumentError' do
908+
expect {
909+
@controller.view_context.search_form_with(model: "not a search object") {}
910+
}.to raise_error(ArgumentError, 'No Ransack::Search object was provided to search_form_with!')
911+
end
912+
end
913+
862914
describe '#turbo_search_form_for with default options' do
863915
subject {
864916
@controller.view_context
@@ -994,6 +1046,28 @@ module Helpers
9941046
helper.send(:extract_search_and_set_url, "invalid", options, 'turbo_search_form_for')
9951047
}.to raise_error(ArgumentError, 'No Ransack::Search object was provided to turbo_search_form_for!')
9961048
end
1049+
1050+
it 'extracts search from Ransack::Search object for search_form_with' do
1051+
options = {}
1052+
result = helper.send(:extract_search_and_set_url, search, options, 'search_form_with')
1053+
expect(result).to eq(search)
1054+
expect(options[:url]).to match(/people/)
1055+
end
1056+
1057+
it 'extracts search from array with Search object for search_form_with' do
1058+
options = {}
1059+
comment_search = Comment.ransack
1060+
result = helper.send(:extract_search_and_set_url, [:admin, comment_search], options, 'search_form_with')
1061+
expect(result).to eq(comment_search)
1062+
expect(options[:url]).to match(/admin/)
1063+
end
1064+
1065+
it 'raises error for invalid record with correct method name for search_form_with' do
1066+
options = {}
1067+
expect {
1068+
helper.send(:extract_search_and_set_url, "invalid", options, 'search_form_with')
1069+
}.to raise_error(ArgumentError, 'No Ransack::Search object was provided to search_form_with!')
1070+
end
9971071
end
9981072
end
9991073
end

0 commit comments

Comments
 (0)