Create repeated / recurring events with Ice Cube Gem

Tram Ho

ice_cube is a Ruby library used to handle recurring or recurring events. It helps us handle those events easily and very quickly. The most basic example is used to create events on the calendar, for example.

Setting

Using

For example, you want to display events that occur:

Every friday the 13th that falls in October

So if I wrote it myself, it would be complicated. With ice_cube, I will handle this problem very easily as follows.

From that object, I will call the available methods of ice_cube like get all events, check schedule that happened on any day, …. There are many methods I can use. Here are some methods and how to use it.

Initialize the schedule object

start_time and end_time are optional

Create rules for the schedule

There are many types of rules:

Daily

Weekly

Similar to the above rules:

  • monthly
  • yearly
  • hourly
  • minutely
  • secondly

For details about the rules, please see its documentation.

Call the required methods

After you’ve created the schedule, added the rule for that schedule, next I can call the methods I need.

As the above example you will know how to call the method, there are many more methods, you can go to its documentation to see details.

Persistence

Actually, the user will create the rules in the UI, not in the code, so how to save those rules to the db?

I will implement it in the following way:

  • Create UI select events:

Here, if you want to code the form yourself, it’s fine. I will get the necessary params from that form to use for creating a schedule schedule.

But to support easy form creation, there’s also the gem https://github.com/GetJobber/recurring_select. It will generate a form to assist you in choosing rules for you already.

  • Create rules:

Once you have the form, next you need to get the params from that form and create the rule for the event. Finally, save the created rule to the db.

I can save rules as yaml, hash, ical.

From that data I just saved it and the db is done.

Conclusion

At this point, you understand how to install and use this gem.

For details see the following documents:

https://github.com/seejohnrun/ice_cube

https://github.com/GetJobber/recurring_select

Share the news now

Source : Viblo