Class: ProductLine::PublicationRetriever

Inherits:
BaseService show all
Defined in:
app/services/product_line/publication_retriever.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(product_line_url, categories: nil, locale: nil, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil) ⇒ Object

A Result object with

  • +all_publications+ - an array of all publications combined
  • +publications_grouped+ - a hash of publications grouped by their category


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/product_line/publication_retriever.rb', line 11

def process(product_line_url, categories: nil, locale: nil, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil)
  locale ||= I18n.locale unless locale == :all
  store_id = Store.store_id_for_locale(locale) unless locale == :all || locale.nil?
  categories = %i[support sales] if categories.blank?
  categories = [categories].flatten.compact
  publications = []

  if categories.include?(:all) || categories.blank?
    publications += retrieve_all_publications(product_line_url, store_id,
                                              tags:,
                                              product_category_ids:,
                                              ignore_item_specific_documents:,
                                              publication_category_paths:)
  else
    if categories.include?(:support)
      publications += retrieve_support_publications(product_line_url, store_id,
                                                    tags:,
                                                    product_category_ids:,
                                                    ignore_item_specific_documents:,
                                                    publication_category_paths:)
    end
    if categories.include?(:sales)
      publications += retrieve_sales_publications(product_line_url, store_id,
                                                  tags:,
                                                  product_category_ids:,
                                                  ignore_item_specific_documents:,
                                                  publication_category_paths:)
    end
  end

  publications = publications.compact.sort_by { |p| [p.product_category_priority, (p.name.presence || p.name_en).downcase] }.uniq
  publications_grouped = publications.group_by(&:product_category)

  Result.new(all_publications: publications, publications_grouped:)
end

#publication_query(query, product_line_paths, store_id: nil, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil) ⇒ Object

Uses ltree paths for efficient queries (no additional DB lookups for path resolution)

Parameters:

  • publication_category_paths (String, Array<String>, nil) (defaults to: nil)

    ltree paths (e.g., LtreePaths::PC_PUBLICATIONS_INSTALLATION)

  • query (Object)
  • product_line_paths (Object)
  • store_id (Object, nil) (defaults to: nil)
  • tags (Object, nil) (defaults to: nil)
  • product_category_ids (Object, nil) (defaults to: nil)
  • ignore_item_specific_documents (Boolean) (defaults to: false)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/product_line/publication_retriever.rb', line 83

def publication_query(query, product_line_paths, store_id: nil, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil)
  # We use full tree query because if provided e.g snow_melting.mat.120_v we want snow_melting.mat and snow_melting publications
  logger.debug "Publication query with product_line_paths: #{product_line_paths.inspect}, product_category_ids: #{product_category_ids.inspect}, store_id: #{store_id.inspect}, tags: #{tags.inspect}, ignore_item_specific_documents: #{ignore_item_specific_documents}"
  res = query.publications.with_publication_attached.active.includes(:product_category).by_product_line_path_full(product_line_paths)
  res = res.in_store(store_id) if store_id.present?
  res = res.where.not(ignore_product_line_on_item_view: true) if ignore_item_specific_documents
  res = res.tagged_with(tags) if tags.present?
  # Use ltree paths directly for hierarchical query (no DB lookup needed)
  if publication_category_paths.present?
    paths = [publication_category_paths].flatten.compact
    res = res.by_product_category_path(paths.first) if paths.any?
  end
  if product_category_ids.present?
    # Take publications tagged specifically for this product category or which don't have a product category specified
    # e.g if you ask for goods-radiant-panel-hardwired you get goods, radiant-panel, and hardwired, and nil
    pc_ids = ProductCategory.self_and_ancestors_ids([product_category_ids].flatten.compact) + [nil]
    res = res.where(secondary_product_category_id: pc_ids)
  end
  res
end

#retrieve_all_publications(product_line_url, store_id, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'app/services/product_line/publication_retriever.rb', line 66

def retrieve_all_publications(product_line_url, store_id, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil)
  pl = ProductLine.find_by(slug_ltree: LtreePaths.slug_ltree_from_legacy_hyphen_url(product_line_url)) ||
       ProductLine.find_by(slug_ltree: product_line_url)
  return [] unless pl

  resolved_pl_paths = [pl.get_first_public&.ltree_path_slugs, pl.ltree_path_slugs].compact.uniq
  publication_query(Item.publications, resolved_pl_paths, store_id:, tags:, product_category_ids:, ignore_item_specific_documents:, publication_category_paths:)
end

#retrieve_sales_publications(product_line_url, store_id, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/services/product_line/publication_retriever.rb', line 57

def retrieve_sales_publications(product_line_url, store_id, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil)
  pl = ProductLine.find_by(slug_ltree: LtreePaths.slug_ltree_from_legacy_hyphen_url(product_line_url)) ||
       ProductLine.find_by(slug_ltree: product_line_url)
  return [] unless pl

  resolved_pl_paths = [pl.get_first_public_for_sales&.ltree_path_slugs, pl.ltree_path_slugs].compact.uniq
  publication_query(Item.publications_for_sales_portal, resolved_pl_paths, store_id:, tags:, product_category_ids:, ignore_item_specific_documents:, publication_category_paths:)
end

#retrieve_support_publications(product_line_url, store_id, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'app/services/product_line/publication_retriever.rb', line 47

def retrieve_support_publications(product_line_url, store_id, tags: nil, product_category_ids: nil, ignore_item_specific_documents: false, publication_category_paths: nil)
  # Get the product line and its first public for support - use ltree paths directly
  pl = ProductLine.find_by(slug_ltree: LtreePaths.slug_ltree_from_legacy_hyphen_url(product_line_url)) ||
       ProductLine.find_by(slug_ltree: product_line_url)
  return [] unless pl

  resolved_pl_paths = [pl.get_first_public_for_support&.ltree_path_slugs, pl.ltree_path_slugs].compact.uniq
  publication_query(Item.publications_for_support_portal, resolved_pl_paths, store_id:, tags:, product_category_ids:, ignore_item_specific_documents:, publication_category_paths:)
end