I’ve been busy working on my latest application, a to-do list app called All Done. One of the features is to have a 7 day countdown for every task and have it displayed. To do this I added the following line to my code:
This uses the distance_of_time_in_words helper method to display the number of days since a to-do item was created. The first argument passed through the method is the starting point from which time is calculated. I set this to when the task was created plus seven hours (item.created_at + 7.days
). The second argument is the end time which is set to the current time (Time.now
). More info about the helper method can be found here.
This is how the app looked after adding this feature:
Looks great, but how to test if the countdown is actually working without waiting around for a whole day to see if the countdown went down by a day? Well one way is to use the Rails console and the update_attribute method to update the created_at attribute on to-do items. I wasn’t sure at first how to do this in the console, but I managed to figure it out using this helpful resource.
Here’s how I updated my test task of “Eat breakfast” where I changed created_at attribute to an earlier date.
Now when I refreshed my page I see that the task has been updated to have only 5 days left!