2011-03-30 20:31:39 -04:00
|
|
|
module Ransack
|
|
|
|
module Helpers
|
|
|
|
module FormHelper
|
2011-08-13 16:36:40 -04:00
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
def search_form_for(record, options = {}, &proc)
|
|
|
|
if record.is_a?(Ransack::Search)
|
|
|
|
search = record
|
2014-04-18 04:49:38 -04:00
|
|
|
options[:url] ||= polymorphic_path(
|
|
|
|
search.klass, format: options.delete(:format)
|
|
|
|
)
|
2013-12-10 13:18:17 -05:00
|
|
|
elsif record.is_a?(Array) &&
|
|
|
|
(search = record.detect { |o| o.is_a?(Ransack::Search) })
|
2013-08-04 09:13:41 -04:00
|
|
|
options[:url] ||= polymorphic_path(
|
2014-04-18 04:49:38 -04:00
|
|
|
record.map { |o| o.is_a?(Ransack::Search) ? o.klass : o },
|
|
|
|
format: options.delete(:format)
|
2013-12-10 13:18:17 -05:00
|
|
|
)
|
2011-03-30 20:31:39 -04:00
|
|
|
else
|
2013-08-04 09:13:41 -04:00
|
|
|
raise ArgumentError,
|
|
|
|
"No Ransack::Search object was provided to search_form_for!"
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
options[:html] ||= {}
|
|
|
|
html_options = {
|
2014-05-01 09:55:39 -04:00
|
|
|
:class => options[:class].present? ?
|
2013-12-10 13:18:17 -05:00
|
|
|
"#{options[:class]}" :
|
|
|
|
"#{search.klass.to_s.underscore}_search",
|
2014-05-01 09:55:39 -04:00
|
|
|
:id => options[:id].present? ?
|
2013-12-10 13:18:17 -05:00
|
|
|
"#{options[:id]}" :
|
|
|
|
"#{search.klass.to_s.underscore}_search",
|
2014-05-01 09:55:39 -04:00
|
|
|
:method => :get
|
2011-03-30 20:31:39 -04:00
|
|
|
}
|
2011-04-18 13:59:47 -04:00
|
|
|
options[:as] ||= 'q'
|
2011-03-30 20:31:39 -04:00
|
|
|
options[:html].reverse_merge!(html_options)
|
|
|
|
options[:builder] ||= FormBuilder
|
|
|
|
|
|
|
|
form_for(record, options, &proc)
|
|
|
|
end
|
|
|
|
|
2011-08-13 16:36:40 -04:00
|
|
|
def sort_link(search, attribute, *args)
|
2012-03-28 15:00:19 -04:00
|
|
|
# Extract out a routing proxy for url_for scoping later
|
|
|
|
if search.is_a?(Array)
|
|
|
|
routing_proxy = search.shift
|
|
|
|
search = search.first
|
|
|
|
end
|
|
|
|
|
2013-12-10 13:18:17 -05:00
|
|
|
raise TypeError, "First argument must be a Ransack::Search!" unless
|
|
|
|
Search === search
|
2011-08-13 16:36:40 -04:00
|
|
|
|
2014-05-13 01:06:53 -04:00
|
|
|
search_params = params[search.context.search_key].presence ||
|
2013-12-10 13:18:17 -05:00
|
|
|
{}.with_indifferent_access
|
2011-08-13 16:36:40 -04:00
|
|
|
|
|
|
|
attr_name = attribute.to_s
|
|
|
|
|
2013-12-10 13:18:17 -05:00
|
|
|
name = (
|
|
|
|
if args.size > 0 && !args.first.is_a?(Hash)
|
|
|
|
args.shift.to_s
|
|
|
|
else
|
2014-05-01 09:55:39 -04:00
|
|
|
Translate.attribute(attr_name, :context => search.context)
|
2013-12-10 13:18:17 -05:00
|
|
|
end
|
|
|
|
)
|
2011-08-13 16:36:40 -04:00
|
|
|
|
2013-08-04 09:13:41 -04:00
|
|
|
if existing_sort = search.sorts.detect { |s| s.name == attr_name }
|
2011-08-13 16:36:40 -04:00
|
|
|
prev_attr, prev_dir = existing_sort.name, existing_sort.dir
|
|
|
|
end
|
|
|
|
|
|
|
|
options = args.first.is_a?(Hash) ? args.shift.dup : {}
|
|
|
|
default_order = options.delete :default_order
|
|
|
|
current_dir = prev_attr == attr_name ? prev_dir : nil
|
|
|
|
|
|
|
|
if current_dir
|
|
|
|
new_dir = current_dir == 'desc' ? 'asc' : 'desc'
|
|
|
|
else
|
|
|
|
new_dir = default_order || 'asc'
|
|
|
|
end
|
|
|
|
|
|
|
|
html_options = args.first.is_a?(Hash) ? args.shift.dup : {}
|
|
|
|
css = ['sort_link', current_dir].compact.join(' ')
|
|
|
|
html_options[:class] = [css, html_options[:class]].compact.join(' ')
|
2012-04-11 11:58:27 -04:00
|
|
|
query_hash = {}
|
2013-12-10 13:18:17 -05:00
|
|
|
query_hash[search.context.search_key] = search_params
|
2014-05-01 09:55:39 -04:00
|
|
|
.merge(:s => "#{attr_name} #{new_dir}")
|
2012-04-11 11:58:27 -04:00
|
|
|
options.merge!(query_hash)
|
2013-07-22 09:23:39 -04:00
|
|
|
options_for_url = params.merge options
|
2012-03-07 14:31:13 -05:00
|
|
|
|
2012-04-11 16:34:37 -04:00
|
|
|
url = if routing_proxy && respond_to?(routing_proxy)
|
2013-07-22 09:23:39 -04:00
|
|
|
send(routing_proxy).url_for(options_for_url)
|
2012-04-02 16:13:35 -04:00
|
|
|
else
|
2013-07-22 09:23:39 -04:00
|
|
|
url_for(options_for_url)
|
2012-04-02 16:13:35 -04:00
|
|
|
end
|
2012-03-28 15:00:19 -04:00
|
|
|
|
2013-12-10 13:18:17 -05:00
|
|
|
link_to(
|
|
|
|
[ERB::Util.h(name), order_indicator_for(current_dir)]
|
|
|
|
.compact
|
|
|
|
.join(' ')
|
|
|
|
.html_safe,
|
2012-04-02 16:13:35 -04:00
|
|
|
url,
|
2012-03-28 15:00:19 -04:00
|
|
|
html_options
|
2013-12-10 13:18:17 -05:00
|
|
|
)
|
2011-08-13 16:36:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def order_indicator_for(order)
|
|
|
|
if order == 'asc'
|
|
|
|
'▲'
|
|
|
|
elsif order == 'desc'
|
|
|
|
'▼'
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-11 11:58:27 -04:00
|
|
|
end
|