Class: Workarea::Recommendation::ProductBased

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

Instance Method Summary collapse

Constructor Details

#initialize(product) ⇒ ProductBased

Returns a new instance of ProductBased.



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

def initialize(product)
  @product = product
  @settings = Settings.find_or_initialize_by(id: @product.id)
end

Instance Method Details

#customObject



30
31
32
# File 'app/queries/workarea/recommendation/product_based.rb', line 30

def custom
  @settings.product_ids
end

#max_resultsObject



9
10
11
12
# File 'app/queries/workarea/recommendation/product_based.rb', line 9

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

#purchasedObject



34
35
36
37
38
# File 'app/queries/workarea/recommendation/product_based.rb', line 34

def purchased
  @purchased ||= Recommendation::ProductPredictor
                  .new
                  .similarities_for(@product.id, limit: max_results)
end

#resultsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/queries/workarea/recommendation/product_based.rb', line 14

def results
  @results ||=
    begin
      results = []

      @settings.sources.each do |source|
        results.push(*send(source))
        results.uniq!

        break if results.length >= max_results
      end

      results.take(max_results)
    end
end

#similarObject



40
41
42
43
44
# File 'app/queries/workarea/recommendation/product_based.rb', line 40

def similar
  @similar ||= Search::RelatedProducts.new(product_ids: @product.id)
                .results
                .map { |r| r[:catalog_id] }
end