app/components/kadmin/finder.rb in kadmin-1.0.9 vs app/components/kadmin/finder.rb in kadmin-1.1.0
- old
+ new
@@ -11,17 +11,24 @@
attr_reader :filters
# @return [ActiveRecord::Relation] the base relation to find items from
attr_reader :scope
+ # @return [String] name of column that is used in ORDER_BY clause
+ attr_reader :sort_column
+
+ # @return [bool] true if sort order is ASC, false if DESC
+ attr_reader :sort_asc
+
# @param [ActiveRecord::Relation] scope base relation to page/filter on
def initialize(scope)
@scope = scope
@pager = nil
@filters = {}
@results = nil
@filtering = false
+ @sort_asc = true
end
# @param [String] name the filter name (should be unique)
# @param [String, Array<String>] column the column(s) name to filter on
# @param [String, Array<String>] value the value or values to look for (OR'd)
@@ -31,9 +38,18 @@
@filtering = true
@scope = filter.apply(@scope, value)
end
return self
+ end
+
+ # sets sort order for scope, any existing order is overwritten
+ # @param [String] name of column to sort
+ # @param [bool] true for ASC sort, false otherwise
+ def order(sort_column, sort_asc)
+ @sort_column = sort_column
+ @sort_asc = sort_asc
+ @scope = @scope.reorder("#{sort_column} #{sort_asc ? 'ASC' : 'DESC'}")
end
def filtering?
return @filtering
end