我正在使用 Railscast 的日历助手:http ://railscasts.com/episodes/213-calendars-revised但我遇到了问题(下面的日历助手):
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, start_date, end_date, scheduled, block).table
end
class Calendar < Struct.new(:view, :date, :start_date, :end_date, :scheduled, :callback)
HEADER = %w[S M T W T F S]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
header + week_rows
end
end
def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end
def week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "start_date" if day == start_date
classes << "end_date" if day == end_date
classes << "notmonth" if day.month != date.month
classes << "scheduled" if day == scheduled
classes.empty? ? nil : classes.join(" ")
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
end
end
我的应用程序会输出一系列预定日期(使用 ice_cube gem)。对于这些日期中的每一个,我想将它们与日历中的日期相匹配,并为它们分配“预定”类。我不知道该怎么做。这段代码是我想要做的:
classes << "scheduled" if day == scheduled
“预定”来自控制器:
Application_Controller.rb
def scheduled
Schedule.find(params[:id]).itinerary.all_occurrences if params[:id]
end
helper_method :scheduled
它返回以下日期数组:
=> [2014-05-16 00:00:00 -0400, 2014-05-19 00:00:00 -0400, 2014-05-20 00:00:00 -0400, 2014-05-21 00:00:00 -0400, 2014-05-22 00:00:00 -0400, 2014-05-23 00:00:00 -0400, 2014-05-26 00:00:00 -0400, 2014-05-27 00:00:00 -0400, 2014-05-28 00:00:00 -0400, 2014-05-29 00:00:00 -0400]
我已经尝试了很多场景,但我无法弄清楚。
例如,这将起作用并显示这 3 天的“预定”课程,但我不知道如何循环所有预定日期并且仍然有 || 块中的运算符:
def day_classes(day)
...
classes << "scheduled" if Date.yesterday == day || Date.tomorrow == day || Date.today == day
...
end
或者也许有人有更好的主意?