Skip to content

Commit

Permalink
Update UpdateTimeslicesCourseDate class to support drastic changes in…
Browse files Browse the repository at this point in the history
… course dates. Now when the previous course period and the current one have no overlap, delete the existing timeslices and generate new ones. Add basic specs for it. (#6185)
  • Loading branch information
gabina authored Feb 7, 2025
1 parent d8af364 commit 42a3448
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/services/update_timeslices_course_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ def initialize(course)
end

def run
update_timeslices_if_drastic_change
update_timeslices_if_start_date_changed
update_timeslices_if_end_date_changed
end

private

# A 'drastic' change occurs when the previous course period and the current one have no overlap.
# In this case, we delete the existing timeslices and generate new ones.
def update_timeslices_if_drastic_change
min_course_start = CourseWikiTimeslice.where(course: @course).minimum(:start)
max_course_end = CourseWikiTimeslice.where(course: @course).maximum(:end)

drastic_change = @course.start >= max_course_end || @course.end <= min_course_start

return unless drastic_change

remove_timeslices_prior_to_start_date
remove_timeslices_after_end_date
@timeslice_manager.create_timeslices_for_new_course_wiki_records(@course.wikis)
end

def update_timeslices_if_start_date_changed
# Get the min course wiki timeslice end date
min_course_end = CourseWikiTimeslice.where(course: @course)
Expand Down
46 changes: 45 additions & 1 deletion spec/services/update_timeslices_course_date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe UpdateTimeslicesCourseDate do
let(:start) { '2021-01-24'.to_datetime }
let(:course) { create(:course, start: '2021-01-24', end: '2021-01-30') }
let(:course) { create(:course, start:, end: '2021-01-30') }
let(:enwiki) { Wiki.get_or_create(language: 'en', project: 'wikipedia') }
let(:updater) { described_class.new(course).run }
let(:user1) { create(:user, username: 'Ragesoss') }
Expand Down Expand Up @@ -127,4 +127,48 @@
expect(course.article_course_timeslices.count).to eq(1)
end
end

context 'when the course was entirely moved backward' do
before do
course.update(start: '2020-02-10')
course.update(end: '2020-02-20')
end

it 'deletes existing timeslices and create new ones' do
# There are two users, two articles and one wiki
expect(course.course_wiki_timeslices.count).to eq(7)
expect(course.course_wiki_timeslices.needs_update.count).to eq(0)
expect(course.course_user_wiki_timeslices.count).to eq(3)
expect(course.article_course_timeslices.count).to eq(2)

described_class.new(course).run
# Timeslices from old period were deleted and new ones were created
expect(course.course_wiki_timeslices.count).to eq(11)
expect(course.course_wiki_timeslices.needs_update.count).to eq(0)
expect(course.course_user_wiki_timeslices.count).to eq(0)
expect(course.article_course_timeslices.count).to eq(0)
end
end

context 'when the course was entirely moved forward' do
before do
course.update(start: '2022-02-10')
course.update(end: '2022-02-20')
end

it 'deletes existing timeslices and create new ones' do
# There are two users, two articles and one wiki
expect(course.course_wiki_timeslices.count).to eq(7)
expect(course.course_wiki_timeslices.needs_update.count).to eq(0)
expect(course.course_user_wiki_timeslices.count).to eq(3)
expect(course.article_course_timeslices.count).to eq(2)

described_class.new(course).run
# Timeslices from old period were deleted and new ones were created
expect(course.course_wiki_timeslices.count).to eq(11)
expect(course.course_wiki_timeslices.needs_update.count).to eq(0)
expect(course.course_user_wiki_timeslices.count).to eq(0)
expect(course.article_course_timeslices.count).to eq(0)
end
end
end

0 comments on commit 42a3448

Please sign in to comment.