Skip to content

Commit

Permalink
Reusable Monthly Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Feb 8, 2024
1 parent 9d47f5d commit 1702b7f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/calendar_controller.rb
@@ -0,0 +1,7 @@
class CalendarController < ApplicationController
def month
@date = Date.parse(params.fetch(:date, Date.today.to_s))
# @events = Event.where(start_date: @date.all_month)
@events = Event.where(start_date: @date.all_month).group_by { |e| e.start_date.to_date }
end
end
11 changes: 11 additions & 0 deletions app/helpers/application_helper.rb
@@ -1,2 +1,13 @@
module ApplicationHelper
def month_offset(date)
date.beginning_of_month.wday - 1
end

def today?(date)
date == Date.today
end

def today_class(date)
"bg-rose-200" if today?(date)
end
end
21 changes: 21 additions & 0 deletions app/views/calendar/_month.html.erb
@@ -0,0 +1,21 @@
<%= link_to "<", calendar_month_path(date: date - 1.month) %>
<%= link_to "Today", calendar_month_path %>
<%= link_to ">", calendar_month_path(date: date + 1.month) %>
<%= date.strftime('%B %Y')%>

<div class="grid grid-cols-7">
<% Date::ABBR_DAYNAMES.rotate.each do |weekday| %>
<div>
<%= weekday %>
</div>
<% end %>
<% month_offset(date).times do %>
<div></div>
<% end %>
<% date.all_month.each do |day| %>
<div class="border min-h-24 <%= today_class(day) %>">
<%= yield day %>
</div>
<% end %>
</div>
9 changes: 9 additions & 0 deletions app/views/calendar/month.html.erb
@@ -0,0 +1,9 @@
<%= render 'calendar/month', date: @date do |day| %>
<%= day.strftime('%d') %>
<% @events[day]&.each do |event| %>
<div class="rounded-md bg-rose-400 m-1">
<%= event.name %>
<%= event.location %>
</div>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -8,4 +8,5 @@

# Defines the root path route ("/")
root "events#index"
get "calendar/month", to: 'calendar#month'
end

0 comments on commit 1702b7f

Please sign in to comment.