Sha256: babdc5ca095c880c3402cbfb0dbbe3d73203c3b8f0890e14b25b0abe3f477153

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

Spree::Product.class_eval do
  # Run after initialization, allows us to process product_decorator from application before this
  Rails.application.config.after_initialize do
    # Check if searchkick_options have been set by the application using this gem
    # If they have, then do not initialize searchkick on the model. If they have not, then set the defaults
    searchkick word_start: [:name] unless Spree::Product.try(:searchkick_options)
  end

  def search_data
    json = {
      name: name,
      description: description,
      active: available?,
      price: price,
      currency: cost_currency,
      sku: sku,
      conversions: orders.complete.count,
      taxon_ids: taxon_and_ancestors.map(&:id),
      taxon_names: taxon_and_ancestors.map(&:name)
    }

    json
  end

  def taxon_and_ancestors
    taxons.map(&:self_and_ancestors).flatten.uniq
  end

  def self.autocomplete(keywords)
    if keywords
      Spree::Product.search(
        keywords,
        fields: ['name^5'],
        match: :word_start,
        limit: 10,
        load: false,
        misspellings: { below: 3 },
        where: search_where,
      ).map(&:name).map(&:strip).uniq
    else
      Spree::Product.search(
        '*',
        where: search_where
      ).map(&:name).map(&:strip).uniq
    end
  end

  def self.search_where
    {
      active: true,
      price: { not: nil }
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_searchkick-0.3.1 app/models/spree/product_decorator.rb
solidus_searchkick-0.3.0 app/models/spree/product_decorator.rb
solidus_searchkick-0.2.3 app/models/spree/product_decorator.rb