Sha256: cc8d29fd93b6b781d9eda30168fb761d8a9397b1b278bd1beafc9fcd799ee6b8

Contents?: true

Size: 924 Bytes

Versions: 4

Compression:

Stored size: 924 Bytes

Contents

module SpreeMobility::CoreExt::Spree::Products::FindWithMobilityDecorator
  private
  # The issue here is that ordering by translated attr (e.g. name) will add
  # an ORDER BY translations_table.name, but the query has a SELECT DISTINCT,
  # which would require the translations_table.name to be added to the SELECT
  # So when the order is added, the appropriate SELECT should be added as well.
  def ordered(products)
    case sort_by
    when 'name-a-z'
      ordered_name(products, :asc)
    when 'name-z-a'
      ordered_name(products, :desc)
    else
      super
    end
  end

  def ordered_name(products, direction)
    helper = SpreeMobility::TranslationQuery.new(::Spree::Product.mobility_backend_class(:name))
    helper.add_joins(products).
    select(products.arel.projections, "#{helper.col_name(:name)} AS _name").
    order(Arel.sql(helper.col_name(:name) + ((direction == :desc) ? ' DESC' : '')))
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_mobility-1.4.0 lib/spree_mobility/core_ext/spree/products/find_with_mobility_decorator.rb
spree_mobility-1.3.0 lib/spree_mobility/core_ext/spree/products/find_with_mobility_decorator.rb
spree_mobility-1.2.0 lib/spree_mobility/core_ext/spree/products/find_with_mobility_decorator.rb
spree_mobility-1.1.0 lib/spree_mobility/core_ext/spree/products/find_with_mobility_decorator.rb