Module: Hippo::Concerns::SortingExpressions::ClassMethods

Defined in:
lib/hippo/concerns/sorting_expressions.rb

Instance Method Summary collapse

Instance Method Details

#append_sort_to_query(query, field, dir) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'lib/hippo/concerns/sorting_expressions.rb', line 22

def append_sort_to_query(query, field, dir)
    dir = :asc unless dir == :desc
    if @sorting_expressions && (block = @sorting_expressions[field])
        block.call(query, dir)
    else
        query.order(field.gsub(/[^\w|^\.]/,'') + ' ' +
                      ( ( :asc == dir ) ? 'ASC' : 'DESC' ) )
    end
end

#export_sort(name, &block) ⇒ Object

Add a proc that will be called to add a sort expression to a query. The proc should accept an arel query object and a single direction parameter, which will be a symbol value of :asc or :desc It must return a arel query with the sort applied

Parameters:

  • name (String)

    The name of the expression


13
14
15
16
# File 'lib/hippo/concerns/sorting_expressions.rb', line 13

def export_sort( name, &block )
    @sorting_expressions ||= {}
    @sorting_expressions[name.to_s] = block
end

#has_sorting_expression?(name) ⇒ Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/hippo/concerns/sorting_expressions.rb', line 18

def has_sorting_expression?(name)
    @sorting_expressions && @sorting_expressions[name]
end