module Ransack::Helpers::FormHelper

Public Instance Methods

search_form_for(record, options = {}, &proc) click to toggle source

search_form_for

<%= search_form_for(@q) do |f| %>
# File lib/ransack/helpers/form_helper.rb, line 9
def search_form_for(record, options = {}, &proc)
  if record.is_a? Ransack::Search
    search = record
    options[:url] ||= polymorphic_path(
      search.klass, format: options.delete(:format)
      )
  elsif record.is_a?(Array) &&
  (search = record.detect { |o| o.is_a?(Ransack::Search) })
    options[:url] ||= polymorphic_path(
      options_for(record), format: options.delete(:format)
      )
  else
    raise ArgumentError,
    'No Ransack::Search object was provided to search_form_for!'
  end
  options[:html] ||= {}
  html_options = {
    class:  html_option_for(options[:class], search),
    id:     html_option_for(options[:id], search),
    method: :get
  }
  options[:as] ||= Ransack.options[:search_key]
  options[:html].reverse_merge!(html_options)
  options[:builder] ||= FormBuilder

  form_for(record, options, &proc)
end

Private Instance Methods

extract_search_and_routing_proxy(search) click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 72
def extract_search_and_routing_proxy(search)
  if search.is_a? Array
    [search.second, search.first]
  else
    [search, nil]
  end
end
html_option_for(option, search) click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 64
def html_option_for(option, search)
  if option.present?
    option.to_s
  else
    "#{search.klass.to_s.underscore}_search"
  end
end
options_for(record) click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 52
def options_for(record)
  record.map &method(:parse_record)
end
parse_record(object) click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 56
def parse_record(object)
  if object.is_a?(Ransack::Search)
    object.klass
  else
    object
  end
end
url(routing_proxy, options_for_url) click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 80
def url(routing_proxy, options_for_url)
  if routing_proxy && respond_to?(routing_proxy)
    send(routing_proxy).url_for(options_for_url)
  else
    url_for(options_for_url)
  end
end