【问题标题】:Calculating the number of weeks in a month for a calendar in Rails在 Rails 中计算日历一个月的周数
【发布时间】:2015-03-16 23:10:31
【问题描述】:

我有一个显示当月公共事件的日历。这是一个响应式布局,所以我需要能够计算 week_row 高度。因此,我需要能够计算该月的周数,包括部分周(包含当月和上/下个月的天数的周)。

以下方法计算该月的周数:

def weeks_in_month(date)
  @week_count = (0.5 + (date.end_of_month.day + date.beginning_of_month.wday).to_f / 7.0).round
end

将适当的类添加到<div> 的代码是:

def week_rows
   weeks.map {|week|
     content_tag :div, class: week_classes(week) do
       week.map { |day| day_cell(day) }.join.html_safe
     end
   }.join.html_safe
end

def week_classes(date)
  classes = ['week']
  classes << "wk_rows_4" if weeks_in_month(date) == 4
  classes << "wk_rows_5" if weeks_in_month(date) == 5
  classes << "wk_rows_6" if weeks_in_month(date) == 6
  classes.empty? ? nil : classes.join(" ")
end

但是,我错过了一些东西。除了默认的 'week' 类之外,week_classes 没有填充任何内容。

这是基于 RailsCasts #213,根据 this tutorial 修改创建一个没有表格的日历,这也大量借鉴了 RC#231。

知道为什么它没有为&lt;div&gt; 分配正确的类吗?

日历中的其他所有内容都在正常工作。完整代码如下:

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

  class Calendar < Struct.new(:view, :date, :callback)
    HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
    START_DAY = :sunday

    delegate :content_tag, to: :view

    def calendar_div
       content_tag 'div', id: "calendar" do
          header + week_rows
       end
    end

    def header
       content_tag 'div', id: 'weekdays' do
          HEADER.map { |day| content_tag :div, class: 'weekday' do
             day
          end }.join.html_safe
       end
    end

    def week_rows
       weeks.map {|week|
          content_tag :div, class: week_classes(week) do
             week.map { |day| day_cell(day) }.join.html_safe
          end
       }.join.html_safe
    end

    def week_classes(date)
       classes = ['week']
       classes << "wk_rows_4" if weeks_in_month(date) == 4
       classes << "wk_rows_5" if weeks_in_month(date) == 5
       classes << "wk_rows_6" if weeks_in_month(date) == 6
       classes.empty? ? nil : classes.join(" ")
    end

    def day_cell(day)
       content_tag :div, view.capture(day, &callback), class: day_classes(day)
    end

    def day_classes(day)
       classes = ['day']
       classes << "today" if day == Date.today
       classes << "notmonth" if day.month != date.month
       classes << "month" if day.month == date.month
       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

    def weeks_in_month(date)
       @week_count = (0.5 + (date.end_of_month.day + date.beginning_of_month.wday).to_f / 7.0).round
    end
  end

【问题讨论】:

  • 您是否对weeks_in_month 方法进行了任何测试以表明它按预期工作?还有,weeks_in_month中的实例变量是干什么的,有必要吗?
  • @philnash - 不,没有测试。实例变量就在那里,所以我可以快速扫描代码。它将在第一次重构时被删除。 day_classes 代码有效,但 week_classes 无效。这就是让我难过的原因。
  • 嗨@Matteo,在这种情况下,我鼓励您为不起作用的方法编写一些测试。这样,您可能会发现哪里出了问题,或者至少是如何解决问题的起点。
  • @philnash - 是的,你是对的。我应该为我的项目编写测试。我只是还不想纠结于一套新的语法。

标签: css ruby-on-rails calendar


【解决方案1】:

好的,所以一些摆弄让我找到了这个解决方案:

  def week_rows
     weeks.map {|week|
        content_tag :div, class: week_classes do
           week.map { |day| day_cell(day) }.join.html_safe
        end
     }.join.html_safe
  end

  def week_classes
     classes = ['week']
     classes << "wk_rows_4" if weeks_in_month == 4
     classes << "wk_rows_5" if weeks_in_month == 5
     classes << "wk_rows_6" if weeks_in_month == 6
     classes.empty? ? nil : classes.join(" ")
  end

  def weeks_in_month
     (0.5 + (date.end_of_month.day + date.at_beginning_of_month.wday).to_f / 7.0).round
  end

没有必要将(date) 作为参数传入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-05
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多