Hey guys, I''m working on an events calendar application (I''ve done this in PHP so I know what I WANT to do but I''m stuck on some details in the HOW department) and I''m perplexed on two things: 1. How do you manipulate dates in Ruby.. formatting as well as grabbing the first and last day of the month.. even possibly just the day of the week? 2. In my PHP calendar I grabbed all of the records within the timeframe of the calendar sorted by date and then checked the current records date with the day of the month to determine whether or not to display it for that day and move on to the next event. How would I do this in Rails? Thanks so much those are some big questions so if anyone has time to take a stab at any of it you''ll be helping me out a lot. - Jim
Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> writes:> I''m working on an events calendar application (I''ve done this in PHP > so I know what I WANT to do but I''m stuck on some details in the HOW > department) and I''m perplexed on two things:There are several people doing something like this.> 1. How do you manipulate dates in Ruby.. formatting as well as > grabbing the first and last day of the month.. even possibly just the > day of the week?You''ll want to look at the Date class that''s part of the standard library. It provides stuff like Date#wday to tell you which day of week it is. For the first day of the month, that''s easy: Date.new(year, month, 1) The last day of the month isn''t much harder, but uglier: next_month = month next_year = year if month == 12 next_month == 1 next_year == year +1 end last_day_of_month = Date.new(next_year, next_month, 1) - 1 You can subtract integers from Dates to get that many days prior.> 2. In my PHP calendar I grabbed all of the records within the > timeframe of the calendar sorted by date and then checked the current > records date with the day of the month to determine whether or not to > display it for that day and move on to the next event. How would I > do this in Rails?I''ve recently written about this on my blog. http://blog.lathi.net search for calendar to find the links that address it. Basically, what I did was implement occurs_on?(date) for the event. I have repeating events, so a simple date wouldn''t work. I then load all the events from the database. I also build a 5x7 grid of arrays for the Dates that will appear on the calendar. Then I loop over the grid asking each event if it occurs_on? that day. I''m sure there''s a better way to do it. As it is, I can end up with a lot of calls to occurs_on?(date). With five events and 35 days, that''s 175 calls. Since my events are repeating, it''s hard for me to narrow my list of events to the ones that occur within a date range without testing all the dates in that range. -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Patrick McCafferty
2005-Sep-18 17:12 UTC
Re: Most effective way to build an events calendar?
Why not feed occurs_on? (or a method like it) an array of every date that you need to check and have it return an array of each of those dates that it actually occurs on? It would reduce the number of calls at the least and could be further optimized after - for instance, if your recurring events occur every 8 days, and you can assume that the dates that you are checking are all sequential, once you find the first one, you could just calculate the remaining days from that, rather than checking for each date. I don''t know how you''ve implemented repeating events, but if you organize arrays correctly I would think that this method would be at least a little faster. The 175 calls might not even be a problem though. Make sure you test before optimizing, of course. On 9/19/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> > Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> writes: > > > I''m working on an events calendar application (I''ve done this in PHP > > so I know what I WANT to do but I''m stuck on some details in the HOW > > department) and I''m perplexed on two things: > > There are several people doing something like this. > > > 1. How do you manipulate dates in Ruby.. formatting as well as > > grabbing the first and last day of the month.. even possibly just the > > day of the week? > > You''ll want to look at the Date class that''s part of the standard > library. It provides stuff like Date#wday to tell you which day of > week it is. For the first day of the month, that''s easy: > > Date.new(year, month, 1) > > The last day of the month isn''t much harder, but uglier: > > next_month = month > next_year = year > if month == 12 > next_month == 1 > next_year == year +1 > end > last_day_of_month = Date.new(next_year, next_month, 1) - 1 > > You can subtract integers from Dates to get that many days prior. > > > 2. In my PHP calendar I grabbed all of the records within the > > timeframe of the calendar sorted by date and then checked the current > > records date with the day of the month to determine whether or not to > > display it for that day and move on to the next event. How would I > > do this in Rails? > > I''ve recently written about this on my blog. > > http://blog.lathi.net > > search for calendar to find the links that address it. > > Basically, what I did was implement occurs_on?(date) for the event. I > have repeating events, so a simple date wouldn''t work. I then load > all the events from the database. I also build a 5x7 grid of arrays > for the Dates that will appear on the calendar. Then I loop over the > grid asking each event if it occurs_on? that day. > > I''m sure there''s a better way to do it. As it is, I can end up with a > lot of calls to occurs_on?(date). With five events and 35 days, > that''s 175 calls. Since my events are repeating, it''s hard for me to > narrow my list of events to the ones that occur within a date range > without testing all the dates in that range. > -- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks Doug, This was really helpful! I was curious is there any difference between using ''Time'' or ''Date'' or are they synonymous? - Jim On Sep 18, 2005, at 10:02 AM, Doug Alcorn wrote:> Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> writes: > > >> I''m working on an events calendar application (I''ve done this in PHP >> so I know what I WANT to do but I''m stuck on some details in the HOW >> department) and I''m perplexed on two things: >> > > There are several people doing something like this. > > >> 1. How do you manipulate dates in Ruby.. formatting as well as >> grabbing the first and last day of the month.. even possibly just the >> day of the week? >> > > You''ll want to look at the Date class that''s part of the standard > library. It provides stuff like Date#wday to tell you which day of > week it is. For the first day of the month, that''s easy: > > Date.new(year, month, 1) > > The last day of the month isn''t much harder, but uglier: > > next_month = month > next_year = year > if month == 12 > next_month == 1 > next_year == year +1 > end > last_day_of_month = Date.new(next_year, next_month, 1) - 1 > > You can subtract integers from Dates to get that many days prior. > > >> 2. In my PHP calendar I grabbed all of the records within the >> timeframe of the calendar sorted by date and then checked the current >> records date with the day of the month to determine whether or not to >> display it for that day and move on to the next event. How would I >> do this in Rails? >> > > I''ve recently written about this on my blog. > > http://blog.lathi.net > > search for calendar to find the links that address it. > > Basically, what I did was implement occurs_on?(date) for the event. I > have repeating events, so a simple date wouldn''t work. I then load > all the events from the database. I also build a 5x7 grid of arrays > for the Dates that will appear on the calendar. Then I loop over the > grid asking each event if it occurs_on? that day. > > I''m sure there''s a better way to do it. As it is, I can end up with a > lot of calls to occurs_on?(date). With five events and 35 days, > that''s 175 calls. Since my events are repeating, it''s hard for me to > narrow my list of events to the ones that occur within a date range > without testing all the dates in that range. > -- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
François Beausoleil
2005-Sep-20 03:38 UTC
Re: Most effective way to build an events calendar?
Hello Jim, Jim Jeffers said the following on 2005-09-19 23:23:> This was really helpful! I was curious is there any difference between > using ''Time'' or ''Date'' or are they synonymous?Time has the time component, while Date only has the date part. This ties in with DATE, TIME, and DATETIME columns. Rails maps DATE columns to Date, and TIME, DATETIME and TIMESTAMP columns to Time. See also the Ruby documentation. Hope that helps ! François
Jim Jeffers
2005-Sep-20 06:13 UTC
Re: Most effective way to build an events calendar? (recurring events)
Doug, Couldn''t you just have your events model as follows: Events -------------- id, start_date, end_date, frequency (monthly, weekly, daily) Then you could just run a query for all events that have a start and end date within your conditions and build an array based on the frequency and end date? This would allow you to simply cycle through them without having to make many calls to occurs_on? The question is is it faster to construct the array or is it faster to take a risk of calling the method many times? - Jim On Sep 18, 2005, at 10:12 AM, Patrick McCafferty wrote:> Why not feed occurs_on? (or a method like it) an array of every > date that you need to check and have it return an array of each of > those dates that it actually occurs on? > > It would reduce the number of calls at the least and could be > further optimized after - for instance, if your recurring events > occur every 8 days, and you can assume that the dates that you are > checking are all sequential, once you find the first one, you could > just calculate the remaining days from that, rather than checking > for each date. > > I don''t know how you''ve implemented repeating events, but if you > organize arrays correctly I would think that this method would be > at least a little faster. > > The 175 calls might not even be a problem though. Make sure you > test before optimizing, of course. > > On 9/19/05, Doug Alcorn < doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote: > Jim Jeffers < rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> writes: > > > I''m working on an events calendar application (I''ve done this in PHP > > so I know what I WANT to do but I''m stuck on some details in the HOW > > department) and I''m perplexed on two things: > > There are several people doing something like this. > > > 1. How do you manipulate dates in Ruby.. formatting as well as > > grabbing the first and last day of the month.. even possibly just > the > > day of the week? > > You''ll want to look at the Date class that''s part of the standard > library. It provides stuff like Date#wday to tell you which day of > week it is. For the first day of the month, that''s easy: > > Date.new (year, month, 1) > > The last day of the month isn''t much harder, but uglier: > > next_month = month > next_year = year > if month == 12 > next_month == 1 > next_year == year +1 > end > last_day_of_month = Date.new(next_year, next_month, 1) - 1 > > You can subtract integers from Dates to get that many days prior. > > > 2. In my PHP calendar I grabbed all of the records within the > > timeframe of the calendar sorted by date and then checked the > current > > records date with the day of the month to determine whether or > not to > > display it for that day and move on to the next event. How would I > > do this in Rails? > > I''ve recently written about this on my blog. > > http://blog.lathi.net > > search for calendar to find the links that address it. > > Basically, what I did was implement occurs_on?(date) for the event. I > have repeating events, so a simple date wouldn''t work. I then load > all the events from the database. I also build a 5x7 grid of arrays > for the Dates that will appear on the calendar. Then I loop over the > grid asking each event if it occurs_on? that day. > > I''m sure there''s a better way to do it. As it is, I can end up with a > lot of calls to occurs_on?(date). With five events and 35 days, > that''s 175 calls. Since my events are repeating, it''s hard for me to > narrow my list of events to the ones that occur within a date range > without testing all the dates in that range. > -- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 9/18/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> Basically, what I did was implement occurs_on?(date) for the event. I > have repeating events, so a simple date wouldn''t work. I then load > all the events from the database. I also build a 5x7 grid of arrays > for the Dates that will appear on the calendar. Then I loop over the > grid asking each event if it occurs_on? that day. > > I''m sure there''s a better way to do it. As it is, I can end up with a > lot of calls to occurs_on?(date). With five events and 35 days, > that''s 175 calls. Since my events are repeating, it''s hard for me to > narrow my list of events to the ones that occur within a date range > without testing all the dates in that range.Could you have your Events create CalendarEvents when they are created/updated/deleted? The CalendarEvent model could have very simple entries (date, time, event_id) while the Event model itself can have the complicated logic to create those entries (every friday, last sunday every month, 1st and 15th, etc). That also lets you add new logic to the Event model as needed without disturbing (or slowing down) any of the consumers of CalendarEvents. You can just select CalendarEvents that are between two dates. - Isaac
That''s pretty slick Isaac I think I''ll give that a shot. - Jim On Sep 20, 2005, at 2:37 AM, Isaac Reuben wrote:> On 9/18/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote: > >> Basically, what I did was implement occurs_on?(date) for the >> event. I >> have repeating events, so a simple date wouldn''t work. I then load >> all the events from the database. I also build a 5x7 grid of arrays >> for the Dates that will appear on the calendar. Then I loop over the >> grid asking each event if it occurs_on? that day. >> >> I''m sure there''s a better way to do it. As it is, I can end up >> with a >> lot of calls to occurs_on?(date). With five events and 35 days, >> that''s 175 calls. Since my events are repeating, it''s hard for me to >> narrow my list of events to the ones that occur within a date range >> without testing all the dates in that range. >> > > Could you have your Events create CalendarEvents when they are > created/updated/deleted? The CalendarEvent model could have very > simple entries (date, time, event_id) while the Event model itself can > have the complicated logic to create those entries (every friday, last > sunday every month, 1st and 15th, etc). That also lets you add new > logic to the Event model as needed without disturbing (or slowing > down) any of the consumers of CalendarEvents. You can just select > CalendarEvents that are between two dates. > > - Isaac > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >