将日期数组比较为一天(在rails日历中)

时间:2014-05-27 13:08:22

标签: ice-cube

我使用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)。对于这些日期中的每一个,我希望将它们与日历中的日期匹配,并将它们分配给班级&#34;预定&#34;。我无法弄清楚如何做到这一点。这段代码是我尝试工作的原因:

classes << "scheduled" if day == scheduled

&#39;调度&#39;来自控制器:

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] 

我尝试了很多场景,但我无法弄明白。

作为一个例子,这将起作用并显示&#34;预定&#34;这3天的课程,但我无法弄清楚如何循环所有预定的日期,仍然有||块中的运算符:

def day_classes(day)
  ...
  classes << "scheduled" if Date.yesterday == day || Date.tomorrow == day || Date.today == day
  ...
end

或许某人有更好的主意?

1 个答案:

答案 0 :(得分:0)

在浏览了ice_cube gem之后,我找到了一个方便的方法,完全符合我的需要。

def day_classes(day)
  ...
  classes << "scheduled" if scheduled.itinerary.occurs_on?(day)
  ...
end