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
  • Select an option

  • Save advorak/2760681 to your computer and use it in GitHub Desktop.

Select an option

Save advorak/2760681 to your computer and use it in GitHub Desktop.

Revisions

  1. advorak revised this gist May 25, 2012. 5 changed files with 91 additions and 10 deletions.
    44 changes: 44 additions & 0 deletions home.css.scss
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    // 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;
    }
    }
    }
    6 changes: 6 additions & 0 deletions home.js.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # 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)
    16 changes: 10 additions & 6 deletions home_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,17 @@
    class HomeController < ApplicationController
    def index
    @today = Date.today
    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[@today.beginning_of_month.cwday] = (@today.beginning_of_month.day..@today.end_of_month.day).to_a
    @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 = @today.beginning_of_month.cwday
    @beginning_of_month_blanks_count.times { @days_in_month[0].shift }
    @beginning_of_month_blanks_count = @current_date.beginning_of_month.wday
    @days_in_month[0].compact!

    @end_of_month_blanks_count = (6 - @today.end_of_month.cwday)
    @end_of_month_blanks_count = (6 - @current_date.end_of_month.wday)
    puts @days_in_month.inspect
    end
    end
    end
    18 changes: 18 additions & 0 deletions home_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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
    17 changes: 13 additions & 4 deletions index.html.haml
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    %table{border: 1}
    %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
    @@ -7,8 +15,9 @@
    - if @beginning_of_month_blanks_count > 0 && index == 0
    %td{colspan: @beginning_of_month_blanks_count} &nbsp;

    - week.each do |day|
    %td{style: (day == @today.day && "background-color: yellow;")}=raw day ? day : "&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 - 1)
    - if index == (@days_in_month.count)
    %td{colspan: @end_of_month_blanks_count} &nbsp;
  2. advorak revised this gist May 21, 2012. 2 changed files with 14 additions and 7 deletions.
    9 changes: 7 additions & 2 deletions home_controller.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,12 @@ class HomeController < ApplicationController
    def index
    @today = Date.today
    @days_in_month = []
    @days_in_month[@today.beginning_of_month.cwday] = (1..@today.end_of_month.day).to_a
    @days_in_month = @days_in_month.flatten.in_groups_of(7)
    @days_in_month[@today.beginning_of_month.cwday] = (@today.beginning_of_month.day..@today.end_of_month.day).to_a
    @days_in_month = @days_in_month.flatten.in_groups_of(7, false)

    @beginning_of_month_blanks_count = @today.beginning_of_month.cwday
    @beginning_of_month_blanks_count.times { @days_in_month[0].shift }

    @end_of_month_blanks_count = (6 - @today.end_of_month.cwday)
    end
    end
    12 changes: 7 additions & 5 deletions index.html.haml
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    %h1 Home#index
    %p Find me in app/views/home/index.html.haml

    %table{border: 1}
    %tr
    - Date::DAYNAMES.each do |day_of_week|
    %th= day_of_week
    %tr
    - @days_in_month.each do |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 |day|
    %td{style: (day == @today.day && "background-color: yellow;")}=raw day ? day : "&nbsp;"

    - if index == (@days_in_month.count - 1)
    %td{colspan: @end_of_month_blanks_count} &nbsp;
  3. advorak revised this gist May 21, 2012. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion index.html.haml
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    %h1 Home#index
    %p Find me in app/views/home/index.html.haml

    %table{border: 1}
    %tr
    - Date::DAYNAMES.each do |day_of_week|
    @@ -6,4 +9,4 @@
    - @days_in_month.each do |week|
    %tr
    - week.each do |day|
    %td=raw day ? day : "&nbsp;"
    %td{style: (day == @today.day && "background-color: yellow;")}=raw day ? day : "&nbsp;"
  4. advorak revised this gist May 21, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion home_controller.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ class HomeController < ApplicationController
    def index
    @today = Date.today
    @days_in_month = []
    @days_in_month[@today.beginning_of_month.cwday] = (@today.beginning_of_month.day..@today.end_of_month.day).to_a
    @days_in_month[@today.beginning_of_month.cwday] = (1..@today.end_of_month.day).to_a
    @days_in_month = @days_in_month.flatten.in_groups_of(7)
    end
    end
  5. @invalid-email-address Anonymous created this gist May 21, 2012.
    8 changes: 8 additions & 0 deletions home_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class HomeController < ApplicationController
    def index
    @today = Date.today
    @days_in_month = []
    @days_in_month[@today.beginning_of_month.cwday] = (@today.beginning_of_month.day..@today.end_of_month.day).to_a
    @days_in_month = @days_in_month.flatten.in_groups_of(7)
    end
    end
    9 changes: 9 additions & 0 deletions index.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    %table{border: 1}
    %tr
    - Date::DAYNAMES.each do |day_of_week|
    %th= day_of_week
    %tr
    - @days_in_month.each do |week|
    %tr
    - week.each do |day|
    %td=raw day ? day : "&nbsp;"