Last active
August 29, 2015 14:04
-
-
Save zimkies/08ea1a409091941b25f9 to your computer and use it in GitHub Desktop.
Code for creating pool objects for the future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PoolsCreator | |
def create_all | |
Week.where("starting_on > ?", 1.week.ago).each do |week| | |
create_for_week(week) | |
end | |
end | |
def create_for_week(week) | |
City.active.each do |city| | |
datetimes_for_city(week, city).each do |datetime| | |
next if pool_exists?(city, datetime) | |
p "creating pool #{city.name} #{week.id} #{datetime}" | |
Pool.create!( | |
datetime: datetime, | |
city_id: city.id, | |
week_id: week.id, | |
) | |
end | |
end | |
end | |
def pool_exists?(city, datetime) | |
Pool.where(city_id: city.id) | |
.where("datetime > ?", datetime - 12.hours) | |
.where("datetime < ?", datetime + 12.hours) | |
.exists? | |
end | |
def datetimes_for_city(week, city) | |
(0..4).map do |day| | |
date = week.starting_on + day.days | |
dt = date.strftime("%Y-%m-%d 20:00:00") | |
ActiveSupport::TimeZone[city.time_zone].parse(dt) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment