Class: Item::PublicationRetriever

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

Overview

Service object: publication retriever.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

#initialize(options = {}) ⇒ PublicationRetriever

Returns a new instance of PublicationRetriever.



11
12
13
14
# File 'app/services/item/publication_retriever.rb', line 11

def initialize(options = {})
  @pl_retriever = ProductLine::PublicationRetriever.new
  super
end

Instance Attribute Details

#pl_retrieverObject (readonly)

Returns the value of attribute pl_retriever.



9
10
11
# File 'app/services/item/publication_retriever.rb', line 9

def pl_retriever
  @pl_retriever
end

Instance Method Details

#get_item_product_line_urls(item, product_line_urls: nil, all_product_lines: false) ⇒ Array<String>

Retrieves the URLs of the product lines associated with the given item.

Parameters:

  • item (Item)

    the item to retrieve the product line URLs for

  • product_line_urls (Object, nil) (defaults to: nil)
  • all_product_lines (Boolean) (defaults to: false)

Returns:

  • (Array<String>)

    the URLs of the product lines associated with the item



106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/item/publication_retriever.rb', line 106

def get_item_product_line_urls(item, product_line_urls: nil, all_product_lines: false)
  pl_urls = []
  if all_product_lines
    pl_urls << item.primary_product_line&.slug_ltree&.to_s
    pl_urls += item.product_lines.pluck(:slug_ltree).map(&:to_s)
  elsif product_line_urls.present?
    pl_urls = product_line_urls & @item.product_lines.pluck(:slug_ltree).map(&:to_s)
  else # primary only
    pl_urls = [item.primary_product_line&.slug_ltree&.to_s].compact
  end
  pl_urls.compact.uniq
end

#get_publications_for_product_line_urls(product_line_urls:, product_category_ids:, categories:, locale:, tags:, publication_category_paths:) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/services/item/publication_retriever.rb', line 119

def get_publications_for_product_line_urls(product_line_urls:, product_category_ids:, categories:, locale:, tags:, publication_category_paths:)
  product_line_urls.flat_map do |product_line_url|
    logger.debug "Retrieving publications for product line #{product_line_url.inspect}, product_category_ids: #{product_category_ids.inspect}"
    res = pl_retriever.process(product_line_url,
                               product_category_ids:,
                               categories:,
                               locale:,
                               tags:,
                               publication_category_paths:,
                               ignore_item_specific_documents: true)
    res.all_publications
  end
end

#process(item_sku, categories: nil, locale: nil, all_product_lines: false, product_line_urls: nil, tags: nil, publication_category_paths: nil, include_product_lines_publications: true) ⇒ Object

A Result object with

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


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/item/publication_retriever.rb', line 19

def process(item_sku, categories: nil,
            locale: nil,
            all_product_lines: false,
            product_line_urls: nil,
            tags: nil,
            publication_category_paths: nil,
            include_product_lines_publications: true)
  publications = []
  publications_grouped = {}
  categories = %i[support sales] if categories.blank?

  logger.tagged("Item::PublicationRetriever(#{item_sku})") do
    locale ||= I18n.locale unless locale == :all
    store_id = Store.store_id_for_locale(locale)
    @item = item_sku.is_a?(Item) ? item_sku : Item.find_by(sku: item_sku)
    categories = [categories].flatten.compact.map(&:to_sym)

    # Retrieve for all this item's product line
    product_line_urls = get_item_product_line_urls(@item, product_line_urls:, all_product_lines:)
    product_category_ids = @item.product_category_id
    if include_product_lines_publications
      publications += get_publications_for_product_line_urls(
        product_line_urls:,
        product_category_ids:,
        categories:,
        locale:,
        tags:,
        publication_category_paths:
      )
    end
    # Retrieve item specific publication
    related_publications = publication_query(@item.specific_publications, store_id:, tags:, publication_category_paths:)
    if categories.include?(:all) || categories.blank?
      publications += related_publications
    else
      publications += related_publications.publications_for_support_portal if categories.include?(:support)
      publications += related_publications.publications_for_sales_portal if categories.include?(:sales)
    end

    # Is this a kit? fun's not over yet
    if @item.is_kit?
      @item.kit_components.where(ItemRelation[:include_in_spec].eq(true)).find_each do |kit_item|
        pr = Item::PublicationRetriever.new
        res = pr.process(kit_item, categories:, locale:,
                                   all_product_lines:,
                                   product_line_urls:,
                                   tags:,
                                   publication_category_paths:)
        publications += res.all_publications
      end
    end

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

#publication_query(query, store_id: nil, tags: nil, publication_category_paths: nil) ⇒ ActiveRecord::Relation<Publication>

Retrieves a collection of active publications for the given store, optionally filtered by tags.

Parameters:

  • query (ActiveRecord::Relation<Publication>)

    the base query to filter

  • store_id (Integer) (defaults to: nil)

    the ID of the store to filter by

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

    an optional array of tags to filter the publications by

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

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

Returns:

  • (ActiveRecord::Relation<Publication>)

    the filtered collection of publications



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/item/publication_retriever.rb', line 84

def publication_query(query, store_id: nil, tags: nil, publication_category_paths: nil)
  res = query.publications.with_publication_attached.active.includes(
    :product_category,
    :taggings,
    { taggings: :tag }
  )
  res = res.in_store(store_id) if store_id.present?
  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
  res
end