Skip to content

Instantly share code, notes, and snippets.

@siassaj
Created September 13, 2012 11:53
Show Gist options
  • Save siassaj/658c7d2a6095c9d24558 to your computer and use it in GitHub Desktop.
Save siassaj/658c7d2a6095c9d24558 to your computer and use it in GitHub Desktop.
Disaster
def self.by_cweek(year=0, cweek=0)
## NEED TO TEST THIS!
## This may break since a cweek can spill over into the next year :/
## Return array of orders update_at cweek of year, ascending updated_at
## order.
## If year = 0 or cweek = 0, select for each year/cweek
## Honestly might be better to just put cweek and year attributes into the
## table
cweek = cweek.to_i
year = year.to_i
if cweek != 0 && year != 0
#create a regular date range if year and cweek != 0
if cweek == 52
year += 1
cweek_next = 1
else
cweek_next = cweek + 1
end
day1 = Date.commercial(year, cweek, 1)
day2 = Date.commercial(year, cweek_next, 1)
utc1 = Time.utc(day1.year, day1.month, day1.day)
utc2 = Time.utc(day2.year, day2.month, day2.day)
self.finalized.order("updated_at asc" ).where(:updated_at => utc1...utc2)
elsif cweek != 0 && year == 0
#more complex, select for a specific week, each year
range = []
self.newest_year.upto(self.oldest_year).each do |y|
if cweek == 52
y += 1
cweek_next = 1
else
cweek_next = cweek + 1
end
day1 = Date.commercial(y, cweek, 1)
day2 = Date.commercial(y, cweek_next, 1)
utc1 = Time.utc(day1.year, day1.month, day1.day)
utc2 = Time.utc(day2.year, day2.month, day2.day)
puts utc1.to_s + " :: " + utc2.to_s
range << (utc1...utc2)
end
orders = self.finalized.order("updated_at asc")
orders = orders.where(:updated_at => range)
elsif cweek == 0 && year != 0
day1 = Date.commercial(year, 1, 1)
day2 = Date.commercial(year+1, 1, 1)
utc1 = Time.utc(day1.year, day1.month, day1.day)
utc2 = Time.utc(day2.year, day2.month, day2.day)
self.finalized.order("updated_at asc" ).where(:updated_at => utc1...utc2)
elsif cweek == 0 && year == 0
self.finalized
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment