Sha256: e1dbc7cadfb5655384da29baea13469f151fa2fe7d14d6e0e40dc24ef404912a

Contents?: true

Size: 921 Bytes

Versions: 14

Compression:

Stored size: 921 Bytes

Contents

module Spree
  module Products
    class Sort < ::Spree::BaseSorter
      def initialize(scope, current_currency, params = {}, allowed_sort_attributes = [])
        super(scope, params, allowed_sort_attributes)
        @currency = params[:currency] || current_currency
      end

      def call
        products = by_param_attribute(scope)
        products = by_price(products)

        products.distinct
      end

      private

      attr_reader :sort, :scope, :currency, :allowed_sort_attributes

      def price?
        sort_field == 'price'
      end

      def by_price(scope)
        return scope unless price?

        scope.joins(master: :prices).
          select("#{Spree::Product.table_name}.*, #{Spree::Price.table_name}.amount").
          distinct.
          where(spree_prices: { currency: currency }).
          order("#{Spree::Price.table_name}.amount #{order_direction}")
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

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