From 4dd10d09ad9c9a3993a3171e9fa6a7dc8fe1d595 Mon Sep 17 00:00:00 2001 From: shlok007 Date: Tue, 7 Feb 2017 01:22:49 +0530 Subject: [PATCH] display concurrent events on event#show --- app/helpers/application_helper.rb | 17 +++++++++++ app/views/proposals/show.html.haml | 17 +++++++++++ spec/helpers/application_helper_spec.rb | 38 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1b8be331..cf343cef 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -575,4 +575,21 @@ module ApplicationHelper def quantity_left_of(resource) "#{resource.quantity - resource.used}/#{resource.quantity}" end + + def concurrent_events(event) + return nil unless event.scheduled? && event.program.selected_event_schedules + event_schedule = event.program.selected_event_schedules.find_by(event: event) + other_event_schedules = event.program.selected_event_schedules.reject { |other_event_schedule| other_event_schedule == event_schedule } + concurrent_events = [] + + event_time_range = (event_schedule.start_time.strftime '%Y-%m-%d %H:%M')...(event_schedule.end_time.strftime '%Y-%m-%d %H:%M') + other_event_schedules.each do |other_event_schedule| + next unless other_event_schedule.event.confirmed? + other_event_time_range = (other_event_schedule.start_time.strftime '%Y-%m-%d %H:%M')...(other_event_schedule.end_time.strftime '%Y-%m-%d %H:%M') + if (event_time_range.to_a & other_event_time_range.to_a).present? + concurrent_events << other_event_schedule.event + end + end + concurrent_events + end end diff --git a/app/views/proposals/show.html.haml b/app/views/proposals/show.html.haml index baf0e404..555ed22b 100644 --- a/app/views/proposals/show.html.haml +++ b/app/views/proposals/show.html.haml @@ -97,3 +97,20 @@ %dt Requires Registration: %dd = link_to "Yes (#{registered_text(@event)})", new_conference_conference_registration_path(@conference.short_title), class: 'btn btn-xs btn-danger', disabled: !@event.registration_possible? + - if concurrent_events(@event).present? + .col-md-12 + %hr + %h4 Happening at the same time: + %ol + - concurrent_events(@event).each do |event| + %li + = link_to conference_program_proposal_path(@conference.short_title, event.id) do + = event.title + %dl + %dt Start Time: + %dd + = event.program.selected_event_schedules.find_by(event: event).start_time.strftime("%Y %B %e %H:%M") + %br + %dt Room: + %dd + = event.room.name diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index a0ac499a..e58f57fc 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -92,4 +92,42 @@ describe ApplicationHelper, type: :helper do end end end + + describe '#concurrent_events' do + before :each do + @other_event = create(:event, program: conference.program, state: 'confirmed') + schedule = create(:schedule, program: conference.program) + conference.program.update_attributes!(selected_schedule: schedule) + @event_schedule = create(:event_schedule, event: event, start_time: conference.start_date, room: create(:room), schedule: schedule) + @other_event_schedule = create(:event_schedule, event: @other_event, start_time: conference.start_date, room: create(:room), schedule: schedule) + end + + describe 'does return correct concurrent events' do + it 'when events starts at the same time' do + expect(concurrent_events(event).include?(@other_event)).to eq true + end + + it 'when event is in between the other event' do + @event_schedule.update_attributes!(start_time: @other_event_schedule.start_time + 10.minutes) + expect(concurrent_events(event).include?(@other_event)).to eq true + end + end + + describe 'does not return as concurrent event ' do + it 'when event is not scheduled' do + @event_schedule.destroy + expect(concurrent_events(event).present?).to eq false + end + + it 'when one event starts and other ends at the same time' do + @event_schedule.update_attributes!(start_time: @other_event_schedule.end_time) + expect(concurrent_events(event).present?).to eq false + end + + it 'when conference program does not have a selected schedule' do + conference.program.update_attributes!(selected_schedule_id: nil) + expect(concurrent_events(event).present?).to eq false + end + end + end end