Skip to content

Instantly share code, notes, and snippets.

@advorak
Forked from anonymous/home_controller.rb
Created May 21, 2012 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advorak/2760681 to your computer and use it in GitHub Desktop.
Save advorak/2760681 to your computer and use it in GitHub Desktop.
Calendar for the current month
// Place all the styles related to the home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
#monthName {
text-align: center;
position: absolute;
top: 0;
display: inline;
a {
text-decoration: none;
color: blue;
&:hover {
color: red;
text-decoration: underline;
}
}
}
table.calendar {
margin-top: 5em;
th {
background-color: lightgrey;
}
td {
font-family: Georgia, serif;
text-align: right;
vertical-align: top;
width: 100px;
height: 100px;
&span.day {
position: relative;
top: 0.5em;
right: 0.5em;
margin-top: 2em;
font-size: 1.5em;
}
&.weekend {
background-color: lightgrey;
}
&.today {
background-color: yellow;
}
}
}
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
$ ->
tableWidth = $('table').width()
$('h1#monthName').css('width',tableWidth)
class HomeController < ApplicationController
def index
current_year = (params[:year ] || Date.today.year )
current_month = (params[:month] || Date.today.month)
@current_date = "#{current_year}-#{current_month}-#{Date.today.day}".to_date
@days_in_month = []
@days_in_month[@current_date.beginning_of_month.wday] = (@current_date.beginning_of_month..@current_date.end_of_month).to_a
@days_in_month = @days_in_month.flatten.in_groups_of(7, false)
@beginning_of_month_blanks_count = @current_date.beginning_of_month.wday
@days_in_month[0].compact!
@end_of_month_blanks_count = (6 - @current_date.end_of_month.wday)
puts @days_in_month.inspect
end
end
module HomeHelper
def multiclass(*args)
if args[0].is_a?(Hash)
args[0] = args[0].to_a.collect do |key,value|
key.to_s if value
end.compact
end
args.join(" ")
end
def root_path_month_year(math)
date = @current_date + math
root_path(month: date.month, year: date.year)
end
end
%h1#monthName
= link_to raw("&laquo;"), root_path_month_year(-1.month)
= @current_date.strftime("%B")
= link_to raw("&raquo;"), root_path_month_year(1.month)
= link_to raw("&laquo;"), root_path_month_year(-1.year)
= @current_date.strftime("%Y")
= link_to raw("&raquo;"), root_path_month_year(1.year)
%table.calendar{border: 1}
%tr
- Date::DAYNAMES.each do |day_of_week|
%th= day_of_week
- @days_in_month.each_with_index do |week,index|
%tr
- if @beginning_of_month_blanks_count > 0 && index == 0
%td{colspan: @beginning_of_month_blanks_count} &nbsp;
- week.each do |date|
%td{class: multiclass(today: date.today?, weekend: (date.sunday? || date.saturday?))}
%span.day=raw date ? date.day : "&nbsp;"
- if index == (@days_in_month.count)
%td{colspan: @end_of_month_blanks_count} &nbsp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment