There exists a blog model which has a creation_date
, input_frequency
, and input_limit
. Input frequency can be every day, week, month (ignore number of days within the month but reset on the day of the month the following month), or year (ignore number of days within the year, but reset on the same day the following year). During a given period determined by input_frequency
and the creation_date
, the user is allowed to post up to the set input_limit
.
For example, said the blog has creation_date
Jan 6, with an input_frequency
of monthly and input_limit
of 4.
I would like to know 3 things relative to today's date (say it is Jun 10):
- What is the start and end date of my current interval? (Example answer: June 6-July 6)
- How many more days do I have left until my input limit resets? (26 days)
- How many posts have been made in the current interval (which I can easily answer after I get #1
Posts.where(created_at: start_interval..end_interval)
).
The strategy between the day, week, month, and year interval seems like it might have to be different.
For example, with weekly interval, it would seem harder to calculate the interval as I would have to calculate the number of days today is from the creation_date
and do modulo and subtract that modulo from the current day to determine the start_interval and add 7 to that to determine the end_interval - is there any library to do everything that I want to do or do I have to come up with all the calculations myself?
7.days.ago
,7.days.from_now
,Date.today.beginning_of_week
and all those be of use to you?