Skip to content

Commit

Permalink
Fix filtering by non-exam / review-session, date integers
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslzhu committed Mar 1, 2019
1 parent e6a3859 commit 84a68e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
22 changes: 15 additions & 7 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def index
@events = @events.order(start_time: sort_direction)
end

@events = @events.where.not(name: ["Exam", "Review Session"])
@events = @events.joins(:event_type)
.where.not(event_types: {name: ["Exam", "Review Session"]})

if (page_no = Integer(params[:page] || 1) rescue nil)
@events = @events.page(page_no).per_page(per_page)
Expand Down Expand Up @@ -93,18 +94,25 @@ def confirm_rsvps
end

def calendar
month = (params[:month] || Time.now.month).to_i
year = (params[:year] || Time.now.year).to_i
unless (month = Integer(params[:month] || Time.now.month) rescue nil) &&
month.between?(1, 12)
self.render_404
end

unless (year = Integer(params[:year] || Time.now.year) rescue nil) &&
year.between?(1900, 2100)
self.render_404
end
# month = (params[:month] || Time.now.month).to_i
# year = (params[:year] || Time.now.year).to_i
# TODO: Fix this, I think we have timezone issues
@start_date = Time.local(year, month).beginning_of_month
@end_date = Time.local(year, month).end_of_month
@events = Event.with_permission(@current_user)
.where(start_time: @start_date..@end_date)
.order(:start_time)
@events.to_a.delete_if { |e|
EventType.where("name IN (?)", ["Exam", "Review Session"])
.include?(e.event_type)
}
@events = @events.joins(:event_type)
.where.not(event_types: {name: ["Exam", "Review Session"]})

# Really convoluted way of getting the first Sunday of the calendar,
# which usually lies in the previous month
Expand Down
21 changes: 14 additions & 7 deletions app/controllers/tutor_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@ def schedule
end

def calendar
month = (params[:month] || Time.now.month).to_i
year = (params[:year] || Time.now.year).to_i
unless (month = Integer(params[:month] || Time.now.month) rescue nil) &&
month.between?(1, 12)
self.render_404
end

unless (year = Integer(params[:year] || Time.now.year) rescue nil) &&
year.between?(1900, 2100)
self.render_404
end
# month = (params[:month] || Time.now.month).to_i
# year = (params[:year] || Time.now.year).to_i
# TODO: Fix this, I think we have timezone issues
@start_date = Time.local(year, month).beginning_of_month
@end_date = Time.local(year, month).end_of_month
@events = Event.with_permission(@current_user)
.where(start_time: @start_date..@end_date)
.order(:start_time)

@events = @events.select { |e|
EventType.where("name IN (?)", ["Exam", "Review Session"])
.include?(e.event_type)
}

@events = @events.joins(:event_type)
.where.not(event_types: {name: ["Exam", "Review Session"]})
# Really convoluted way of getting the first Sunday of the calendar,
# which usually lies in the previous month
@calendar_start_date = (@start_date.wday == 0) ? @start_date : @start_date.next_week.ago(8.days)
Expand Down

0 comments on commit 84a68e5

Please sign in to comment.