Class: ProductLine::ImageRetriever

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

Overview

Retrieves all the images associated with a particular product line

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

#initialize(options = {}) ⇒ ImageRetriever

==== Initialization options (pass as a hash)

  • +:tags+ - filter all image library images retrieval by these tags, specify one or many to query on the presence of any of these tags
  • +:locales+ - filter all images by those having any of these locales specifically or no locales at all
  • +:product_line_all_images_query_limit+ - how many images max to retrieve from the item library linked to the product line or its ancestor, default is 5
  • +:max_images+ - At max, return only this number of images
  • +:ignore_individual_query_limits+ true/false, default: false, if true then all default limits are ignored
  • +:ignore_product_line_primary_image+ - true/false
    ==== Examples

ir = ProductLine::ImageRetriever.new(tags: 'for-product-page', max_images: 20)



20
21
22
# File 'app/services/product_line/image_retriever.rb', line 20

def initialize(options = {})
  super
end

Instance Method Details

#ignore_ancestry?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/services/product_line/image_retriever.rb', line 48

def ignore_ancestry?
  options[:ignore_ancestry].to_b
end

#ignore_individual_query_limits?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/services/product_line/image_retriever.rb', line 44

def ignore_individual_query_limits?
  options[:ignore_individual_query_limits].to_b
end

#process(product_line) ⇒ Object

Retrieves all product specifications available to a given item

==== Parameters

  • +product_line+ - The Product Line for which you want to retrieve images

==== Returns

A Result object with

  • +all_images+ - an array of images combined
  • +images_grouped+ - hash wrapping all images as a single group (origin grouping removed)


35
36
37
38
39
40
41
42
# File 'app/services/product_line/image_retriever.rb', line 35

def process(product_line)
  all_images = []
  all_images << product_line_primary_image(product_line)
  all_images += product_line_images(product_line)
  all_images = all_images.compact.uniq
  all_images = all_images[0..(options[:max_images] - 1)] if options[:max_images]
  Result.new(all_images: all_images, images_grouped: { nil => all_images })
end

#product_line_images(product_line) ⇒ Object



59
60
61
62
63
64
65
66
# File 'app/services/product_line/image_retriever.rb', line 59

def product_line_images(product_line)
  pl_ids = ignore_ancestry? ? [product_line.id] : product_line.self_and_ancestors_ids
  pl_images = Image.active.by_product_line_id_direct(pl_ids).joins(:product_lines).order(:position)
  pl_images = pl_images.limit(product_line_images_query_limit) if product_line_images_query_limit
  pl_images = pl_images.tagged_with(options[:tags]) if options[:tags].present?
  pl_images = pl_images.localized_for_or_not(options[:locales]) if options[:locales].present?
  pl_images.to_a.compact.uniq
end

#product_line_images_query_limitObject



68
69
70
71
72
# File 'app/services/product_line/image_retriever.rb', line 68

def product_line_images_query_limit
  return if ignore_individual_query_limits?

  options[:product_line_images_query_limit] || 5
end

#product_line_primary_image(product_line) ⇒ Object



52
53
54
55
56
57
# File 'app/services/product_line/image_retriever.rb', line 52

def product_line_primary_image(product_line)
  return if options[:ignore_product_line_primary_image].present?
  return unless plmi = product_line.try(:primary_image_inherited)

  plmi
end