Class: TimeTable::TimetableCalendarHelper::TimetableCalendar

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/time_table/timetable_calendar_helper.rb

Constant Summary collapse

MAX_NUMBER_OF_DAYS =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bounds:, range: 7, query_params: {}, view: nil) ⇒ TimetableCalendar

Returns a new instance of TimetableCalendar.



49
50
51
52
53
54
55
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 49

def initialize(bounds:, range: 7, query_params: {}, view: nil)
  @calendar_boundaries = bounds
  @query_params = query_params 
  @view = view 

  @current_date_range = get_bounded_date_range(range)
end

Instance Attribute Details

#calendar_boundariesObject

Returns the value of attribute calendar_boundaries.



43
44
45
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 43

def calendar_boundaries
  @calendar_boundaries
end

#current_date_rangeObject

Returns the value of attribute current_date_range.



43
44
45
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 43

def current_date_range
  @current_date_range
end

#multi_timetableObject

Returns the value of attribute multi_timetable.



43
44
45
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 43

def multi_timetable
  @multi_timetable
end

#query_paramsObject

Returns the value of attribute query_params.



43
44
45
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 43

def query_params
  @query_params
end

#viewObject

Returns the value of attribute view.



43
44
45
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 43

def view
  @view
end

Instance Method Details

#calendar_end_boundaryObject



165
166
167
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 165

def calendar_end_boundary
  @calendar_end_boundary ||= calendar_boundaries.last
end

#calendar_limitObject



169
170
171
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 169

def calendar_limit 
  @calendar_limit ||= calendar_boundaries.count
end

#calendar_start_boundaryObject



161
162
163
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 161

def calendar_start_boundary
  @calendar_start_boundary ||= calendar_boundaries.first 
end

#current_end_dateObject Also known as: end_date



149
150
151
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 149

def current_end_date
  @current_end_date ||= @current_date_range.last
end

#current_number_of_daysObject Also known as: number_of_days



155
156
157
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 155

def current_number_of_days 
  @current_number_of_days ||= @current_date_range.count
end

#current_start_dateObject Also known as: start_date

ATTRIBUTE READERS ##



143
144
145
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 143

def current_start_date
  @current_start_date ||= @current_date_range.first
end


130
131
132
133
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 130

def disabled_nav_link(text = nil, **params, &block)
  params[:class] = "#{params[:class]} disabled"
  link_to(text, "", **params, &block)
end


135
136
137
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 135

def link_to(*args, **params, &block)
  @view.link_to *args.compact, **params, &block
end

#months_stringObject

INFORMATION ##



79
80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 79

def months_string 
  if current_start_date.year == current_end_date.year
    if current_start_date.month == current_end_date.month
      "#{current_start_date.strftime('%B %Y')}"
    else
      "#{current_start_date.strftime('%B')} - #{current_end_date.strftime('%B %Y')}"
    end
  else 
    "#{current_start_date.strftime('%B %Y')} - #{current_end_date.strftime('%B %Y')}"
  end
end


119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 119

def next_link(text = nil, **params, &block)
  if current_start_date + current_number_of_days.days > calendar_end_boundary
    return disabled_nav_link(text, **params, &block)
  end

  next_day = current_start_date + current_number_of_days.days
  next_day = calendar_end_boundary - current_number_of_days.days + 1 if next_day + current_number_of_days.days > calendar_end_boundary

  link_to(text, @query_params.merge(start_date: next_day), **params, &block)
end

NAVIGATION LINKS ##



108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 108

def previous_link(text = nil, **params, &block)
  if current_start_date == calendar_start_boundary
    return disabled_nav_link(text, **params, &block) 
  end
  
  previous_day = current_start_date - current_number_of_days
  previous_day = calendar_start_boundary if previous_day < calendar_start_boundary

  link_to(text, @query_params.merge(start_date: previous_day), **params, &block)
end

QUERY LINKS ##



95
96
97
98
99
100
101
102
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 95

def query_link(text = nil, query:, **params, &block)
  active = query.reduce(true) do |acc, (key, value)|
    acc && send(key) == value
  end

  params[:class] = "#{params[:class]} active" if active
  link_to(text, @query_params.merge(query), **params, &block)
end

#render_days(&block) ⇒ Object

HEADER RENDERING ##



65
66
67
68
69
70
71
72
73
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 65

def render_days(&block)
  @view. :div, class: "timetable-headers" do 
    @view.concat(current_date_range.map do |day| 
      @view. :div, class: "timetable-header" do 
        yield(day)
      end
    end.join("").html_safe)
  end
end

#set_multi_timetable(**options) ⇒ Object



57
58
59
# File 'app/helpers/time_table/timetable_calendar_helper.rb', line 57

def set_multi_timetable(**options)
  @multi_timetable = Timetable.new(start_date: current_start_date, end_date: current_end_date, view: view, **options)
end