class Tablets::Data::Processing::Order

Incapsulate relation ordering logic

Public Instance Methods

apply(relation) click to toggle source

Applies order processing on relation

# File lib/tablets/data/processing/order.rb, line 9
def apply(relation)
  params[:order].values.inject(relation) do |rel, item|
    sorting_direction = direction(item)
    nulls = null_place(sorting_direction)
    rel.order("#{column(item)} #{sorting_direction} #{nulls}")
  end
end

Private Instance Methods

column(item) click to toggle source

Determines order column from params

# File lib/tablets/data/processing/order.rb, line 20
def column(item)
  columns[item[:column].to_i][:order]
end
direction(item) click to toggle source

Determines order direction from params (ASC by default)

# File lib/tablets/data/processing/order.rb, line 25
def direction(item)
  if %w(ASC DESC).include?(item.fetch(:dir, '').upcase)
    item[:dir].upcase
  else
    'ASC'
  end
end
null_place(sort) click to toggle source

Determines where nulls appear

# File lib/tablets/data/processing/order.rb, line 34
def null_place(sort)
  if sort.upcase == 'ASC'
    'NULLS FIRST'
  else
    'NULLS LAST'
  end
end