Skip to content

Commit

Permalink
multiple column sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushan committed Jul 17, 2012
1 parent c7f0844 commit 247e465
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/meta_search/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,33 @@ def get_sort

def set_sort(val)
return if val.blank?
column, direction = val.split('.')
direction ||= 'asc'
if ['asc','desc'].include?(direction)
if @base.respond_to?("sort_by_#{column}_#{direction}")
search_attributes['meta_sort'] = val
@relation = @relation.send("sort_by_#{column}_#{direction}")
elsif attribute = get_attribute(column)
#multiple column search
if val.scan('_and_').present?
attribute_names = []
val.split('_and_').each do |v|
column, direction = v.split('.')
direction ||= 'asc'
if ['asc', 'desc'].include?(direction)
attribute_names << {attr: column, dir: direction}
end
end
attributes = attribute_names.map {|n| get_attribute(n[:attr])}
if attribute_names.size == attributes.compact.size
search_attributes['meta_sort'] = val
@relation = @relation.order(attribute.send(direction).to_sql)
elsif column.scan('_and_').present?
attribute_names = column.split('_and_')
attributes = attribute_names.map {|n| get_attribute(n)}
if attribute_names.size == attributes.compact.size # We found all attributes
attributes.each_with_index do |attribute, i|
@relation = @relation.order(attribute.send(attribute_names[i][:dir]).to_sql)
end
end
else
column, direction = val.split('.')
direction ||= 'asc'
if ['asc','desc'].include?(direction)
if @base.respond_to?("sort_by_#{column}_#{direction}")
search_attributes['meta_sort'] = val
attributes.each do |attribute|
@relation = @relation.order(attribute.send(direction).to_sql)
end
@relation = @relation.send("sort_by_#{column}_#{direction}")
elsif attribute = get_attribute(column)
search_attributes['meta_sort'] = val
@relation = @relation.order(attribute.send(direction).to_sql)
end
end
end
Expand Down

0 comments on commit 247e465

Please sign in to comment.