Sha256: e14bd34e5e42372b5ae0361ff3045c15722f3bfe270572c6d6121ea01a5ff1a7

Contents?: true

Size: 896 Bytes

Versions: 6

Compression:

Stored size: 896 Bytes

Contents

# frozen_string_literal: true

module Care::AutoFinder
  #
  # Содержит методы для сортировки коллекции
  #
  # @example
  #   class Finder
  #     include Sortable
  #
  #     attr_reader :params
  #
  #     def call
  #       params = { sort: 'name_asc' }
  #       sort(Document)
  #     end
  #   end
  #
  module Sortable
    # def sort(items)
    #   params[:sort].present? ? items.order(order_expiration) : items.order(created_at: :asc)
    # end

    def sort(items)
      if params[:sort].present?
        items.order(order_expiration)
      elsif items.methods.include?(:default_sort)
        items.default_sort
      else
        items.order(created_at: :asc)
      end
    end

    private

    def order_expiration
      order_match_parts = params[:sort].rpartition("_")
      {order_match_parts.first => order_match_parts.last}
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
care-0.1.6 lib/care/auto_finder/sortable.rb
care-0.1.5 lib/care/auto_finder/sortable.rb
care-0.1.4 lib/care/auto_finder/sortable.rb
care-0.1.3 lib/care/auto_finder/sortable.rb
care-0.1.1 lib/care/auto_finder/sortable.rb
care-0.1.0 lib/care/auto_finder/sortable.rb