Class: Item::VideoRetriever
- Inherits:
-
BaseService
- Object
- BaseService
- Item::VideoRetriever
- Defined in:
- app/services/item/video_retriever.rb
Overview
Retrieves all the videos associated with a particular item. Queries main video, product line main video,
video library for video tagged directly to the item or to the product line and its ancestor
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #ignore_individual_query_limits? ⇒ Boolean
-
#initialize(options = {}) ⇒ VideoRetriever
constructor
==== Initialization options (pass as a hash).
- #item_videos_query(item) ⇒ Object
- #item_videos_query_limit ⇒ Object
-
#process(item) ⇒ Object
Retrieves all product specifications available to a given item.
- #product_line_query(item) ⇒ Object
- #product_line_query_limit ⇒ Object
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
- +:item_videos_query_limit+ - how many videos max to retrieve from the item library, default to 10
- +:product_line_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
==== Examples
ir = Item::VideoRetriever.new(tags: 'for-product-page', item_videos_query_limit: 20)
23 24 25 |
# File 'app/services/item/video_retriever.rb', line 23 def initialize(={}) super() end |
Instance Method Details
#ignore_individual_query_limits? ⇒ Boolean
47 48 49 |
# File 'app/services/item/video_retriever.rb', line 47 def ignore_individual_query_limits? [:ignore_individual_query_limits].present? end |
#item_videos_query(item) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'app/services/item/video_retriever.rb', line 51 def item_videos_query(item) item_videos = Video.active.by_item_ids(item.id) item_videos = item_videos.tagged_with([:tags]) if [:tags].present? item_videos = item_videos.where(category: [:categories]) if [:categories].present? item_videos = item_videos.where(series: [:series]) if [:series].present? item_videos = item_videos.preload(:poster_image) item_videos = item_videos.order(Arel.sql('coalesce(digital_assets.air_date, digital_assets.created_at) desc nulls last')) item_videos = item_videos.limit(item_videos_query_limit) if item_videos_query_limit item_videos.to_a end |
#item_videos_query_limit ⇒ Object
62 63 64 65 |
# File 'app/services/item/video_retriever.rb', line 62 def item_videos_query_limit return if ignore_individual_query_limits? [:item_videos_query_limit] || 10 end |
#process(item) ⇒ Object
Retrieves all product specifications available to a given item
==== Parameters
- +item+ - The item 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)
38 39 40 41 42 43 44 45 |
# File 'app/services/item/video_retriever.rb', line 38 def process(item) all_videos = [] all_videos += item_videos_query(item) all_videos += product_line_query(item) all_videos = all_videos.compact.uniq all_videos = all_videos[0..([:max_videos] - 1)] if [:max_videos] Result.new(all_videos: all_videos, videos_grouped: { nil => all_videos }) end |
#product_line_query(item) ⇒ Object
67 68 69 70 |
# File 'app/services/item/video_retriever.rb', line 67 def product_line_query(item) return [] unless pl = item.primary_product_line ProductLine::VideoRetriever.new().process(pl, item.product_category).all_videos end |
#product_line_query_limit ⇒ Object
72 73 74 75 |
# File 'app/services/item/video_retriever.rb', line 72 def product_line_query_limit return if ignore_individual_query_limits? [:product_line_query_limit] || 10 end |