Class: Workarea::Categorization

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/queries/workarea/categorization.rb

Instance Method Summary collapse

Constructor Details

#initialize(product = nil) ⇒ Categorization

Returns a new instance of Categorization.



6
7
8
# File 'app/queries/workarea/categorization.rb', line 6

def initialize(product = nil)
  @product = product
end

Instance Method Details

#defaultObject



23
24
25
# File 'app/queries/workarea/categorization.rb', line 23

def default
  default_model.try(:id)
end

#default_modelObject



14
15
16
17
18
19
20
21
# File 'app/queries/workarea/categorization.rb', line 14

def default_model
  key = [@product.cache_key, 'default_category', Release.current&.id].compact.join('/')

  @default_model ||= Rails.cache.fetch(key, expires_in: Workarea.config.cache_expirations.products_default_category) do
    manual_default_model ||
      to_models.sort_by(&:created_at).select(&:active?).first
  end
end

#each(&block) ⇒ Object



10
11
12
# File 'app/queries/workarea/categorization.rb', line 10

def each(&block)
  all.each(&block)
end

#manualObject



27
28
29
# File 'app/queries/workarea/categorization.rb', line 27

def manual
  manual_models.map(&:id)
end

#manual_default_modelObject



40
41
42
43
44
45
46
# File 'app/queries/workarea/categorization.rb', line 40

def manual_default_model
  return unless @product.default_category_id.present?

  @manual_default_model ||=
    Catalog::Category.where(id: @product.default_category_id)
                     .detect { |c| c.active? && c.id.to_s.in?(all)  }
end

#queriesObject



31
32
33
34
# File 'app/queries/workarea/categorization.rb', line 31

def queries
  return [] if @product.blank?
  @queries ||= Search::Storefront::CategoryQuery.find_by_product(@product)
end

#to_modelsObject



36
37
38
# File 'app/queries/workarea/categorization.rb', line 36

def to_models
  @to_models ||= (manual_models + query_models).uniq
end