Sha256: fd066ec87be4ff419a63d2d0b10d02577a84782bcfb5ccd4f7efc6f8a52d9f45

Contents?: true

Size: 776 Bytes

Versions: 14

Compression:

Stored size: 776 Bytes

Contents

module Spree
  class BaseSorter
    def initialize(scope, params = {}, allowed_sort_attributes = [])
      @scope = scope
      @sort = params[:sort]
      @allowed_sort_attributes = allowed_sort_attributes
    end

    def call
      by_param_attribute(scope)
    end

    protected

    attr_reader :scope, :collection, :sort, :allowed_sort_attributes

    def by_param_attribute(scope)
      return scope if sort_field.blank? || !allowed_sort_attributes.include?(sort_field.to_sym)

      scope.order("#{sort_field}": order_direction)
    end

    def desc_order
      @desc_order ||= String(sort)[0] == '-'
    end

    def sort_field
      @sort_field ||= desc_order ? sort[1..-1] : sort
    end

    def order_direction
      desc_order ? :desc : :asc
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-4.2.7 app/sorters/spree/base_sorter.rb
spree_core-4.2.6 app/sorters/spree/base_sorter.rb
spree_core-4.3.0.rc2 app/sorters/spree/base_sorter.rb
spree_core-4.3.0.rc1 app/sorters/spree/base_sorter.rb
spree_core-4.2.5 app/sorters/spree/base_sorter.rb
spree_core-4.2.4 app/sorters/spree/base_sorter.rb
spree_core-4.2.3.1 app/sorters/spree/base_sorter.rb
spree_core-4.2.3 app/sorters/spree/base_sorter.rb
spree_core-4.2.2 app/sorters/spree/base_sorter.rb
spree_core-4.2.1 app/sorters/spree/base_sorter.rb
spree_core-4.2.0 app/sorters/spree/base_sorter.rb
spree_core-4.2.0.rc5 app/sorters/spree/base_sorter.rb
spree_core-4.2.0.rc4 app/sorters/spree/base_sorter.rb
spree_core-4.2.0.rc3 app/sorters/spree/base_sorter.rb