Fixed DeleteEventSchedules issue

Issues with DeleteEventSchedules method in conference controller:
1.It deletes EventSchedules of all the conferences that are not in the
hours range. Instead it should delete EventSchedules of those events
only that belong to that particular conference only.
2.If we set invalid start or end hour attribute of a conference then
also EventSchedules gets deleted even though conference is not
successfully updated.
Fixed both the issues and added test for the same.
This commit is contained in:
Siddhant Bajaj 2017-03-06 21:45:48 +05:30
parent ec71b111fe
commit 7ce816e146
3 changed files with 34 additions and 10 deletions

View file

@ -82,7 +82,6 @@ module Admin
short_title = @conference.short_title
@conference.assign_attributes(conference_params)
send_mail_on_conf_update = @conference.notify_on_dates_changed?
delete_event_schedules if @conference.start_hour_changed? || @conference.end_hour_changed?
if @conference.update_attributes(conference_params)
ConferenceDateUpdateMailJob.perform_later(@conference) if send_mail_on_conf_update
@ -189,14 +188,5 @@ module Admin
:targets, :targets_attributes,
:campaigns, :campaigns_attributes, :registration_limit)
end
def delete_event_schedules
event_schedules = EventSchedule.select do |e|
e.start_time.strftime('%H').to_i < @conference.start_hour ||
e.end_time.strftime('%H').to_i > @conference.end_hour ||
(e.end_time.strftime('%H').to_i == @conference.end_hour && e.end_time.strftime('%M').to_i > 0)
end
event_schedules.each(&:destroy)
end
end
end

View file

@ -67,6 +67,7 @@ class Conference < ActiveRecord::Base
before_create :create_email_settings
after_create :create_free_ticket
after_update :delete_event_schedules
##
# Checks if the user is registered to the conference
@ -80,6 +81,20 @@ class Conference < ActiveRecord::Base
user.present? && registrations.where(user_id: user.id).count > 0
end
##
# Delete all EventSchedules that are not in the hours range
# After the conference has been successfully updated
def delete_event_schedules
if start_hour_changed? || end_hour_changed?
event_schedules = program.event_schedules.select do |event_schedule|
event_schedule.start_time.hour < start_hour ||
event_schedule.end_time.hour > end_hour ||
(event_schedule.end_time.hour == end_hour && event_schedule.end_time.minute > 0)
end
event_schedules.each(&:destroy)
end
end
##
# Checks if the registration for the conference is currently open
#

View file

@ -1639,4 +1639,23 @@ describe Conference do
expect(free_ticket.price_cents).to eq(0)
end
end
describe 'after_update' do
let(:conference) { create(:conference) }
let(:scheduled_event_before_conference) { create(:event_scheduled, program: conference.program, hour: conference.start_date + conference.start_hour.hours) }
let(:scheduled_event_after_conference) { create(:event_scheduled, program: conference.program, hour: conference.start_date + conference.end_hour.hours - 1.hour) }
let!(:scheduled_event_during_conference) { create(:event_scheduled, program: conference.program, hour: conference.start_date + conference.start_hour.hours + 3.hours) }
it 'delete event schedules that are not in hour ranges, when conference start hour is updated' do
scheduled_event_before_conference
conference.start_hour = conference.start_hour + 1
expect{ conference.save }.to change{ EventSchedule.count }.from(2).to(1)
end
it 'delete event schedules that are not in hour ranges, when conference end hour is updated' do
scheduled_event_after_conference
conference.end_hour = conference.end_hour - 2
expect{ conference.save }.to change{ EventSchedule.count }.from(2).to(1)
end
end
end