Simple Calendar is a library designed to help developers build calendars quickly and easily. This library can render calendar very flexibly as render calendar with day view, week view, month view, etc. Moreover, it can also be used to render events on that calendar.
Setting
Add to Gemfile:
1 2 | gem <span class="token string">"simple_calendar"</span> <span class="token punctuation">,</span> <span class="token string">"~> 2.0"</span> |
=> bundle install
Add the file app/assets/stylesheets/application.css
1 2 | <span class="token operator">*=</span> require simple_calendar |
Using
Month Calendar
You can generate the calendar display by month using the following method:
1 2 3 4 | <span class="token operator"><</span> <span class="token string">%= month_calendar do |date| %> <%=</span> date <span class="token string">%> <% end %></span> |
The default start_date params will be the current date.
Week Calendar
You can generate the calendar display by the week using the following method:
1 2 3 4 | <span class="token operator"><</span> <span class="token string">%= week_calendar number_of_weeks: 2 do |date| %> <%=</span> date <span class="token string">%> <% end %></span> |
Where number_of_weeks is the number of weeks to display, the default will be 1.
Calendar with any number of days
1 2 3 4 | <span class="token operator"><</span> <span class="token string">%= calendar number_of_days: 4 do |date| %> <%=</span> date <span class="token string">%> <% end %></span> |
Where number_of_days is the number of days to display, the default is 4.
Custom name params
The default params start_date will be used and displayed on the URL. To customize this params to another name, you may pass start_date_param
in the calendar function as follows:
1 2 3 4 | <span class="token operator"><</span> <span class="token string">%= calendar start_date_param: :my_date do |date| %> <%=</span> date <span class="token string">%> <% end %></span> |
Rendering Events
The above methods you can use it to render calendars by month, week, day, … Here we will use it to render events on it.
- Step 1:
Create model with 2 attributes needed: start_time
and end_time
. If that model only has the start_time attribute, it means it’s an event that only happens during the day. If there is both end_time, the event occurs within days.
1 2 3 4 5 6 | <span class="token comment"># single day events</span> $ rails g model <span class="token constant">Meeting</span> name start_time <span class="token symbol">:datetime</span> <span class="token comment"># multi-day events</span> $ rails g model <span class="token constant">Meeting</span> name start_time <span class="token symbol">:datetime</span> end_time <span class="token symbol">:datetime</span> |
I will create data for this event as follows:
1 2 3 4 5 6 | <span class="token constant">Meeting</span> <span class="token punctuation">.</span> create start_time <span class="token punctuation">:</span> <span class="token builtin">Time</span> <span class="token punctuation">.</span> current <span class="token punctuation">,</span> name <span class="token punctuation">:</span> <span class="token string">"Current"</span> <span class="token constant">Meeting</span> <span class="token punctuation">.</span> create start_time <span class="token punctuation">:</span> <span class="token builtin">Time</span> <span class="token punctuation">.</span> current <span class="token operator">+</span> <span class="token number">1.</span> day <span class="token punctuation">,</span> name <span class="token punctuation">:</span> <span class="token string">"Tomorrow"</span> <span class="token constant">Meeting</span> <span class="token punctuation">.</span> create start_time <span class="token punctuation">:</span> <span class="token builtin">Time</span> <span class="token punctuation">.</span> current <span class="token punctuation">,</span> end_time <span class="token punctuation">:</span> <span class="token builtin">Time</span> <span class="token punctuation">.</span> current <span class="token operator">+</span> <span class="token number">1.</span> days <span class="token punctuation">,</span> name <span class="token punctuation">:</span> <span class="token string">"Multi 2 days"</span> |
- Step 2
From the above model, I can query to get that event to display on the calendar as follows:
1 2 3 4 | <span class="token keyword">def</span> index <span class="token variable">@meetings</span> <span class="token operator">=</span> <span class="token constant">Meeting</span> <span class="token punctuation">.</span> all <span class="token keyword">end</span> |
Next in the view, you just need to pass the events
option. It will automatically render the date that the event occurs for each day on the calendar.
1 2 3 4 5 6 7 8 9 10 | <span class="token operator"><</span> <span class="token string">%= month_calendar events: @meetings do |date, meetings| %> <%=</span> date <span class="token string">%> <% meetings.each do |meeting| %></span> <span class="token operator"><</span> div <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token operator">=</span> meeting <span class="token punctuation">.</span> name <span class="token string">%> </div></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token keyword">end</span> <span class="token operator">%</span> <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token keyword">end</span> <span class="token operator">%</span> |
The result will look like this:
At this point, I have built a simple calendar according to my needs. Moreover, I can also customize that calendar’s View, please see its documentation Customizing The Calendar