Skip to content

Instantly share code, notes, and snippets.

@johnhamelink
Created May 22, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhamelink/6cd23e17cbeac1ddd489fd4cc4594bd4 to your computer and use it in GitHub Desktop.
Save johnhamelink/6cd23e17cbeac1ddd489fd4cc4594bd4 to your computer and use it in GitHub Desktop.
namespace :db do
task ice_cube_test: :environment do
CSV.open("out.csv", "wb") do |csv|
Event.all.each do |event|
repeatable = event.repeatables.first
if repeatable.nil?
puts "Bad Event: #{event.id}. No Repeatable."
csv << [event.id, nil, nil, nil]
next
end
next_occurrence = repeatable.schedule_formatted.next_occurrence
if next_occurrence
next_occurrence = next_occurrence.utc
# Build the new start and end times for this occurrence
starts_on = DateTime.parse("#{next_occurrence.to_date} #{repeatable.time_start}").utc
# If a time_end isn't set, then set to the next day
ends_on = if repeatable.time_end.nil?
(DateTime.parse(next_occurrence.to_date.to_s) + 1.day).utc
else
DateTime.parse("#{next_occurrence.to_date} #{repeatable.time_end}").utc
end
csv << [event.id, repeatable.id, next_occurrence, starts_on, ends_on]
else
csv << [event.id, repeatable.id, nil, nil, nil]
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment