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
-
#asc? ⇒ Boolean
Convenience method for determining if the ordering is ascending.
-
#desc? ⇒ Boolean
Convenience method for determining if the ordering is descending.
-
#joins_with_ordering ⇒ Object
:nodoc:.
-
#order_as ⇒ Object
Determines how the search is being ordered: as DESC or ASC.
-
#order_as=(value) ⇒ Object
Sets how the results will be ordered: ASC or DESC.
-
#order_by ⇒ Object
Determines by what columns the search is being ordered.
-
#order_by=(value) ⇒ Object
Lets you set how to order the data.
-
#order_by_joins ⇒ Object
Returns the joins neccessary for the “order” statement so that we don’t get an SQL error.
-
#order_with_ordering=(value) ⇒ Object
:nodoc.
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 :joins, :ordering alias_method_chain :order=, :ordering end end |
Instance Method Details
#asc? ⇒ Boolean
Convenience method for determining if the ordering is ascending
38 39 40 |
# File 'lib/searchgasm/search/ordering.rb', line 38 def asc? !desc? end |
#desc? ⇒ Boolean
Convenience method for determining if the ordering is descending
43 44 45 |
# File 'lib/searchgasm/search/ordering.rb', line 43 def desc? order_as == "DESC" end |
#joins_with_ordering ⇒ Object
:nodoc:
26 27 28 |
# File 'lib/searchgasm/search/ordering.rb', line 26 def joins_with_ordering # :nodoc: merge_joins(joins_without_ordering, order_by_joins) end |
#order_as ⇒ Object
Determines how the search is being ordered: as DESC or ASC
48 49 50 |
# File 'lib/searchgasm/search/ordering.rb', line 48 def order_as @order_as ||= (order.blank? || order =~ /ASC$/i) ? "ASC" : "DESC" end |
#order_as=(value) ⇒ Object
Sets how the results will be ordered: ASC or DESC
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? @order = order_by_to_order(order_by, value) else @order.gsub!(/(ASC|DESC)/i, value) end @order_as = value end |
#order_by ⇒ Object
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_by = order_parts.size <= 1 ? order_parts.first : order_parts else @order_by = 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 107 |
# File 'lib/searchgasm/search/ordering.rb', line 102 def order_by=(value) self.order_by_joins.clear @order_by = get_order_by_value(value) @order = order_by_to_order(@order_by, order_as) @order_by end |
#order_by_joins ⇒ Object
Returns the joins neccessary for the “order” statement so that we don’t get an SQL error
110 111 112 113 114 115 |
# File 'lib/searchgasm/search/ordering.rb', line 110 def order_by_joins @order_by_joins ||= [] @order_by_joins.compact! @order_by_joins.uniq! @order_by_joins end |
#order_with_ordering=(value) ⇒ Object
:nodoc
30 31 32 33 34 35 |
# File 'lib/searchgasm/search/ordering.rb', line 30 def order_with_ordering=(value) # :nodoc @order_by = nil @order_as = nil self.order_by_joins.clear self.order_without_ordering = value end |