Class: QueryHelper::SqlSort

Inherits:
Object
  • Object
show all
Defined in:
lib/query_helper/sql_sort.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sort_string: "", sort_tiebreak: "", column_maps: [], column_sort_order: {}, keep_nulls_last: false) ⇒ SqlSort

Returns a new instance of SqlSort.



8
9
10
11
12
13
14
15
# File 'lib/query_helper/sql_sort.rb', line 8

def initialize(sort_string: "", sort_tiebreak: "", column_maps: [], column_sort_order: {}, keep_nulls_last: false)
  @sort_string = sort_string
  @column_maps = column_maps
  @sort_tiebreak = sort_tiebreak
  @column_sort_order = column_sort_order
  @select_strings = []
  @keep_nulls_last = keep_nulls_last
end

Instance Attribute Details

#column_mapsObject

Returns the value of attribute column_maps.



6
7
8
# File 'lib/query_helper/sql_sort.rb', line 6

def column_maps
  @column_maps
end

#column_sort_orderObject

Returns the value of attribute column_sort_order.



6
7
8
# File 'lib/query_helper/sql_sort.rb', line 6

def column_sort_order
  @column_sort_order
end

#select_stringsObject

Returns the value of attribute select_strings.



6
7
8
# File 'lib/query_helper/sql_sort.rb', line 6

def select_strings
  @select_strings
end

#sort_tiebreakObject

Returns the value of attribute sort_tiebreak.



6
7
8
# File 'lib/query_helper/sql_sort.rb', line 6

def sort_tiebreak
  @sort_tiebreak
end

Instance Method Details

#attributes_sql_expression(sort_attribute) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/query_helper/sql_sort.rb', line 28

def attributes_sql_expression(sort_attribute)
  sql_strings = []
  if sort_attribute.present?
    sorts = sort_attribute.split(",")
    sorts.each_with_index do |sort, index|
      sort_alias = sort.split(":")[0]
      direction = sort.split(":")[1]
      modifier = sort.split(":")[2]
      begin
        sql_expression = @column_maps.find{ |m| m.alias_name.casecmp?(sort_alias) }.sql_expression
      rescue NoMethodError => e
        raise InvalidQueryError.new("Sorting not allowed on column '#{sort_alias}'")
      end

      if direction == "desc"
        case ActiveRecord::Base.connection.adapter_name
        when "SQLite" # SQLite is used in the test suite
          direction = @keep_nulls_last ? "desc nulls last" : "desc"
        else
          direction = "desc nulls last"
        end
      else
        direction = @keep_nulls_last ? "asc nulls last" : "asc"
      end

      case modifier
      when "lowercase"
        sql_expression = "lower(#{sql_expression})"
        # When select distincts are used, the order by clause must be included in the select clause
        @select_strings << sql_expression
      end

      sql_strings << "#{sql_expression} #{direction}"
    end
  end
  sql_strings << parse_custom_sort_string if @column_sort_order.present? && @column_sort_order[:sort_values].present?
  sql_strings
end

#parse_custom_sort_stringObject

This method is used for sorting enum based column



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/query_helper/sql_sort.rb', line 68

def parse_custom_sort_string
  sort_column = @column_sort_order[:column_name]
  sort_values = @column_sort_order[:sort_values]
  direction = @column_sort_order[:direction]

  sql_expression = '(CASE'
  sort_values.each_with_index do |value, index|
      sql_expression << " WHEN #{sort_column}=#{value} THEN #{index}"
  end
  sql_expression << " END) #{direction}" 
  sql_expression
end

#parse_sort_stringObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/query_helper/sql_sort.rb', line 17

def parse_sort_string
  return [] if @sort_string.blank? && @sort_tiebreak.blank? && @column_sort_order.blank?

  return attributes_sql_expression(@sort_tiebreak) if @sort_string.blank?

  sql_strings = attributes_sql_expression(@sort_string)
  return sql_strings if @sort_tiebreak.blank?

  sql_strings + attributes_sql_expression(@sort_tiebreak)
end