Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. def filter_tracks
  2. # Does this need to be so high when JavaScript limits display to 14?
  3. @limit ||= 50
  4.  
  5. # The query methods are responsible for adding their own offsets to this
  6. # hash map; that way, changing or removing a query method doesn't affect any
  7. # other method
  8. # If you need persistence with your implementation of pagination,
  9. # make a session/cookie instead of an instance variable
  10. @tracks_offset ||= {}
  11. @tracks_offset[:default] ||= 0
  12.  
  13. # If you need persistence with your implementation of pagination,
  14. # make a session/cookie instead of an instance variable
  15. @result_track_ids ||= []
  16.  
  17. @order ||= params[:order] || 'heavy_rotation'
  18.  
  19. tracks = Track.ready.with_artist
  20. tracks = parse_params(params[:q], tracks)
  21. @result_count = tracks.count
  22.  
  23. # Where is this used? Should this be an instance variable?
  24. searched = params[:q] && params[:q][:search].present?
  25.  
  26. # Checks for heavy_rotation filter flag
  27. if heavy_rotation? @order
  28. @tracks = heavy_rotation
  29. else
  30. @tracks = order_by_params
  31. end
  32.  
  33. render partial: "shared/results"
  34. end
  35.  
  36. def order_by_params
  37. offset = @tracks_offset[:default]
  38. order = "tracks.#{@order}"
  39.  
  40. # Are these fall-through conditionals or are both meant to be applied?
  41. order.match(/artist.*/) { |m| @order = @order.sub /tracks\./, '' }
  42. order.match(/title.*/) { |m| @order = @order.sub /tracks.(title)(.*)/i, 'LOWER(\1)\2' }
  43.  
  44. unfiltered_results = tracks.offset(offset).order(@order, 'tracks.updated_at DESC').limit(@limit)
  45. top_results = validate_and_return_top_results unfiltered_results, @limit
  46. @tracks_offset[:default] += top_tracks[:offset]
  47.  
  48. @tracks = top_tracks[:top_results]
  49. end
  50.  
  51. def heavy_rotation
  52. week_ago = Time.now - 7.days
  53. two_weeks_ago = Time.now - 14.days
  54. three_months_ago = Time.now - 3.months
  55.  
  56. # This will prevent the method from throwing an error
  57. # if you don't pass an argument to the query methods
  58. # However, if you have strict criteria for the queries and WANT to throw
  59. # an error then set @default = {} (easiest) or delete references to @default in
  60. # all the query methods
  61. @default = {
  62. date_range: two_weeks_ago,
  63. max_results: 5 }
  64.  
  65. tracks_top_licensed(date_range: three_months_ago, max_results: 5) +
  66. tracks_top_listens(date_range: two_weeks_ago, max_results: 3) +
  67. tracks_top_downloaded(date_range: two_weeks_ago, max_results: 2) +
  68. tracks_staff_picks(date_range: three_months_ago, max_results: 4)
  69.  
  70. end
  71.  
  72. # check for heavy rotation filter flag
  73. def heavy_rotation?(order_type)
  74. order_type == "heavy_rotation"
  75. end
  76.  
  77. def validate_and_return_top_results(collection, max = 1)
  78. top_results = []
  79. i = 0 # offset incrementer
  80.  
  81. until top_results.count >= max do
  82. # Checks if track has already appeared in the results
  83. unless @result_track_ids.include? collection[i].id
  84.  
  85. # this will be returned in the method caller
  86. top_results << collection[i]
  87.  
  88. # this is the point of reference to validate all your query method results
  89. @result_track_ids << collection[i].id
  90. end
  91. i += 1
  92. end
  93.  
  94. { top_results: top_results, offset: i }
  95. end
  96.  
  97. # mix in top licensed tracks within last 3 months
  98. def tracks_top_licensed(args = {})
  99. args = @default.merge args
  100. max = args[:max_results]
  101. date_range = args[:date_range]
  102.  
  103. # Adds own offset to #filter_tracks hash map => @tracks_offset
  104. @tracks_offset[:top_licensed] ||= 0
  105.  
  106. unfiltered_results = Track.top_licensed
  107. .where("tracks.updated_at >= :date_range", date_range: date_range)
  108. .limit(@limit)
  109. .offset(@tracks_offset[:top_licensed])
  110.  
  111. top_tracks = validate_and_return_top_results(unfiltered_results, max)
  112.  
  113. # Add offset of your most recent query to the cumulative offset
  114. # so triggering 'view more'/pagination returns contiguous results
  115. @tracks_offset[:top_licensed] += top_tracks[:offset]
  116.  
  117. top_tracks[:top_results]
  118. end
  119.  
  120. # mix top listened to tracks within last two weeks
  121. def tracks_top_listens(args = {})
  122. args = @default.merge args
  123. max = args[:max_results]
  124. date_range = args[:date_range]
  125.  
  126. # Adds own offset to #filter_tracks hash map => @tracks_offset
  127. @tracks_offset[:top_listens] ||= 0
  128.  
  129. unfiltered_results = Track.order('tracks.listens_count DESC')
  130. .where("tracks.updated_at >= :date_range", date_range: date_range)
  131. .limit(@limit)
  132. .offset(@tracks_offset[:top_listens])
  133.  
  134. top_tracks = validate_and_return_top_results(unfiltered_results, max)
  135. @tracks_offset[:top_listens] += top_tracks[:offset]
  136.  
  137. top_tracks[:top_results]
  138. end
  139.  
  140. # mix top downloaded tracks within last two weeks
  141. def tracks_top_downloaded(args = {})
  142. args = @default.merge args
  143. max = args[:max_results]
  144. date_range = args[:date_range]
  145.  
  146. # Adds own offset to #filter_tracks hash map => @tracks_offset
  147. @tracks_offset[:top_downloaded] ||= 0
  148.  
  149. unfiltered_results = Track.order('tracks.listens_count DESC')
  150. .where("tracks.updated_at >= :date_range", date_range: date_range)
  151. .limit(@limit)
  152. .offset(@tracks_offset[:top_downloaded])
  153.  
  154. top_tracks = validate_and_return_top_results(unfiltered_results, max)
  155. @tracks_offset[:top_downloaded] += top_tracks[:offset]
  156.  
  157. top_tracks[:top_results]
  158. end
  159.  
  160. # mix in 25% of staff picks added within 3 months
  161. def tracks_staff_picks(args = {})
  162. args = @default.merge args
  163. max = args[:max_results]
  164. date_range = args[:date_range]
  165.  
  166. # Adds own offset to #filter_tracks hash map => @tracks_offset
  167. @tracks_offset[:staff_picks] ||= 0
  168.  
  169. unfiltered_results = Track.ready
  170. .staff_picks
  171. .includes(:artist)
  172. .order("tracks.created_at DESC")
  173. .where("tracks.updated_at >= :date_range", date_range: date_range)
  174.  
  175. top_tracks = validate_and_return_top_results(unfiltered_results, max)
  176. @tracks_offset[:staff_picks] += top_tracks[:offset]
  177.  
  178. top_tracks[:top_results]
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement