Class: CalendarHelper::Calendar

Inherits:
Struct
  • Object
show all
Defined in:
app/helpers/calendar_helper.rb

Constant Summary collapse

HEADER =

HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]

[%w(Sun Mon Tue Wed Thu Fri Sat)].freeze
START_DAY =
:sunday

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbackObject

Returns the value of attribute callback

Returns:

  • (Object)

    the current value of callback



8
9
10
# File 'app/helpers/calendar_helper.rb', line 8

def callback
  @callback
end

#dateObject

Returns the value of attribute date

Returns:

  • (Object)

    the current value of date



8
9
10
# File 'app/helpers/calendar_helper.rb', line 8

def date
  @date
end

#viewObject

Returns the value of attribute view

Returns:

  • (Object)

    the current value of view



8
9
10
# File 'app/helpers/calendar_helper.rb', line 8

def view
  @view
end

Instance Method Details

#day_cell(day) ⇒ Object



35
36
37
# File 'app/helpers/calendar_helper.rb', line 35

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

#day_classes(day) ⇒ Object



39
40
41
42
43
44
45
# File 'app/helpers/calendar_helper.rb', line 39

def day_classes(day)
  classes = []
  # classes << "today" if day == Time.zone.today
  # classes << "today" if day == Time.zone.today or day == @date.day
  classes << 'notmonth' if day.month != date.month
  classes.empty? ? nil : classes.join(' ')
end

#headerObject



21
22
23
24
25
# File 'app/helpers/calendar_helper.rb', line 21

def header
   :tr do
    HEADER.map { |day|  :th, day }.join.html_safe
  end
end

#tableObject



15
16
17
18
19
# File 'app/helpers/calendar_helper.rb', line 15

def table
   :table, class: 'calendar' do
    header + week_rows
  end
end

#week_rowsObject



27
28
29
30
31
32
33
# File 'app/helpers/calendar_helper.rb', line 27

def week_rows
  weeks.map do |week|
     :tr do
      week.map { |day| day_cell(day) }.join.html_safe
    end
  end.join.html_safe
end

#weeksObject



47
48
49
50
51
# File 'app/helpers/calendar_helper.rb', line 47

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