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 Attribute Summary

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 = {}) ⇒ 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)

Parameters:

  • options (Hash) (defaults to: {})

    Options hash (forwarded to BaseService)

Options Hash (options):

  • :tags (String, Array<String>)

    filter image library retrieval to images having any of these tags

  • :locales (Array<String>)

    filter images by these locales (or no locale)

  • :product_line_images_query_limit (Integer)

    max images to retrieve from the product line library (default 5)

  • :max_images (Integer)

    at max, return only this number of images

  • :ignore_individual_query_limits (Boolean)

    if true, all default limits are ignored (default false)

  • :ignore_product_line_primary_image (Boolean)

    true/false

  • :ignore_ancestry (Boolean)

    skip querying product line ancestors

  • :logger (Logger)

    custom logger (defaults to Rails.logger)



30
31
32
# File 'app/services/product_line/image_retriever.rb', line 30

def initialize(options = {})
  super
end

Instance Method Details

#ignore_ancestry?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/services/product_line/image_retriever.rb', line 58

def ignore_ancestry?
  options[:ignore_ancestry].to_b
end

#ignore_individual_query_limits?Boolean

Returns:

  • (Boolean)


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

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)


45
46
47
48
49
50
51
52
# File 'app/services/product_line/image_retriever.rb', line 45

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



69
70
71
72
73
74
75
76
# File 'app/services/product_line/image_retriever.rb', line 69

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



78
79
80
81
82
# File 'app/services/product_line/image_retriever.rb', line 78

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



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

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