Skip to content

Instantly share code, notes, and snippets.

@jakecraige
Created December 11, 2013 02:18
Show Gist options
  • Save jakecraige/7904153 to your computer and use it in GitHub Desktop.
Save jakecraige/7904153 to your computer and use it in GitHub Desktop.
def top_retailers
retailers = self.site_product_prices.last_24_hours.trusted.by_price
retailers = retailers.group_by { |retailer| retailer.site }
.map(&:last)
.each{ |sites_array| sites_array.sort_by! { |site| site.weight }.reverse! }
.map(&:first).reverse
.sort_by { |x| x.price }
if retailers.count > 1
# remove best price from results
retailers.shift
elsif retailers.count == 1
# If the first retailor is the same as the best price,
# don't use it
if retailers.first.site == self.best_site_product_price.site
retailers.shift
end
end
# If the top retailers is blank
# and the origiinal site product price is not the same as the best price
# add the originail site product price
if retailers.blank?
first_site_product_price = self.site_product_prices.first
if first_site_product_price.site != self.best_site_product_price.site
retailers = [first_site_product_price]
end
end
retailers[0..5] # return max of 6 results
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment