Class: ProductLine::VideoRetriever

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

Overview

Retrieves all the videos 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 = {}) ⇒ VideoRetriever

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

  • +:tags+ - filter all video library videos retrieval by these tags, specify one or many to query on the presence of any of these tags
  • +:categories+ - filter all video by categories
  • +:series+ - filter all video by series
  • +:product_line_all_videos_query_limit+ - how many videos max to retrieve from the item library linked to the product line or its ancestor, default is 5
  • +:max_videos+ - At max, return only this number of videos
  • +:ignore_individual_query_limits+ true/false, default: false, if true then all default limits are ignored
  • +:ignore_product_line_primary_video+ - true/false
    ==== Examples

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



22
23
24
# File 'app/services/product_line/video_retriever.rb', line 22

def initialize(options={})
  super(options)
end

Instance Method Details

#ignore_individual_query_limits?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/services/product_line/video_retriever.rb', line 53

def ignore_individual_query_limits?
  options[:ignore_individual_query_limits].present?
end

#process(product_line_object, product_category_object = nil) ⇒ Object

Retrieves all product specifications available to a given item

==== Parameters

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

==== Returns

A Result object with

  • +all_videos+ - an array of videos combined
  • +videos_grouped+ - hash wrapping all videos as a single group (origin grouping removed)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/product_line/video_retriever.rb', line 37

def process(product_line_object, product_category_object = nil)
  product_line = if product_line_object.is_a?(String)
                   slug = LtreePaths.slug_ltree_from_legacy_hyphen_url(product_line_object)
                   ProductLine.find_by(slug_ltree: slug) || ProductLine.find_by(slug_ltree: product_line_object)
                 else
                   product_line_object
                 end
  all_videos = []
  if product_line
    all_videos += product_line_videos(product_line, product_category_object)
    all_videos = all_videos.compact.uniq
    all_videos = all_videos[0..product_line_videos_query_limit] if product_line_videos_query_limit
  end
  Result.new(all_videos: all_videos, videos_grouped: { nil => all_videos })
end

#product_line_videos(product_line, product_category_object = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/product_line/video_retriever.rb', line 57

def product_line_videos(product_line, product_category_object = nil)
  pl_ids = product_line.self_and_ancestors_ids
  pl_videos = Video.active.by_product_line_id_direct(pl_ids).joins(:product_lines).order("product_lines.level DESC")
  unless options[:ignore_product_category].to_b
    pc_ids = product_category_object.self_and_ancestors_ids if product_category_object.present?
    pc_ids ||= ProductCategory.self_and_ancestors_ids(product_line.product_category_ids) if product_line.product_category_ids.present?
    pl_videos = pl_videos.by_product_category_id_direct_or_optional(pc_ids)
  end
  pl_videos = pl_videos.tagged_with(options[:tags]) if options[:tags].present?
  pl_videos = pl_videos.where(category: options[:categories]) if options[:categories].present?
  pl_videos = pl_videos.where(series: options[:series]) if options[:series].present?
  pl_videos = pl_videos.preload(:poster_image)
  pl_videos = pl_videos.order(:position)
  pl_videos = pl_videos.order(Arel.sql('coalesce(digital_assets.air_date, digital_assets.created_at) desc nulls last'))
  pl_videos.to_a
end

#product_line_videos_query_limitObject



74
75
76
77
# File 'app/services/product_line/video_retriever.rb', line 74

def product_line_videos_query_limit
  return if ignore_individual_query_limits?
  options[:product_line_videos_query_limit] || 10
end