Skip to content

Instantly share code, notes, and snippets.

@femmestem
Last active August 29, 2015 14:14
Show Gist options
  • Save femmestem/2b539abe92e9813c02da to your computer and use it in GitHub Desktop.
Save femmestem/2b539abe92e9813c02da to your computer and use it in GitHub Desktop.
Unique Records Persisting Multiple Requests
def filter_tracks
# Does this need to be so high when JavaScript limits display to 14?
@limit ||= 50
# The query methods are responsible for adding their own offsets to this
# hash map; that way, changing or removing a query method doesn't affect any
# other method
# If you need persistence with your implementation of pagination,
# make a session/cookie instead of an instance variable
@tracks_offset ||= {}
@tracks_offset[:default] ||= 0
# If you need persistence with your implementation of pagination,
# make a session/cookie instead of an instance variable
@result_track_ids ||= []
@order ||= params[:order] || 'heavy_rotation'
tracks = Track.ready.with_artist
tracks = parse_params(params[:q], tracks)
@result_count = tracks.count
# Where is this used? Should this be an instance variable?
searched = params[:q] && params[:q][:search].present?
# Checks for heavy_rotation filter flag
if heavy_rotation? @order
@tracks = heavy_rotation
else
@tracks = order_by_params
end
render partial: "shared/results"
end
def order_by_params
offset = @tracks_offset[:default]
order = "tracks.#{@order}"
# Are these fall-through conditionals or are both meant to be applied?
order.match(/artist.*/) { |m| @order = @order.sub /tracks\./, '' }
order.match(/title.*/) { |m| @order = @order.sub /tracks.(title)(.*)/i, 'LOWER(\1)\2' }
unfiltered_results = tracks.offset(offset).order(@order, 'tracks.updated_at DESC').limit(@limit)
top_results = validate_and_return_top_results unfiltered_results, @limit
@tracks_offset[:default] += top_tracks[:offset]
@tracks = top_tracks[:top_results]
end
def heavy_rotation
week_ago = Time.now - 7.days
two_weeks_ago = Time.now - 14.days
three_months_ago = Time.now - 3.months
# This will prevent the method from throwing an error
# if you don't pass an argument to the query methods
# However, if you have strict criteria for the queries and WANT to throw
# an error then set @default = {} (easiest) or delete references to @default in
# all the query methods
@default = {
date_range: two_weeks_ago,
max_results: 5 }
tracks_top_licensed(date_range: three_months_ago, max_results: 5) +
tracks_top_listens(date_range: two_weeks_ago, max_results: 3) +
tracks_top_downloaded(date_range: two_weeks_ago, max_results: 2) +
tracks_staff_picks(date_range: three_months_ago, max_results: 4)
end
# check for heavy rotation filter flag
def heavy_rotation?(order_type)
order_type == "heavy_rotation"
end
def validate_and_return_top_results(collection, max = 1)
top_results = []
i = 0 # offset incrementer
until top_results.count >= max do
# Checks if track has already appeared in the results
unless @result_track_ids.include? collection[i].id
# this will be returned in the method caller
top_results << collection[i]
# this is the point of reference to validate all your query method results
@result_track_ids << collection[i].id
end
i += 1
end
{ top_results: top_results, offset: i }
end
# mix in top licensed tracks within last 3 months
def tracks_top_licensed(args = {})
args = @default.merge args
max = args[:max_results]
date_range = args[:date_range]
# Adds own offset to #filter_tracks hash map => @tracks_offset
@tracks_offset[:top_licensed] ||= 0
unfiltered_results = Track.top_licensed
.where("tracks.updated_at >= :date_range", date_range: date_range)
.limit(@limit)
.offset(@tracks_offset[:top_licensed])
top_tracks = validate_and_return_top_results(unfiltered_results, max)
# Add offset of your most recent query to the cumulative offset
# so triggering 'view more'/pagination returns contiguous results
@tracks_offset[:top_licensed] += top_tracks[:offset]
top_tracks[:top_results]
end
# mix top listened to tracks within last two weeks
def tracks_top_listens(args = {})
args = @default.merge args
max = args[:max_results]
date_range = args[:date_range]
# Adds own offset to #filter_tracks hash map => @tracks_offset
@tracks_offset[:top_listens] ||= 0
unfiltered_results = Track.order('tracks.listens_count DESC')
.where("tracks.updated_at >= :date_range", date_range: date_range)
.limit(@limit)
.offset(@tracks_offset[:top_listens])
top_tracks = validate_and_return_top_results(unfiltered_results, max)
@tracks_offset[:top_listens] += top_tracks[:offset]
top_tracks[:top_results]
end
# mix top downloaded tracks within last two weeks
def tracks_top_downloaded(args = {})
args = @default.merge args
max = args[:max_results]
date_range = args[:date_range]
# Adds own offset to #filter_tracks hash map => @tracks_offset
@tracks_offset[:top_downloaded] ||= 0
unfiltered_results = Track.order('tracks.listens_count DESC')
.where("tracks.updated_at >= :date_range", date_range: date_range)
.limit(@limit)
.offset(@tracks_offset[:top_downloaded])
top_tracks = validate_and_return_top_results(unfiltered_results, max)
@tracks_offset[:top_downloaded] += top_tracks[:offset]
top_tracks[:top_results]
end
# mix in 25% of staff picks added within 3 months
def tracks_staff_picks(args = {})
args = @default.merge args
max = args[:max_results]
date_range = args[:date_range]
# Adds own offset to #filter_tracks hash map => @tracks_offset
@tracks_offset[:staff_picks] ||= 0
unfiltered_results = Track.ready
.staff_picks
.includes(:artist)
.order("tracks.created_at DESC")
.where("tracks.updated_at >= :date_range", date_range: date_range)
top_tracks = validate_and_return_top_results(unfiltered_results, max)
@tracks_offset[:staff_picks] += top_tracks[:offset]
top_tracks[:top_results]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment