Sha256: adae3670614689a4cb0b28200a1bfdf5dbdcf0b63446abfcf97b12bbdc3b42e6

Contents?: true

Size: 1.24 KB

Versions: 25

Compression:

Stored size: 1.24 KB

Contents

module Spree
  module Products
    class Sort
      def initialize(scope, params, current_currency)
        @scope    = scope
        @sort     = params[:sort]
        @currency = params[:currency] || current_currency
      end

      def call
        products = updated_at(scope)
        products = price(products)

        products
      end

      private

      attr_reader :sort, :scope, :currency

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

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

      def updated_at?
        sort_field == 'updated_at'
      end

      def price?
        sort_field == 'price'
      end

      def order_direction
        desc_order ? :desc : :asc
      end

      def updated_at(products)
        return products unless updated_at?

        products.order(updated_at: order_direction)
      end

      def price(products)
        return products unless price?

        products.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

25 entries across 25 versions & 1 rubygems

Version Path
spree_core-3.7.14.1 app/sorters/spree/products/sort.rb
spree_core-3.7.14 app/sorters/spree/products/sort.rb
spree_core-3.7.13 app/sorters/spree/products/sort.rb
spree_core-3.7.12 app/sorters/spree/products/sort.rb
spree_core-3.7.11 app/sorters/spree/products/sort.rb
spree_core-3.7.10 app/sorters/spree/products/sort.rb
spree_core-3.7.9 app/sorters/spree/products/sort.rb
spree_core-3.7.8 app/sorters/spree/products/sort.rb
spree_core-4.0.1 app/sorters/spree/products/sort.rb
spree_core-3.7.7 app/sorters/spree/products/sort.rb
spree_core-4.0.0 app/sorters/spree/products/sort.rb
spree_core-4.0.0.rc3 app/sorters/spree/products/sort.rb
spree_core-3.7.6 app/sorters/spree/products/sort.rb
spree_core-3.7.5 app/sorters/spree/products/sort.rb
spree_core-4.0.0.rc2 app/sorters/spree/products/sort.rb
spree_core-4.0.0.rc1 app/sorters/spree/products/sort.rb
spree_core-4.0.0.beta app/sorters/spree/products/sort.rb
spree_core-3.7.4 app/sorters/spree/products/sort.rb
spree_core-3.7.3 app/sorters/spree/products/sort.rb
spree_core-3.7.2 app/sorters/spree/products/sort.rb