module Arrangeable

Constants

VERSION

Public Instance Methods

arrange(order_param) click to toggle source
# File lib/arrangeable.rb, line 11
def arrange(order_param)
  order_by_clause = order_string(order_param)
  order(order_by_clause)
end
arrangeable_fields() click to toggle source

array of strings of fields that support being sorted by

# File lib/arrangeable.rb, line 17
def arrangeable_fields
  raise NotImplementedError, 'You need to overwrite this method in the calling class'
end
order_string(sort_string) click to toggle source
# File lib/arrangeable.rb, line 23
def order_string(sort_string)
  keys = sort_string.gsub(' ', '').split(',').uniq.reject(&:blank?)
  keys << 'id' unless keys.include?('id') || keys.include?('-id')
  keys.map do |key|
    order = ' ASC'
    if key[0] == '-'
      order = ' DESC'
      key = key[1..-1]
    end
    next unless arrangeable_fields.include?(key)
    key + order
  end.compact.join(', ')
end