Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overlap validation to blocks (WIP) #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/models/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ class Block < ActiveRecord::Base

belongs_to :schedule

before_validation :check_for_overlap, message: "This block overlaps another"

# Calculates when a block ends
def end_time
self.start_time + self.duration
end

def occupied_times
ot = []
self.start_time.upto self.duration do |i|
ot << i
end
return ot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(self.start_time..self.duration).collect ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i would cache it,

def occupied_times
  @occupied_times ||= (self.start_time..self.duration).collect
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL hey sean you need them to be arrays so you can & compare them.

def occupied_times
  @occupied_times ||= self.start_time.upto(self.duration).inject([]) { |sum, memo| sum << memo }
end

end

def immutable?
ret = false
ret = true if self.teachers.length > 1 || self.student_groups.length > 1
Expand All @@ -33,4 +42,24 @@ def day?
return 'Friday'
end
end

private
def check_for_overlap
if student_groups.present?
student_groups.each do |sg|
sg.blocks.each do |b|
overlap = self.occupied_times & b.occupied_times
return false if overlap.length > 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true if overlap.empty?

end
end
end
if teachers.present?
teachers.each do |t|
t.blocks.each do |b|
overlap = self.occupied_times & b.occupied_times
return false if overlap.length > 0
end
end
end
end
end