日历:date_started直到Date.current?

时间:2015-12-30 21:53:30

标签: css ruby-on-rails ruby calendar

我们如何从date_started转到Date.current

例如,此代码仅计算例程的date_started

routines = current_user.routines.group_by {|i| i.date_started.to_date}
classes << "completed" if routines.include?(day)    

但是我想完成&#34;完成&#34;,这是使td背景变为蓝色的CSS类,就像我在下面解释的那样:

enter image description here

模式

create_table "routines", force: true do |t|
  t.datetime "date_started"
  t.string   "action"
  t.integer  "user_id"
end

我已按照 tutorial 来制作日历。

FULL calendar_helper.rb

module CalendarHelper
    def calendar(date = Date.current, &block)
        Calendar.new(self, date, block).table
    end

    class Calendar < Struct.new(:view, :date, :callback)
        HEADER = %w[Sun Mon Tue Wed Thu Fri Sat]
        START_DAY = :sunday

        delegate :content_tag, :current_user, to: :view

        def past
        end

        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 << "lastmonth" if day.month < date.month
            classes << "nextmonth" if day.month > date.month
            ###
            routines = current_user.routines.group_by {|i| i.date_started.to_date}
            classes << "completed" if routines.include?(day)
            ### 
            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

1 个答案:

答案 0 :(得分:1)

如果我理解正确,为什么不检查日历中的给定日期是否在指定的日期范围内?

控制器:

@date_started = Date.today - 2.days
@today = Date.today
@given_date = Date.yesterday

HTML:

<td class="<%= 'completed' if @given_date >= @date_started and @given_date <= @today %>">

修改

我认为@given_date需要是一个数组,因此每天都可以在HTML中进行迭代。如果不了解你如何渲染日历,就不清楚如何写出来。但总体思路应该有效。

EDIT2

教程HTML是这样的:

<%= calender do |date| %>
  <%= date.day %>
  # assuming this is where you add your `completed` css class somehow
  # check if date.day falls between the @date_started and @today range
<% end %>

EDIT3

好的,我知道为什么现在这让人困惑。我想我明白了。您有许多routine个对象,并且您希望routine.date_startedDate.today之间的任何日期都突出显示为蓝色。我相信这对你有用:

def day_classes(day)
  classes = []
  classes << "lastmonth" if day.month < date.month
  classes << "nextmonth" if day.month > date.month
  ###
  routines = current_user.routines.group_by {|i| i.date_started.to_date}
  # remove this line:
  #classes << "completed" if routines.include?(day)
  # in favor of this line:
  classes << "completed" if routines.any? {|date_attr, routine_obj| day >= date_attr and day <= Date.today } 
  ### 
  classes.empty? ? nil : classes.join(" ")
end