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.
-
#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_auto_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.
-
#priority_order=(value) ⇒ Object
Let’s you set a priority order.
-
#priority_order_as ⇒ Object
Same as order_as but for your priority order.
-
#priority_order_as=(value) ⇒ Object
Same as order_as= but for your priority order.
-
#priority_order_by ⇒ Object
Same as order_by but for your priority order.
-
#priority_order_by=(value) ⇒ Object
Same as order_by= but for your priority order.
- #priority_order_by_auto_joins ⇒ Object
- #sanitize_with_ordering(searching = true) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/searchgasm/search/ordering.rb', line 19 def self.included(klass) klass.class_eval do alias_method_chain :order=, :ordering alias_method_chain :sanitize, :ordering attr_reader :priority_order end end |
Instance Method Details
#asc? ⇒ Boolean
Convenience method for determining if the ordering is ascending
35 36 37 |
# File 'lib/searchgasm/search/ordering.rb', line 35 def asc? !desc? end |
#desc? ⇒ Boolean
Convenience method for determining if the ordering is descending
40 41 42 43 |
# File 'lib/searchgasm/search/ordering.rb', line 40 def desc? return false if order_as.nil? order_as == "DESC" end |
#order_as ⇒ Object
Determines how the search is being ordered: as DESC or ASC
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/searchgasm/search/ordering.rb', line 46 def order_as return if order.blank? return @order_as if @order_as case order when /ASC$/i @order_as = "ASC" when /DESC$/i @order_as = "DESC" else nil end end |
#order_as=(value) ⇒ Object
Sets how the results will be ordered: ASC or DESC
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/searchgasm/search/ordering.rb', line 61 def order_as=(value) value = value.blank? ? nil : value.to_s.upcase raise(ArgumentError, "order_as only accepts a blank string / nil or a string as 'ASC' or 'DESC'") if !value.blank? && !["ASC", "DESC"].include?(value) if @order_by @order = order_by_to_order(@order_by, value) elsif order @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.
74 75 76 77 |
# File 'lib/searchgasm/search/ordering.rb', line 74 def order_by return if order.blank? @order_by ||= order_to_order_by(order) 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
88 89 90 91 92 93 |
# File 'lib/searchgasm/search/ordering.rb', line 88 def order_by=(value) @order_by_auto_joins = nil @order_by = get_order_by_value(value) @order = order_by_to_order(@order_by, @order_as) @order_by end |
#order_by_auto_joins ⇒ Object
Returns the joins neccessary for the “order” statement so that we don’t get an SQL error
96 97 98 |
# File 'lib/searchgasm/search/ordering.rb', line 96 def order_by_auto_joins @order_by_auto_joins ||= build_order_by_auto_joins(order_by) end |
#order_with_ordering=(value) ⇒ Object
:nodoc
27 28 29 30 31 32 |
# File 'lib/searchgasm/search/ordering.rb', line 27 def order_with_ordering=(value) # :nodoc @order_by = nil @order_as = nil @order_by_auto_joins = nil self.order_without_ordering = value end |
#priority_order=(value) ⇒ Object
Let’s you set a priority order. Meaning this will get ordered first before anything else, but is unnoticeable and abstracted out from your regular order. For example, lets say you have a model called Product that had a “featured” boolean column. You want to order the products by the price, quantity, etc., but you want the featured products to always be first.
Without a priority order your controller would get cluttered and your code would be much more complicated. All of your order_by_link methods would have to be order_by_link [:featured, :price], :text => “Price” Your order_by_link methods alternate between ASC and DESC, so the featured products would jump from the top the bottom. It presents a lot of “work arounds”. So priority_order solves this.
105 106 107 |
# File 'lib/searchgasm/search/ordering.rb', line 105 def priority_order=(value) @priority_order = value end |
#priority_order_as ⇒ Object
Same as order_as but for your priority order. See priority_order= for more informaton on priority_order.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/searchgasm/search/ordering.rb', line 124 def priority_order_as return if priority_order.blank? return @priority_order_as if @priority_order_as case priority_order when /ASC$/i @priority_order_as = "ASC" when /DESC$/i @priority_order_as = "DESC" else nil end end |
#priority_order_as=(value) ⇒ Object
Same as order_as= but for your priority order. See priority_order= for more informaton on priority_order.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/searchgasm/search/ordering.rb', line 139 def priority_order_as=(value) value = value.blank? ? nil : value.to_s.upcase raise(ArgumentError, "priority_order_as only accepts a blank string / nil or a string as 'ASC' or 'DESC'") if !value.blank? && !["ASC", "DESC"].include?(value) if @priority_order_by @priority_order = order_by_to_order(@priority_order_by, value) elsif priority_order @priority_order.gsub!(/(ASC|DESC)/i, value) end @priority_order_as = value end |
#priority_order_by ⇒ Object
Same as order_by but for your priority order. See priority_order= for more informaton on priority_order.
110 111 112 113 |
# File 'lib/searchgasm/search/ordering.rb', line 110 def priority_order_by return if priority_order.blank? @priority_order_by ||= order_to_order_by(priority_order) end |
#priority_order_by=(value) ⇒ Object
Same as order_by= but for your priority order. See priority_order= for more informaton on priority_order.
116 117 118 119 120 121 |
# File 'lib/searchgasm/search/ordering.rb', line 116 def priority_order_by=(value) @priority_order_by_auto_joins = nil @priority_order_by = get_order_by_value(value) @priority_order = order_by_to_order(@priority_order_by, @priority_order_as) @priority_order_by end |
#priority_order_by_auto_joins ⇒ Object
150 151 152 |
# File 'lib/searchgasm/search/ordering.rb', line 150 def priority_order_by_auto_joins @priority_order_by_auto_joins ||= build_order_by_auto_joins(priority_order_by) end |
#sanitize_with_ordering(searching = true) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/searchgasm/search/ordering.rb', line 154 def sanitize_with_ordering(searching = true) = sanitize_without_ordering(searching) unless priority_order.blank? order_parts = [priority_order, [:order]].compact [:order] = order_parts.join(", ") end end |