has_search_sortable.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module HasSearchSortable
  3. extend ActiveSupport::Concern
  4. # methods defined here are going to extend the class, not the instance of it
  5. class_methods do
  6. =begin
  7. This function will check the params for the "sort_by" attribute
  8. and validate its values.
  9. sort_by = search_get_sort_by(params, default)
  10. returns
  11. sort_by = [
  12. 'created_at',
  13. 'updated_at',
  14. ]
  15. =end
  16. def search_get_sort_by(params, default)
  17. sort_by = []
  18. if params[:sort_by].present? && params[:sort_by].is_a?(String)
  19. params[:sort_by] = [params[:sort_by]]
  20. elsif params[:sort_by].blank?
  21. params[:sort_by] = []
  22. end
  23. # check order
  24. params[:sort_by].each do |value|
  25. # only accept values which are set for the db schema
  26. raise "Found invalid column '#{value}' for sorting." if columns_hash[value].blank?
  27. sort_by.push(value)
  28. end
  29. if sort_by.blank?
  30. if default.is_a?(Array)
  31. sort_by = default
  32. else
  33. sort_by.push(default)
  34. end
  35. end
  36. sort_by
  37. end
  38. =begin
  39. This function will check the params for the "order_by" attribute
  40. and validate its values.
  41. order_by = search_get_order_by(params, default)
  42. returns
  43. order_by = [
  44. 'asc',
  45. 'desc',
  46. ]
  47. =end
  48. def search_get_order_by(params, default)
  49. order_by = []
  50. if params[:order_by].present? && params[:order_by].is_a?(String)
  51. params[:order_by] = [ params[:order_by] ]
  52. elsif params[:order_by].blank?
  53. params[:order_by] = []
  54. end
  55. # check order
  56. params[:order_by].each do |value|
  57. raise "Found invalid order by value #{value}. Please use 'asc' or 'desc'." if !value.match?(/\A(asc|desc)\z/i)
  58. order_by.push(value.downcase)
  59. end
  60. if order_by.blank?
  61. if default.is_a?(Array)
  62. order_by = default
  63. else
  64. order_by.push(default)
  65. end
  66. end
  67. order_by
  68. end
  69. =begin
  70. This function will use the evaluated values for sort_by and
  71. order_by to generate the ORDER-SELECT sql statement for the sorting
  72. of the result.
  73. sort_by = [ 'created_at', 'updated_at' ]
  74. order_by = [ 'asc', 'desc' ]
  75. default = 'tickets.created_at'
  76. sql = search_get_order_select_sql(sort_by, order_by, default)
  77. returns
  78. sql = 'tickets.created_at, tickets.updated_at'
  79. =end
  80. def search_get_order_select_sql(sort_by, order_by, default)
  81. sql = []
  82. sort_by.each_with_index do |value, index|
  83. next if value.blank?
  84. next if order_by[index].blank?
  85. sql.push( "#{ActiveRecord::Base.connection.quote_table_name(table_name)}.#{ActiveRecord::Base.connection.quote_column_name(value)}" )
  86. end
  87. if sql.blank?
  88. sql.push("#{ActiveRecord::Base.connection.quote_table_name(table_name)}.#{ActiveRecord::Base.connection.quote_column_name(default)}")
  89. end
  90. sql.join(', ')
  91. end
  92. =begin
  93. This function will use the evaluated values for sort_by and
  94. order_by to generate the ORDER- sql statement for the sorting
  95. of the result.
  96. sort_by = [ 'created_at', 'updated_at' ]
  97. order_by = [ 'asc', 'desc' ]
  98. default = 'tickets.created_at DESC'
  99. sql = search_get_order_sql(sort_by, order_by, default)
  100. returns
  101. sql = 'tickets.created_at ASC, tickets.updated_at DESC'
  102. =end
  103. def search_get_order_sql(sort_by, order_by, default)
  104. sql = []
  105. sort_by.each_with_index do |value, index|
  106. next if value.blank?
  107. next if order_by[index].blank?
  108. sql.push( "#{ActiveRecord::Base.connection.quote_table_name(table_name)}.#{ActiveRecord::Base.connection.quote_column_name(value)} #{order_by[index]}" )
  109. end
  110. if sql.blank?
  111. sql.push("#{ActiveRecord::Base.connection.quote_table_name(table_name)}.#{ActiveRecord::Base.connection.quote_column_name(default)}")
  112. end
  113. sql.join(', ')
  114. end
  115. end
  116. end