2011-03-31 00:31:39 +00:00
|
|
|
module Ransack
|
|
|
|
module Helpers
|
|
|
|
module FormHelper
|
2011-08-13 20:36:40 +00:00
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
def search_form_for(record, options = {}, &proc)
|
|
|
|
if record.is_a?(Ransack::Search)
|
|
|
|
search = record
|
2014-04-18 08:49:38 +00:00
|
|
|
options[:url] ||= polymorphic_path(
|
|
|
|
search.klass, format: options.delete(:format)
|
|
|
|
)
|
2013-12-10 18:18:17 +00:00
|
|
|
elsif record.is_a?(Array) &&
|
|
|
|
(search = record.detect { |o| o.is_a?(Ransack::Search) })
|
2013-08-04 13:13:41 +00:00
|
|
|
options[:url] ||= polymorphic_path(
|
2014-04-18 08:49:38 +00:00
|
|
|
record.map { |o| o.is_a?(Ransack::Search) ? o.klass : o },
|
|
|
|
format: options.delete(:format)
|
2013-12-10 18:18:17 +00:00
|
|
|
)
|
2011-03-31 00:31:39 +00:00
|
|
|
else
|
2013-08-04 13:13:41 +00:00
|
|
|
raise ArgumentError,
|
|
|
|
"No Ransack::Search object was provided to search_form_for!"
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
options[:html] ||= {}
|
|
|
|
html_options = {
|
2014-05-01 13:55:39 +00:00
|
|
|
:class => options[:class].present? ?
|
2013-12-10 18:18:17 +00:00
|
|
|
"#{options[:class]}" :
|
|
|
|
"#{search.klass.to_s.underscore}_search",
|
2014-05-01 13:55:39 +00:00
|
|
|
:id => options[:id].present? ?
|
2013-12-10 18:18:17 +00:00
|
|
|
"#{options[:id]}" :
|
|
|
|
"#{search.klass.to_s.underscore}_search",
|
2014-05-01 13:55:39 +00:00
|
|
|
:method => :get
|
2011-03-31 00:31:39 +00:00
|
|
|
}
|
2014-11-15 19:56:19 +00:00
|
|
|
options[:as] ||= Ransack.options[:search_key]
|
2011-03-31 00:31:39 +00:00
|
|
|
options[:html].reverse_merge!(html_options)
|
|
|
|
options[:builder] ||= FormBuilder
|
|
|
|
|
|
|
|
form_for(record, options, &proc)
|
|
|
|
end
|
|
|
|
|
2014-12-06 22:58:03 +00:00
|
|
|
# sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name')
|
2011-08-13 20:36:40 +00:00
|
|
|
def sort_link(search, attribute, *args)
|
2012-03-28 19:00:19 +00: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 18:18:17 +00:00
|
|
|
raise TypeError, "First argument must be a Ransack::Search!" unless
|
|
|
|
Search === search
|
2011-08-13 20:36:40 +00:00
|
|
|
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
# This is the field that this link represents. The direction of the sort icon (up/down arrow) will
|
|
|
|
# depend on the sort status of this field
|
|
|
|
field_name = attribute.to_s
|
|
|
|
|
|
|
|
# Determine the fields we want to sort on
|
|
|
|
sort_fields = if Array === args.first
|
|
|
|
args.shift
|
|
|
|
else
|
|
|
|
Array(field_name)
|
|
|
|
end
|
|
|
|
|
2014-10-03 18:54:20 +00:00
|
|
|
label_text =
|
2014-10-07 03:25:21 +00:00
|
|
|
if String === args.first
|
|
|
|
args.shift.to_s
|
|
|
|
else
|
|
|
|
Translate.attribute(field_name, :context => search.context)
|
|
|
|
end
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
|
|
|
|
options = args.first.is_a?(Hash) ? args.shift.dup : {}
|
2014-11-24 15:48:29 +00:00
|
|
|
hide_indicator = options.delete :hide_indicator
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
default_order = options.delete :default_order
|
2014-10-03 18:58:22 +00:00
|
|
|
default_order_is_a_hash = Hash === default_order
|
2014-10-03 18:54:20 +00:00
|
|
|
|
2014-12-06 22:58:03 +00:00
|
|
|
# If the default order is a hash of fields, duplicate it and let us
|
|
|
|
# access it with strings or symbols.
|
|
|
|
if default_order_is_a_hash
|
|
|
|
default_order = default_order.dup.with_indifferent_access
|
|
|
|
end
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
|
2014-05-13 05:06:53 +00:00
|
|
|
search_params = params[search.context.search_key].presence ||
|
2013-12-10 18:18:17 +00:00
|
|
|
{}.with_indifferent_access
|
2011-08-13 20:36:40 +00:00
|
|
|
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
# Find the current direction (if there is one) of the primary sort field
|
|
|
|
if existing_sort = search.sorts.detect { |s| s.name == field_name }
|
|
|
|
field_current_dir = existing_sort.dir
|
|
|
|
end
|
2011-08-13 20:36:40 +00:00
|
|
|
|
2014-12-06 22:58:03 +00:00
|
|
|
sort_params = initialize_sort_params(sort_fields, existing_sort,
|
|
|
|
search, default_order_is_a_hash, default_order)
|
2011-08-13 20:36:40 +00:00
|
|
|
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
# if there is only one sort parameter, remove it from the array and just
|
|
|
|
# use the string as the parameter
|
2014-12-06 22:58:03 +00:00
|
|
|
if sort_params.size == 1
|
|
|
|
sort_params = sort_params.first
|
|
|
|
end
|
2011-08-13 20:36:40 +00:00
|
|
|
|
|
|
|
html_options = args.first.is_a?(Hash) ? args.shift.dup : {}
|
2014-11-21 19:03:20 +00:00
|
|
|
css = [Constants::SORT_LINK, field_current_dir]
|
|
|
|
.compact.join(Constants::SPACE)
|
2014-10-09 21:48:38 +00:00
|
|
|
html_options[:class] = [css, html_options[:class]]
|
2014-11-21 19:03:20 +00:00
|
|
|
.compact.join(Constants::SPACE)
|
2014-10-03 18:54:20 +00:00
|
|
|
|
2012-04-11 15:58:27 +00:00
|
|
|
query_hash = {}
|
2013-12-10 18:18:17 +00:00
|
|
|
query_hash[search.context.search_key] = search_params
|
Add multiple sort field support in sort_link
This patch allows users to sort on multiple fields with the sort_link
helper.
To specify sorting on multiple fields:
sort_link(:kind, [:kind, 'name asc'])
This will create a sort link that sorts first by kind, and then by
name. The first `:kind` parameter ensures that the link generated
shows the sort status of the `kind` field.
When you specify a sort direction in the sort fields array, the
direction is locked to that direction. In the above example, clicking
the resulting link would toggle sorting of the kind field, but the name
field would always sort ascending.
Also added was the ability to specify multiple default_order fields
with a hash:
sort_link(:kind, [:kind, :name],
:default_order => { :name => 'asc', :kind => 'desc' })
Clicking the resulting link will toggle the sort directions of both
`name` and `kind`, sorting the `name` field by default ascending, and
the `kind` field descending.
2014-09-30 22:58:33 +00:00
|
|
|
.merge(:s => sort_params)
|
2012-04-11 15:58:27 +00:00
|
|
|
options.merge!(query_hash)
|
2014-10-03 22:44:28 +00:00
|
|
|
options_for_url = params.merge(options)
|
2012-03-07 19:31:13 +00:00
|
|
|
|
2014-12-06 22:58:03 +00:00
|
|
|
url = build_url_for(routing_proxy, options_for_url)
|
2014-11-24 15:48:29 +00:00
|
|
|
name = link_name(label_text, field_current_dir, hide_indicator)
|
2014-10-03 18:54:20 +00:00
|
|
|
|
|
|
|
link_to(name, url, html_options)
|
2011-08-13 20:36:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-12-06 22:58:03 +00:00
|
|
|
# Extract out a routing proxy for url_for scoping later
|
|
|
|
def routing_proxy_and_search_object(search)
|
|
|
|
if search.is_a? Array
|
|
|
|
[search.first, search.second]
|
|
|
|
else
|
|
|
|
[nil, search]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_sort_params(sort_fields, existing_sort, search,
|
|
|
|
default_order_is_a_hash, default_order)
|
|
|
|
sort_fields.each_with_object([]) do |field, a|
|
|
|
|
attr_name, new_dir = field.to_s.split(/\s+/)
|
|
|
|
current_dir = nil
|
|
|
|
# if the user didn't specify the sort direction, detect the previous
|
|
|
|
# sort direction on this field and invert it.
|
|
|
|
if neither_asc_nor_desc?(new_dir)
|
|
|
|
if existing_sort = search.sorts.detect { |s| s.name == attr_name }
|
|
|
|
current_dir = existing_sort.dir
|
|
|
|
end
|
|
|
|
new_dir =
|
|
|
|
if current_dir
|
|
|
|
direction_text(current_dir)
|
|
|
|
elsif default_order_is_a_hash
|
|
|
|
default_order[attr_name] || Constants::ASC
|
|
|
|
else
|
|
|
|
default_order || Constants::ASC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
a << "#{attr_name} #{new_dir}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_url_for(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
|
2014-10-03 18:54:20 +00:00
|
|
|
|
2014-12-06 22:58:03 +00:00
|
|
|
def link_name(label_text, dir, hide_indicator)
|
|
|
|
[ERB::Util.h(label_text), order_indicator_for(dir, hide_indicator)]
|
|
|
|
.compact
|
|
|
|
.join(Constants::NON_BREAKING_SPACE)
|
|
|
|
.html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_indicator_for(dir, hide_indicator)
|
|
|
|
if hide_indicator || neither_asc_nor_desc?(dir)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
direction_arrow(dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def neither_asc_nor_desc?(dir)
|
|
|
|
Constants::ASC_DESC.none? { |d| d == dir }
|
|
|
|
end
|
|
|
|
|
|
|
|
def direction_arrow(dir)
|
|
|
|
if dir == Constants::DESC
|
|
|
|
Constants::DESC_ARROW
|
|
|
|
else
|
|
|
|
Constants::ASC_ARROW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def direction_text(dir)
|
|
|
|
if dir == Constants::DESC
|
|
|
|
Constants::ASC
|
|
|
|
else
|
|
|
|
Constants::DESC
|
|
|
|
end
|
2011-08-13 20:36:40 +00:00
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
2012-04-11 15:58:27 +00:00
|
|
|
end
|