Module: Ransack::Helpers::FormHelper

Defined in:
lib/ransack/helpers/form_helper.rb

Defined Under Namespace

Classes: SortLink

Instance Method Summary collapse

Instance Method Details

#search_form_for(record, options = {}, &proc) ⇒ Object

search_form_for

<%= search_form_for(@q) do |f| %>


9
10
11
12
13
14
15
# File 'lib/ransack/helpers/form_helper.rb', line 9

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)
  form_for(record, options, &proc)
end

#search_form_with(record_or_options = {}, options = {}, &proc) ⇒ Object

search_form_with

<%= search_form_with(model: @q) do |f| %>


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ransack/helpers/form_helper.rb', line 21

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

sort_link

<%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %>

You can also use a block:

<%= sort_link(@q, :name, [:name, 'kind ASC']) do %>
  <strong>Player Name</strong>
<% end %>


65
66
67
68
69
70
71
72
73
# File 'lib/ransack/helpers/form_helper.rb', line 65

def sort_link(search_object, attribute, *args, &block)
  search, routing_proxy = extract_search_and_routing_proxy(search_object)
  unless Search === search
    raise TypeError, 'First argument must be a Ransack::Search!'
  end
  args[args.first.is_a?(Array) ? 1 : 0, 0] = capture(&block) if block_given?
  s = SortLink.new(search, attribute, args, params, &block)
  link_to(s.name, url(routing_proxy, s.url_options), s.html_options(args))
end

#sort_url(search_object, attribute, *args) ⇒ Object

sort_url <%= sort_url(@q, :created_at, default_order: :desc) %>



78
79
80
81
82
83
84
85
# File 'lib/ransack/helpers/form_helper.rb', line 78

def sort_url(search_object, attribute, *args)
  search, routing_proxy = extract_search_and_routing_proxy(search_object)
  unless Search === search
    raise TypeError, 'First argument must be a Ransack::Search!'
  end
  s = SortLink.new(search, attribute, args, params)
  url(routing_proxy, s.url_options)
end

#turbo_search_form_for(record, options = {}, &proc) ⇒ Object

turbo_search_form_for

<%= turbo_search_form_for(@q) do |f| %>

This is a turbo-enabled version of search_form_for that submits via turbo streams instead of traditional HTML GET requests. Useful for seamless integration with paginated results and other turbo-enabled components.



45
46
47
48
49
50
51
52
53
# File 'lib/ransack/helpers/form_helper.rb', line 45

def turbo_search_form_for(record, options = {}, &proc)
  search = extract_search_and_set_url(record, options, 'turbo_search_form_for')
  options[:html] ||= {}
  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)
  form_for(record, options, &proc)
end