issue #1. I'm a newb.
So, I'm trying to set up a calendar of lessons using the
WeekViewHelper snippet found here:
http://snippets.dzone.com/posts/show/5206 (very slightly altered). The
problem only occurs when I try to set the :url option. Without it
works fine, except I haven't been able to (can't figure it out) create
links to go to the next or previous weeks.
Any help, or suggestions are greatly appreciated, or if you know of a
different way to go about it.
The code for the various files are posted below. "start_date" and
"end_date" are columns in the table "lessons", while "starts_at" and
"ends_at" are options for the helper.
WEEK_VIEW_HELPER.RB:
require 'date'
# Author: Josh Adams
# This helper is based on CalendarHelper.
# WeekViewHelper allows you to draw a databound week view calendar
with fine-grained CSS formatting
module WeekViewHelper
VERSION = '0.0.1'
# Returns an HTML week-view calendar. In its simplest form, this
method generates a plain
# calendar (which can then be customized using CSS) for a given
span of days.
# However, this may be customized in a variety of ways -- changing
the default CSS
# classes, generating the individual day entries yourself, and so
on.
#
# The following options are required:
# :starts_at
# :ends_at
#
# The following are optional, available for customizing the
default behaviour:
# :table_class => "week-view" # The class for the
<table> tag.
# :day_name_class => "dayName" # The class is for the
names of the days, at the top.
# :day_class => "day" # The class for the
individual day number cells.
# This may or may not
be used if you specify a block (see below).
# :show_today => true # Highlights today on
the calendar using the CSS class 'today'.
# # Defaults to true.
# :previous_span_text => nil # Displayed left if
set
# :next_span_text => nil # Displayed right if
set
#
# For more customization, you can pass a code block to this
method, that will get two argument, both DateTime objects,
# and return a values for the individual table cells. The block
can return an array, [cell_text, cell_attrs],
# cell_text being the text that is displayed and cell_attrs a hash
containing the attributes for the <td> tag
# (this can be used to change the <td>'s class for customization
with CSS).
# This block can also return the cell_text only, in which case the
<td>'s class defaults to the value given in
# +:day_class+. If the block returns nil, the default options are
used.
#
# Example usage:
# week_view(:starts_at => (Date.today - 5), :ends_at =>
Date.today) # This generates the simplest possible week-view.
# week_view(:starts_at => (Date.today - 5), :ends_at =>
Date.today, :table_class => "calendar_helper"}) # This generates a
week-view, as
#
# before, but the <table>'s class
#
# is set to "calendar_helper".
# week_view(:starts_at => (Date.today - 5), :ends_at =>
Date.today) do |s| # This generates a simple week-view, but gives
special spans
# if listOfSpecialSpans.include?(s) # (spans that are
in the array listOfSpecialSpans) one CSS class,
# ["", {:class => "specialSpan"}] # "specialSpan", and
gives the rest of the spans another CSS class,
# else # "normalSpan".
You can also use this to highlight the current time differently
# ["", {:class => "normalSpan"}] # from the rest of
the days, etc.
# end
# end
#
# For consistency with the themes provided in the calendar_styles
generator, use "specialSpan" as the CSS class for marked days.
#
def week_view(options = {}, &block)
raise(ArgumentError, "No start date given") unless
options.has_key?(:starts_at)
raise(ArgumentError, "No end date given") unless options.has_key?
(:ends_at)
span = (options[:ends_at] - options[:starts_at]).to_i # Get the
number of days represented by the span given
dates = (options[:starts_at]..options[:ends_at])
start_time = 13
end_time = 21
time_range = (start_time..end_time).to_a
duration = 15
block ||= Proc.new {|d| nil}
defaults = {
:table_class => 'week-view',
:day_name_class => 'dayName',
:day_class => 'day',
:show_today => true,
:previous_span_text => nil,
:next_span_text => nil
}
options = defaults.merge options
if options[:url]
next_starts_at = options[:ends_at] + 1
next_ends_at = next_starts_at + 7
next_link = link_to('>>',
url_for(options[:url].merge({options[:starts_at] => next_starts_at,
options[:ends_at] => next_ends_at})) + options[:url_append])
prev_starts_at = options[:starts_at] - span
prev_ends_at = options[:starts_at] - 1
#prev_link = link_to('<<',
url_for(options[:url].merge(:starts_at => prev_starts_at, :ends_at =>
prev_ends_at)) + options[:url_append])
end
#next_starts_at = options[:ends_at] + 1
#next_ends_at = next_starts_at + 5
[EMAIL PROTECTED] = options[:starts_at] = next_starts_at
[EMAIL PROTECTED] = options[:ends_at] = next_ends_at
#redirect_to 'index'
#next_link = link_to('>>',
url_for(options[:url].merge(:starts_at => next_starts_at, :ends_at =>
next_ends_at)) + options[:url_append])
# TODO Use some kind of builder instead of straight HTML
cal = %(<table class="#{options[:table_class]}">\n)
cal << %(\t<thead>\n\t\t<tr>\n)
cal << %(\t\t\t<th>#{dates.first.strftime("%Y")}</th>\n)
dates.each do |d|
cal << "\t\t\t<th#{Date.today == d ? " class='today'" :
""}>#{d.strftime("%A")}<br />#{d.strftime("%m/%d")}</th>\n"
end
cal << "\t\t</tr>\n\t</thead>\n\t<tbody>\n"
time_range.each do |hour|
minutes = 0
print_hour = hour.to_s.rjust(2, '0')
4.times do |i|
print_minutes = minutes.to_s.rjust(2, '0')
cal << %(\t\t<tr class='m#{print_minutes} d#{duration}'>\n)
if hour < 12
cal << %(\t\t\t<th rowspan="4"><h3>#{hour}</h3>AM</th>\n)
if i==0
end
if hour == 12
cal << %(\t\t\t<th rowspan="4"><h3>#{hour}</h3>PM</th>\n)
if i==0
end
if hour > 12
hour = hour - 12
cal << %(\t\t\t<th rowspan="4"><h3>#{hour}</h3>PM</th>\n)
if i==0
end
options[:starts_at].upto(options[:ends_at]) do |d|
the_minutes = minutes
print_start_minutes = the_minutes.to_s.ljust(2, '0')
starts_attime_string = %
(#{d.to_s(:db)}T#{print_hour}:#{print_start_minutes}:00-06:00)
starts_attime =
DateTime.parse(starts_attime_string).to_datetime
ends_attime = (starts_attime +
duration.minutes).to_datetime
range = (starts_attime...ends_attime)
# cell_attrs should return a hash.
cell_text, cell_attrs = block.call(range)
cell_text ||= ""
cell_attrs ||= {}
cell_attrs[:class] = cell_attrs[:class].to_s + " today" if
Date.today == d
cell_attrs = cell_attrs.map {|k, v| %
(#{k}="#{v}") }.join(" ")
cal << "\t\t\t<td #{cell_attrs}>\n#{cell_text} \t\t
\t</td>\n"
end
minutes += duration
cal << %(\t\t</tr>)
end
end
cal << "\n\t</tbody>\n</table>\n"
cal << "<br/>\n"
cal << %(#{hour})
end
private
end
WKLY_CAL_CONTROLLER.RB:
class WklyCalController < ApplicationController
def index
@lessons = Lesson.find(:all)
end
end
INDEX.HTML.ERB (WKLY_CAL)
<h1>Weekly Calendar</h1>
<% for lesson in @lessons %><%end%>
<%= week_view :starts_at => (Date.today - 2), :ends_at => (Date.today
+ 2), :url => wkly_cal_path do |range|
cell_text = ""
cell_attrs = {}
@lessons.each do |e|
#if range.first.to_date == e.starts_at.to_date#range.include?
(e.starts_at.to_datetime)
if range.include?(e.start_date.to_datetime)
cell_text << "<div class='event-container'>\n"
cell_text << lesson.student_name
cell_text << "\n"
cell_text << "</div>\n"
end
end
[cell_text, cell_attrs]
end
-%>
(IN ROUTES.RB):
map.wkly_cal '', :controller => 'wkly_cal', :action => 'index'
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---