Commit 79e14153911836ee7cc922233714fad6e1684099

Authored by Andrew Kane
1 parent a86fc93f

Improved order pattern

Showing 2 changed files with 10 additions and 15 deletions   Show diff stats
lib/searchkick/relation.rb
... ... @@ -117,33 +117,27 @@ module Searchkick
117 117 end
118 118  
119 119 # experimental
120   - def order(value)
121   - clone.order!(value)
  120 + def order(*values)
  121 + clone.order!(*values)
122 122 end
123 123  
124 124 # experimental
125   - def order!(value)
  125 + def order!(*values)
  126 + values = values.first if values.size == 1 && values.first.is_a?(Array)
126 127 check_loaded
127   - if @options[:order]
128   - order = @options[:order]
129   - order = [order] unless order.is_a?(Array)
130   - order << value
131   - @options[:order] = order
132   - else
133   - @options[:order] = value
134   - end
  128 + (@options[:order] ||= []).concat(values)
135 129 self
136 130 end
137 131  
138 132 # experimental
139   - def reorder(value)
140   - clone.reorder!(value)
  133 + def reorder(*values)
  134 + clone.reorder!(*values)
141 135 end
142 136  
143 137 # experimental
144   - def reorder!(value)
  138 + def reorder!(*values)
145 139 check_loaded
146   - @options[:order] = value
  140 + @options[:order] = values
147 141 self
148 142 end
149 143  
... ...
test/order_test.rb
... ... @@ -21,6 +21,7 @@ class OrderTest &lt; Minitest::Test
21 21 ]
22 22 assert_order "product", ["Product A", "Product B", "Product C"], order: {color: :asc, store_id: :desc}
23 23 assert_order_relation ["Product A", "Product B", "Product C"], Product.search("product").order(color: :asc, store_id: :desc)
  24 + assert_order_relation ["Product A", "Product B", "Product C"], Product.search("product").order(:color, store_id: :desc)
24 25 assert_order_relation ["Product A", "Product B", "Product C"], Product.search("product").order(color: :asc).order(store_id: :desc)
25 26 assert_order_relation ["Product B", "Product C", "Product A"], Product.search("product").order(color: :asc).reorder(store_id: :desc)
26 27 end
... ...