Advertisement
c00lways

ransack multi column sort

Aug 5th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.56 KB | None | 0 0
  1.   #usage example: <%=multi_sort_link(@q, :state_name, ["state_country asc", :state_name]) %>
  2.   #if order (ASC/DESC) is specified, the sorting will not be inverted in sort
  3.   def multi_sort_link(search, field, attribute, *args)
  4.     # Extract out a routing proxy for url_for scoping later
  5.     if search.is_a?(Array)
  6.       routing_proxy = search.shift
  7.       search = search.first
  8.     end
  9.  
  10.    
  11.     raise TypeError, "First argument must be a Ransack::Search!" unless
  12.       Ransack::Search === search
  13.  
  14.     search_params = params[search.context.search_key].presence ||
  15.       {}.with_indifferent_access
  16.    
  17.     #attribute is a hash
  18.     attr_name = field if field.is_a?(String)
  19.     attr_name = field.to_s if field.is_a?(Symbol)
  20.    
  21.     options = args.first.is_a?(Hash) ? args.shift.dup : {}
  22.     default_order = options.delete :default_order
  23.    
  24.     sort_params = []
  25.     current_dir = "ASC"
  26.     attribute.each do |value|
  27.       if value.is_a?(Symbol)
  28.         field_name, dir = value.to_s.split(" ")
  29.       else
  30.         field_name, dir = value.split(" ")
  31.       end
  32.      
  33.      
  34.       #invert direction when no direction specified
  35.       if dir.nil?
  36.         if existing_sort = search.sorts.detect { |s| s.name == field_name }
  37.           prev_attr, prev_dir = existing_sort.name, existing_sort.dir
  38.         end
  39.        
  40.         current_dir = prev_attr == attr_name ? prev_dir : nil
  41.         if current_dir
  42.           new_dir = current_dir == 'desc' ? 'asc' : 'desc'
  43.         else
  44.           new_dir = default_order || 'asc'
  45.         end
  46.       else
  47.         new_dir = dir
  48.       end
  49.      
  50.       sort_params << field_name+" #{new_dir}"
  51.     end
  52.    
  53.     name = (
  54.       if args.size > 0 && !args.first.is_a?(Hash)
  55.         args.shift.to_s
  56.       else
  57.         Ransack::Translate.attribute(field, :context => search.context)
  58.       end
  59.       )
  60.  
  61.     html_options = args.first.is_a?(Hash) ? args.shift.dup : {}
  62.     css = ['sort_link', current_dir].compact.join(' ')
  63.     html_options[:class] = [css, html_options[:class]].compact.join(' ')
  64.     query_hash = {}
  65.     query_hash[search.context.search_key] = search_params
  66.     .merge(:s => sort_params)
  67.    
  68.    
  69.    
  70.     options.merge!(query_hash)
  71.     options_for_url = params.merge options
  72.  
  73.     url = if routing_proxy && respond_to?(routing_proxy)
  74.       send(routing_proxy).url_for(options_for_url)
  75.     else
  76.       url_for(options_for_url)
  77.     end
  78.  
  79.     link_to(
  80.       [ERB::Util.h(name), order_indicator_for(current_dir)]
  81.         .compact
  82.         .join(' ')
  83.         .html_safe,
  84.       url,
  85.       html_options
  86.       )
  87.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement