Skip to content

Instantly share code, notes, and snippets.

@dankmitchell
Created December 16, 2013 16:25
Show Gist options
  • Save dankmitchell/7989827 to your computer and use it in GitHub Desktop.
Save dankmitchell/7989827 to your computer and use it in GitHub Desktop.
search.rb
module Lovepleasure
class Search < Spree::Core::Search::Base
def initialize(params, session)
self.current_currency = Spree::Config[:currency]
@properties = {}
prepare(params, session)
end
def retrieve_products
if keywords.present?
@products = base_products
@products = @products.joins(:product_stock)
@products = @products.joins("LEFT OUTER JOIN spree_taxons AS brand_taxons ON brand_taxons.id = spree_products.brand_id")
@products = @products.where("( (brand_taxons.id IS NULL AND spree_product_stocks.stock != 0)
OR ( (brand_taxons.show_out_of_stock = 'true')
OR (brand_taxons.show_out_of_stock = 'false'
AND spree_product_stocks.stock != 0)))")
@products = @products.basic_search({name: keywords, description: keywords}, false)
else
@products = get_base_scope
end
curr_page = page || 1
@products = @products.page(curr_page).per(per_page)
@products
end
protected
def get_base_scope
base_scope = Spree::Product.active
base_scope = base_scope.in_taxon(taxon) unless taxon.blank?
base_scope = get_products_conditions_for(base_scope, keywords)
base_scope = add_search_scopes(base_scope)
case ordering
when "name"
base_scope = base_scope.order("spree_products.name ASC")
when "price_asc"
base_scope = base_scope.order("spree_prices.amount ASC")
when "price_desc"
base_scope = base_scope.order("spree_prices.amount DESC")
# when "bestselling"
# base_scope = base_scope.order("spree_product_sales.sold DESC NULLS LAST")
# else
# base_scope = base_scope.order("spree_product_sales.sold DESC NULLS LAST")
end
base_scope
end
def add_search_scopes(base_scope)
search.each do |name, scope_attribute|
scope_name = name.to_sym
if base_scope.respond_to?(:search_scopes) && base_scope.search_scopes.include?(scope_name.to_sym)
base_scope = base_scope.send(scope_name, *scope_attribute)
else
base_scope = base_scope.merge(Spree::Product.ransack({scope_name => scope_attribute}).result)
end
end if search
base_scope
end
# method should return new scope based on base_scope
def get_products_conditions_for(base_scope, query)
unless query.blank?
base_scope = base_scope.like_any([:name, :description], query.split)
end
base_scope
end
def prepare(params, session)
@properties[:taxon] = params[:taxon].blank? ? nil : Spree::Taxon.find(params[:taxon])
@properties[:keywords] = params[:keywords]
@properties[:search] = params[:search]
if params[:keywords].present?
session[:ordering] = nil
@properties[:page] = 1
@properties[:ordering] = nil
else
ordering = session[:ordering] if session[:ordering]
@properties[:ordering] = ordering.blank? ? nil : ordering
end
per_page = params[:per_page].to_i
per_page = session[:display_per_page].to_i if session[:display_per_page]
@properties[:per_page] = per_page > 0 ? per_page : Spree::Config[:products_per_page]
@properties[:page] = (params[:page].to_i <= 0) ? 1 : params[:page].to_i
# Set per page to be crazy high if the user wants to just display all products.
if session[:display_all]
@properties[:page] = 1
@properties[:per_page] = 10_000
end
if session[:reset_page]
session[:reset_page] = nil
@properties[:page] = 1
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment