Sha256: 8b7d543562da5a1757a2efb23693559a8a63746a0709cc3d3786486d446e5128

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

class BMC::Sorter
  include BMC::SortingHelper

  attr_reader :collection, :sort_param, :options, :column, :direction

  def self.call(...)
    new(...).call
  end

  def initialize(collection, sort_param, options = {})
    @collection = collection
    @sort_param = sort_param
    @options = options
    @column, @direction = sortable_column_order(sort_param.to_s)
  end

  def sort
    raise NotImplementedError
  end

  def call
    # Don't replace by `collection.reorder(sort)`
    # #sort can change #collection and must be called before #collection
    order = sort
    collection.reorder(order)
  end

  private

  def inverted_direction
    {asc: :desc, desc: :asc}[direction]
  end

  def joins(...)
    @collection = @collection.joins(...)
  end

  def left_joins(...)
    @collection = @collection.left_joins(...)
  end

  def by_column(c = column)
    {c => direction}
  end

  def by_lower(c = column)
    Arel.sql "LOWER(#{c}) #{direction}"
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bmc-1.6.2 app/sorters/bmc/sorter.rb
bmc-1.6.1 app/sorters/bmc/sorter.rb
bmc-1.6.0 app/sorters/bmc/sorter.rb
bmc-1.5.1 app/sorters/bmc/sorter.rb