class Spree::Search::Searchkick

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/spree/search/searchkick.rb, line 7
def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

Public Instance Methods

retrieve_products() click to toggle source
# File lib/spree/search/searchkick.rb, line 21
def retrieve_products
  @products = get_base_search
end

Protected Instance Methods

add_search_attributes(query) click to toggle source
# File lib/spree/search/searchkick.rb, line 70
def add_search_attributes(query)
  return query unless search
  search.each do |name, scope_attribute|
    query.merge!(Hash[name, scope_attribute])
  end

  query
end
add_search_filters(search) click to toggle source
# File lib/spree/search/searchkick.rb, line 79
def add_search_filters(search)
  return search unless filters
  all_filters = taxon ? taxon.applicable_filters : Spree::Core::SearchkickFilters.all_filters

  applicable_filters = {}

  # Find filter method definition from filters passed in
  filters.to_a.each do |filter_param|
    search_filter, search_labels = filter_param
    filter = all_filters.find { |filter| filter[:scope] == search_filter.to_sym }
    applicable_filters[search_filter.to_sym] = filter[:conds].find_all { |filter_condition| search_labels.include?(filter_condition.first) }
  end

  # Loop through the applicable filters, collect the conditions, and generate filter options
  filter_items = []
  applicable_filters.each do |applicable_filter|
    filter_name, filter_details = applicable_filter
    filter_options = []
    filter_details.each do |details|
      label, conditions = details
      filter_options << conditions
    end

    # Add filter_options to filter_items for the conditions from an applicable_filter
    filter_items << {
      bool: {
        should: filter_options
      }
    }
  end

  # Set search_filters with filter_items defined above
  search_filters = {
    bool: {
      must: filter_items
    }
  }

  # Update the search query filter hash in order to process the additional filters as well as the base_search
  search.body[:query][:bool][:filter].push(search_filters)
  search
end
includes_clause() click to toggle source
# File lib/spree/search/searchkick.rb, line 122
def includes_clause
  includes_clause =  { master: [:currently_valid_prices] }
  includes_clause[:master] << :images if include_images
  includes_clause
end
prepare(params) click to toggle source
Calls superclass method
# File lib/spree/search/searchkick.rb, line 128
def prepare(params)
  @properties[:query] = params[:query].blank? ? nil : params[:query]
  @properties[:filters] = params[:filter].blank? ? nil : params[:filter]
  @properties[:fields] = params[:fields].blank? ? nil : params[:fields]
  @properties[:searchkick_options] = params[:searchkick_options].blank? ? {} : params[:searchkick_options].deep_symbolize_keys
  params = params.deep_symbolize_keys
  super
end
where_clause() click to toggle source
# File lib/spree/search/searchkick.rb, line 57
def where_clause
  # Default items for where_clause
  where_clause = {
    active: true,
    currency: pricing_options.currency,
    price: { not: nil }
  }
  where_clause.merge!({taxon_ids: taxon.id}) if taxon

  # Add search attributes from params[:search]
  add_search_attributes(where_clause)
end