Module: Searchgasm::Search::Ordering

Included in:
Base
Defined in:
lib/searchgasm/search/ordering.rb

Overview

Search Ordering

The purpose of this module is to provide easy ordering for your searches. All that these options do is build :order for you. This plays a huge part in ordering your data on the interface. See the options and examples below. The readme also touches on ordering. It’s pretty simple thought:

Examples

search.order_by = :id
search.order_by = [:id, :first_name]
search.order_by = {:user_group => :name}
search.order_by = [:id, {:user_group => :name}]
search.order_by = {:user_group => {:account => :name}} # you can traverse through all of your relationships

search.order_as = "DESC"
search.order_as = "ASC"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



19
20
21
22
23
24
# File 'lib/searchgasm/search/ordering.rb', line 19

def self.included(klass)
  klass.class_eval do
    alias_method_chain :include, :ordering
    alias_method_chain :order=, :ordering
  end
end

Instance Method Details

#asc?Boolean

Convenience method for determining if the ordering is ascending

Returns:

  • (Boolean)


37
38
39
# File 'lib/searchgasm/search/ordering.rb', line 37

def asc?
  !desc?
end

#desc?Boolean

Convenience method for determining if the ordering is descending

Returns:

  • (Boolean)


42
43
44
# File 'lib/searchgasm/search/ordering.rb', line 42

def desc?
  order_as == "DESC"
end

#include_with_orderingObject

:nodoc:



26
27
28
29
# File 'lib/searchgasm/search/ordering.rb', line 26

def include_with_ordering # :nodoc:
  includes = [include_without_ordering, order_by_includes].flatten.compact.uniq
  includes.blank? ? nil : (includes.size == 1 ? includes.first : includes)
end

#order_asObject

Determines how the search is being ordered: as DESC or ASC



47
48
49
50
# File 'lib/searchgasm/search/ordering.rb', line 47

def order_as
  return "ASC" if order.blank?
  order =~ /ASC$/i ? "ASC" : "DESC"
end

#order_as=(value) ⇒ Object

Sets how the results will be ordered: ASC or DESC

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/searchgasm/search/ordering.rb', line 53

def order_as=(value)
  value = value.to_s.upcase
  raise(ArgumentError, "order_as only accepts a string as ASC or DESC") unless ["ASC", "DESC"].include?(value)
  
  if order.blank?
    self.order = order_by_to_order(order_by, value)
  else
    self.order.gsub!(/(ASC|DESC)/i, value)
  end
  
  value
end

#order_byObject

Determines by what columns the search is being ordered. This is nifty in that is reverse engineers the order SQL to determine this, only if you haven’t explicitly set the order_by option yourself.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/searchgasm/search/ordering.rb', line 68

def order_by
  return @order_by if @order_by
  
  if !order.blank?
    # Reversege engineer order, only go 1 level deep with relationships, anything beyond that is probably excessive and not good for performance
    order_parts = order.split(",").collect do |part|
      part.strip!
      part.gsub!(/ (ASC|DESC)$/i, "").gsub!(/(.*)\./, "")
      table_name = ($1 ? $1.gsub(/[^a-z0-9_]/i, "") : nil)
      part.gsub!(/[^a-z0-9_]/i, "")
      reflection = nil
      if table_name && table_name != klass.table_name
        reflection = klass.reflect_on_association(table_name.to_sym) || klass.reflect_on_association(table_name.singularize.to_sym)
        next unless reflection
        {reflection.name.to_s => part}
      else
        part
      end
    end.compact
    order_parts.size <= 1 ? order_parts.first : order_parts
  else
    klass.primary_key
  end
end

#order_by=(value) ⇒ Object

Lets you set how to order the data

Examples

In these examples “ASC” is determined by the value of order_as

order_by = :id # => users.id ASC
order_by = [:id, name] # => users.id ASC, user.name ASC
order_by = [:id, {:user_group => :name}] # => users.id ASC, user_groups.name ASC


102
103
104
105
106
# File 'lib/searchgasm/search/ordering.rb', line 102

def order_by=(value)
  @order_by = get_order_by_value(value)
  @order = order_by_to_order(@order_by, order_as) # use @order so @order_by doesnt get reset
  @order_by
end

#order_by_includesObject

Returns the includes neccessary for the “order” statement so that we don’t get an SQL error



109
110
111
112
113
114
# File 'lib/searchgasm/search/ordering.rb', line 109

def order_by_includes
  @order_by_includes ||= []
  @order_by_includes.compact!
  @order_by_includes.uniq!
  @order_by_includes
end

#order_with_ordering=(value) ⇒ Object

:nodoc



31
32
33
34
# File 'lib/searchgasm/search/ordering.rb', line 31

def order_with_ordering=(value) # :nodoc
  @order_by = nil
  self.order_without_ordering = value
end