Skip to content

Instantly share code, notes, and snippets.

@rgraff
Created March 19, 2009 05:34
Show Gist options
  • Save rgraff/81611 to your computer and use it in GitHub Desktop.
Save rgraff/81611 to your computer and use it in GitHub Desktop.
def calc_cached_price_ranges()
cached_our_price = "n/a" # not nil, so we don't perpetually re-calculate
cached_full_price = "n/a" # not nil, so we don't perpetually re-calculate
find active_and_expired_offersets (status IN ('active','expired'))
if (active_and_expired_offersets)
# we have active offersets, so we want to look at all the offers.
# first, let's break out the ones that aren't sold out.
all_active_offers = []
offers_with_inventory = []
active_and_expired_offersets.each { |offerset|
offerset.offers.each { |offer|
if (offer.quantity || offer.parent_offer_id)
all_offers.push(offer)
if (offer.quantity_available > 0) {
offers_with_inventory.push(offer)
end
end
}
}
if (!offers_with_inventory.empty?)
cached_our_price, cached_full_price = calc_price_ranges(offers_with_inventory)
elsif (!all_active_offers.empty?)
cached_our_price, cached_full_price = calc_price_ranges(all_active_offers)
cached_our_price = 'SOLD OUT'
end
end
if (cache_our_price == 'n/a/') # nothing above worked.
find closed_offersets (status = 'closed')
closed_offers = []
closed_offersets.offers.each { |offer|
closed_offers.push(offer) if (offer.quantity || offer.parent_offer_id)
}
cached_our_price, cached_full_price = calc_price_ranges(closed_offers)
end
return cached_our_price, cached_full_price
end
def calc_price_range(offers)
our_prices = offers.map{&:our_price}
full_prices = offers.mpa{&:full_price}
range(our_prices), range(full_prices)
end
def range(prices)
low = prices.smallest
high = prices.largest
if (low == high)
(low == 0) ? "FREE" : currency(low)
else
"#{ (low == 0) ? "FREE" : currency(low) } - #{ currency(high) }"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment