Class: RailsFancies::FancyWeeklyCalendarHelper::Calendar

Inherits:
Struct
  • Object
show all
Defined in:
lib/rails_fancies/fancy_weekly_calendar_helper.rb

Constant Summary collapse

HEADER =
%w[Time Monday Tuesday Wednesday Thursday Friday Saturday Sunday]
START_DAY =
:monday
SLOT_DURATION =
30.minutes
SLOT_START =
17
SLOT_END =
31

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbackObject

Returns the value of attribute callback

Returns:

  • (Object)

    the current value of callback



11
12
13
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 11

def callback
  @callback
end

#dateObject

Returns the value of attribute date

Returns:

  • (Object)

    the current value of date



11
12
13
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 11

def date
  @date
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



11
12
13
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 11

def options
  @options
end

#viewObject

Returns the value of attribute view

Returns:

  • (Object)

    the current value of view



11
12
13
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 11

def view
  @view
end

Class Method Details

.availability_hashObject



96
97
98
99
100
101
102
103
104
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 96

def self.availability_hash
  { monday: [17, 18, 19, 20, 21, 24, 25, 26, 27],
    tuesday: [17, 18, 19, 20, 21, 24, 25, 26, 27],
    wednesday: [17, 18, 19, 20, 21],
    thursday: [17, 18, 19, 20, 21, 24, 25, 26, 27],
    friday: [17, 18, 19, 20, 21, 24, 25, 26, 27],
    sunday: [24, 25, 26, 27]
  }
end

.available?(day, slot_num) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 90

def self.available?(day, slot_num)
  day_sym = day.strftime("%A").downcase.to_sym
  slots = Calendar.availability_hash[day_sym]
  slot_num.in? slots if slots.present?
end

Instance Method Details

#day_classes(day, slot_num) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 63

def day_classes(day, slot_num)
  classes = []
  classes << 'today' if day == Date.today
  classes << 'notmonth' if day.month != date.month
  classes << 'available' if Calendar.available?(day, slot_num)
  classes.empty? ? nil : classes.join(' ')
end

#day_slot_cell(day, slot_num) ⇒ Object



59
60
61
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 59

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

#headerObject



26
27
28
29
30
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 26

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

#slot_cell(slot_num) ⇒ Object



44
45
46
47
48
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 44

def slot_cell(slot_num)
   :td do
    slot_num_to_time(slot_num)
  end
end

#slot_num_to_time(slot_num) ⇒ Object



50
51
52
53
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 50

def slot_num_to_time(slot_num)
  start_time = slot_start_time + (slot_num - SLOT_START) * SLOT_DURATION
  Time.at(start_time).utc.strftime("%H:%M")
end

#slot_start_timeObject



55
56
57
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 55

def slot_start_time
  SLOT_START * SLOT_DURATION
end

#slotsObject



85
86
87
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 85

def slots
  (SLOT_START..SLOT_END) 
end

#tableObject



20
21
22
23
24
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 20

def table
   :table, class: "calendar" do
    header + weekly_rows
  end
end

#weekly_rowsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 32

def weekly_rows
  slot_num = SLOT_START - 1
  weekly_slots.map do |week|
    slot_num += 1
     :tr do
      row = [slot_cell(slot_num)]
      row += week.map { |day| day_slot_cell(day,slot_num) }
      row.join.html_safe
    end
  end.join.html_safe
end

#weekly_slotsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rails_fancies/fancy_weekly_calendar_helper.rb', line 71

def weekly_slots
	puts 'start day', start_day, options
	start_day = options[:start_day] || START_DAY
  first = date.beginning_of_week(start_day)
  last = date.end_of_week(start_day)
  weeks = []
  week = (first..last).to_a
  slots = (SLOT_START..SLOT_END)
  slots.each do
    weeks << week
  end
  weeks
end