DEV Community

zenizh
zenizh

Posted on

A monthly calendar gem for Rails application

Carender is a monthly calendar gem for Ruby on Rails application.

Usage

This gem automatically renders a monthly calendar as HTML table by fetching params[:year] and params[:month] at view context.
Following code renders a simple monthly calendar:

<%= carender do |date| %>
  <%= date.day %>
<% end %>

The content passed as a block is rendered in each cell.
For example, above code renders like following HTML table.

<table>
  <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
  <tr><td></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
  <tr><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td></tr>
  ...
</table>

Collection

To render a calendar with a collection which is grouped by the specified column, you can pass :collection and :column arguments like as follows:

<%= carender collection: @posts, column: :posted_on do |date, posts| %>
  <% posts.each do |post| %>
    <%= link_to post.title, post %>
  <% end %>
<% end %>

Installation

Add this line to your application's Gemfile:

gem 'carender'

And then execute:

$ bundle

Top comments (0)