Skip to content

Instantly share code, notes, and snippets.

@rafarubert
Created July 7, 2011 14:27
Show Gist options
  • Save rafarubert/1069627 to your computer and use it in GitHub Desktop.
Save rafarubert/1069627 to your computer and use it in GitHub Desktop.
calendario baseado has_Calendar
#encoding: utf-8
module Atendente::AgendaHelper
def calendar(options={}, &block)
options = {
:events => nil,
:field => :created_at,
:header_format => :week,
:id => "calendar",
:month => Date.today.month,
:today => nil,
:year => Date.today.year
}.merge(options)
cols = String.new
contents = "<div class='grade'><div class='grade-content'>"
head = String.new
rows = String.new
today = Date.today
date = today.beginning_of_week
date = date - 1.day if RUBY_PLATFORM =~ /darwin/
# bug de fevereiro
today_day = today.day
last_day = Date.new(options[:year], options[:month], 1).end_of_month.day
today_day = last_day if last_day < today_day
#
cmd = 'cal '; cmd << '-m ' unless RUBY_PLATFORM =~ /darwin/
output = IO.popen("#{cmd} #{options[:month]} #{options[:year]}") {|f| f.read}
io = StringIO.new(output)
lines = io.readlines
lines = lines[2, lines.size-2]
lines.map!(&:chomp)
days = lines.inject([]) do |holder, line|
0.step(line.size, 3){|n|
holder << line[n, 3].squish
}
puts holder
holder
end
head << content_tag(:ul, :class => 'dias') do
begin
(0..6).collect { |i| content_tag(:li, l(date + i.days, :format => options[:header_format])) } * ""
rescue
(0..6).collect { |i| content_tag(:li, date + i.days)} * ""
end
end
rows << content_tag(:ul, :class => :dias) do
days.in_groups_of(7, "") do |group|
group.inject("") do |cols, day|
head << content_tag(:ul, :class => 'datas') do
if day.blank?
content_tag(:li, day,:class => "mes-corrente")
else
content_tag(:li, day,:class => "mes-inativo")
end
end
end
end
end
contents = CGI.unescapeHTML(contents+head + rows)
#concat(contents) if block_given?
contents = CGI.unescapeHTML(String.new(contents))
raw contents+"<div></div>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment