Class: Workarea::Search::Admin::CatalogCategory

Inherits:
Workarea::Search::Admin show all
Includes:
Releasable
Defined in:
app/models/workarea/search/admin/catalog_category.rb

Instance Method Summary collapse

Methods included from Releasable

#status

Methods inherited from Workarea::Search::Admin

#created_at, find_for_model, for, #id, jump_to, #jump_to_param, #jump_to_route_helper, #jump_to_search_text, #keywords, #name, #releasable?, #sanitized_keywords, #should_be_indexed?, #status, #type, #updated_at

Methods included from Elasticsearch::Document

all, #as_bulk_document, current_index_prefix, #destroy, #initialize, #save

Instance Method Details

#as_documentObject



57
58
59
# File 'app/models/workarea/search/admin/catalog_category.rb', line 57

def as_document
  super.merge(breadcrumbs: breadcrumbs)
end


61
62
63
# File 'app/models/workarea/search/admin/catalog_category.rb', line 61

def breadcrumbs
  Workarea::Navigation::Breadcrumbs.new(model).join(' > ')
end

#displayable_productsObject



23
24
25
26
27
28
# File 'app/models/workarea/search/admin/catalog_category.rb', line 23

def displayable_products
  view_model = Workarea::Storefront::CategoryViewModel.wrap(model)
  return [] unless view_model.respond_to?(:products)

  view_model.products
end

#facetsObject



19
20
21
# File 'app/models/workarea/search/admin/catalog_category.rb', line 19

def facets
  super.merge(issues: issues)
end

#has_products?Boolean

This funny business exists because displayable_products depends on product browse indexing, which will be delayed (async).

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/workarea/search/admin/catalog_category.rb', line 33

def has_products?
  if displayable_products.blank? && model.product_ids.present?
    # Simulate whether it will show up
    # TODO FIX, this is a HACK please forgive my cursed soul
    products = Catalog::Product.any_in(id: model.product_ids.take(100))
    products.any? do |product|
      search_model = Storefront::Product.new(product)

      product.active? &&
        search_model.variant_count > 0 &&
        search_model.inventory.available_to_sell > 0
    end
  else
    displayable_products.present?
  end
end

#issuesObject



50
51
52
53
54
55
# File 'app/models/workarea/search/admin/catalog_category.rb', line 50

def issues
  result = []
  result << I18n.t('workarea.alerts.issues.no_displayable_products') unless has_products?
  result << I18n.t('workarea.alerts.issues.not_in_taxonomy') unless model.taxon.present?
  result
end

#jump_to_positionObject



15
16
17
# File 'app/models/workarea/search/admin/catalog_category.rb', line 15

def jump_to_position
  4
end

#jump_to_textObject



11
12
13
# File 'app/models/workarea/search/admin/catalog_category.rb', line 11

def jump_to_text
  model.name
end

#search_textObject



7
8
9
# File 'app/models/workarea/search/admin/catalog_category.rb', line 7

def search_text
  "category #{model.name}"
end