Class: QueryHelper::SqlSort
- Inherits:
-
Object
- Object
- QueryHelper::SqlSort
- Defined in:
- lib/query_helper/sql_sort.rb
Instance Attribute Summary collapse
-
#column_maps ⇒ Object
Returns the value of attribute column_maps.
-
#select_strings ⇒ Object
Returns the value of attribute select_strings.
Instance Method Summary collapse
-
#initialize(sort_string: "", column_maps: []) ⇒ SqlSort
constructor
A new instance of SqlSort.
- #parse_sort_string ⇒ Object
Constructor Details
#initialize(sort_string: "", column_maps: []) ⇒ SqlSort
Returns a new instance of SqlSort.
8 9 10 11 12 |
# File 'lib/query_helper/sql_sort.rb', line 8 def initialize(sort_string: "", column_maps: []) @sort_string = sort_string @column_maps = column_maps @select_strings = [] end |
Instance Attribute Details
#column_maps ⇒ Object
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 |
#select_strings ⇒ Object
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 |
Instance Method Details
#parse_sort_string ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/query_helper/sql_sort.rb', line 14 def parse_sort_string sql_strings = [] sorts = @sort_string.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 == 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 = "desc" else direction = "desc nulls last" end else direction = "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 return sql_strings end |