Class: Workarea::Recommendation::OrderBased

Inherits:
Object
  • Object
show all
Defined in:
app/queries/workarea/recommendation/order_based.rb

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderBased

Returns a new instance of OrderBased.



4
5
6
# File 'app/queries/workarea/recommendation/order_based.rb', line 4

def initialize(order)
  @order = order
end

Instance Method Details

#max_resultsObject



42
43
44
45
# File 'app/queries/workarea/recommendation/order_based.rb', line 42

def max_results
  # accommodate some missing or undisplayable products
  Workarea.config.per_page
end

#order_category_idsObject



38
39
40
# File 'app/queries/workarea/recommendation/order_based.rb', line 38

def order_category_ids
  @order.items.map(&:category_ids).flatten
end

#order_product_idsObject



34
35
36
# File 'app/queries/workarea/recommendation/order_based.rb', line 34

def order_product_ids
  @order.items.map(&:product_id)
end

#prediction_product_idsObject



27
28
29
30
31
32
# File 'app/queries/workarea/recommendation/order_based.rb', line 27

def prediction_product_ids
  ProductPredictor.new.predictions_for(
    item_set: order_product_ids,
    limit: max_results
  )
end


17
18
19
20
21
22
23
24
25
# File 'app/queries/workarea/recommendation/order_based.rb', line 17

def related_product_ids
  query = Workarea::Search::RelatedProducts.new(
    product_ids: order_product_ids,
    category_ids: order_category_ids,
    exclude_product_ids: order_product_ids
  )

  query.results.map { |r| r[:catalog_id] }.first(max_results)
end

#resultsObject



8
9
10
11
12
13
14
15
# File 'app/queries/workarea/recommendation/order_based.rb', line 8

def results
  @results ||=
    begin
      result = prediction_product_ids
      return result if result.size == max_results
      (result + related_product_ids).uniq.take(max_results)
    end
end