27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/ransack/helpers/form_helper.rb', line 27
def sort_link(search, attribute, *args)
if search.is_a?(Array)
routing_proxy = search.shift
search = search.first
end
raise TypeError, "First argument must be a Ransack::Search!" unless Search === search
search_params = params[search.context.search_key] || {}.with_indifferent_access
attr_name = attribute.to_s
name = (args.size > 0 && !args.first.is_a?(Hash)) ? args.shift.to_s : Translate.attribute(attr_name, :context => search.context)
if existing_sort = search.sorts.detect {|s| s.name == attr_name}
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(' ')
query_hash = {}
query_hash[search.context.search_key] = search_params.merge(:s => "#{attr_name} #{new_dir}")
options.merge!(query_hash)
url = if routing_proxy && respond_to?(routing_proxy)
send(routing_proxy).url_for(options)
else
url_for(options)
end
link_to [ERB::Util.h(name), order_indicator_for(current_dir)].compact.join(' ').html_safe,
url,
html_options
end
|