Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def search_form_for(record, options = {}, &proc)
search = extract_search_and_set_url(record, options, 'search_form_for')
options[:html] ||= {}
html_options = build_html_options(search, options, :get)
finalize_form_options(options, html_options)
finalize_form_options(search, options, html_options)
form_for(record, options, &proc)
end

Expand All @@ -30,7 +30,7 @@ def search_form_with(record_or_options = {}, options = {}, &proc)
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)
finalize_form_with_options(search, options, html_options)
form_with(model: search, **options, &proc)
end

Expand All @@ -48,7 +48,7 @@ def turbo_search_form_for(record, options = {}, &proc)
turbo_options = build_turbo_options(options)
method = options.delete(:method) || :post
html_options = build_html_options(search, options, method).merge(turbo_options)
finalize_form_options(options, html_options)
finalize_form_options(search, options, html_options)
form_for(record, options, &proc)
end

Expand Down Expand Up @@ -122,14 +122,14 @@ def build_html_options(search, options, method)
}
end

def finalize_form_options(options, html_options)
options[:as] ||= Ransack.options[:search_key]
def finalize_form_options(search, options, html_options)
options[:as] ||= search.context.search_key
options[:html].reverse_merge!(html_options)
options[:builder] ||= FormBuilder
end

def finalize_form_with_options(options, html_options)
options[:scope] ||= Ransack.options[:search_key]
def finalize_form_with_options(search, options, html_options)
options[:scope] ||= search.context.search_key
options[:html].reverse_merge!(html_options)
options[:builder] ||= FormBuilder
end
Expand Down
26 changes: 26 additions & 0 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,21 @@ module Helpers
it { should match /example_name_eq/ }
end

# Regression test for https://github.com/activerecord-hackery/ransack/issues/1118
# When a per-search `search_key:` option is passed to `Person.ransack`,
# `search_form_for` should honour it instead of falling back to the
# global `Ransack.options[:search_key]`. `sort_link` and `sort_url`
# already read this from the search's context.
describe '#search_form_for with per-search search_key' do
subject {
@controller.view_context
.search_form_for(Person.ransack({}, search_key: :people_search)) { |f|
f.text_field :name_eq
}
}
it { should match /people_search_name_eq/ }
end

describe '#search_form_with with default format' do
subject { @controller.view_context
.search_form_with(model: Person.ransack) {} }
Expand Down Expand Up @@ -903,6 +918,17 @@ module Helpers
it { should match /example\[name_eq\]/ }
end

# Regression test for https://github.com/activerecord-hackery/ransack/issues/1118
describe '#search_form_with with per-search search_key' do
subject {
@controller.view_context
.search_form_with(model: Person.ransack({}, search_key: :people_search)) { |f|
f.text_field :name_eq
}
}
it { should match /people_search\[name_eq\]/ }
end

describe '#search_form_with without Ransack::Search object' do
it 'raises ArgumentError' do
expect {
Expand Down
Loading