Skip to content

Commit a45cd39

Browse files
refactor(reference_timelines_controller): remove bulk times creation
1 parent 9e64054 commit a45cd39

File tree

9 files changed

+13
-109
lines changed

9 files changed

+13
-109
lines changed

app/controllers/course/reference_timelines_controller.rb

+4-14
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@ class Course::ReferenceTimelinesController < Course::ComponentController
44

55
def index
66
@timelines = @reference_timelines.includes(:reference_times, :course_users)
7+
8+
# TODO: [PR#5491] Allow timelines management for items other than assessments
79
@items = current_course.lesson_plan_items.
810
where(actable_type: Course::Assessment.name).
911
order(:title).
1012
includes(:reference_times)
1113
end
1214

13-
def show
14-
end
15-
1615
def create
1716
if @reference_timeline.save
18-
render partial: 'reference_timeline', locals: {
19-
reference_timeline: @reference_timeline,
20-
render_times: reference_timeline_params[:reference_times_attributes].present?
21-
}
17+
render partial: 'reference_timeline', locals: { timeline: @reference_timeline }
2218
else
2319
render json: { errors: @reference_timeline.errors.full_messages.to_sentence }, status: :bad_request
2420
end
2521
end
2622

2723
def update
28-
if @reference_timeline.update(reference_timeline_update_params)
24+
if @reference_timeline.update(reference_timeline_params)
2925
head :ok
3026
else
3127
render json: { errors: @reference_timeline.errors.full_messages.to_sentence }, status: :bad_request
@@ -54,12 +50,6 @@ def destroy
5450
private
5551

5652
def reference_timeline_params
57-
params.require(:reference_timeline).permit(:title, :weight, {
58-
reference_times_attributes: Course::ReferenceTimesController.base_params
59-
})
60-
end
61-
62-
def reference_timeline_update_params
6353
params.require(:reference_timeline).permit(:title, :weight)
6454
end
6555

app/models/course/reference_timeline.rb

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class Course::ReferenceTimeline < ApplicationRecord
1717

1818
default_scope { order(:weight) }
1919

20-
accepts_nested_attributes_for :reference_times
21-
2220
def initialize_duplicate(duplicator, _other)
2321
self.course = duplicator.options[:destination_course]
2422
self.reference_times = []
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
# frozen_string_literal: true
2-
json.id reference_timeline.id
3-
json.title reference_timeline.title
4-
json.weight reference_timeline.weight
2+
json.id timeline.id
3+
json.title timeline.title || t('.default_title')
4+
json.timesCount timeline.reference_times.size
55

6-
if render_times
7-
reference_times = reference_timeline.reference_times.includes(:lesson_plan_item)
8-
json.times reference_times do |time|
9-
json.id time.id
10-
json.startAt time.start_at
11-
json.bonusEndAt time.bonus_end_at if time.bonus_end_at.present?
12-
json.endAt time.end_at if time.end_at.present?
13-
14-
item = time.lesson_plan_item
15-
json.itemId item.id
16-
json.actableId item.actable_id
17-
json.type item.actable_type.constantize.model_name.human
18-
json.title item.title
19-
end
20-
end
6+
json.weight timeline.weight if timeline.weight.present?
7+
json.default true if timeline.default?
8+
json.assignees timeline.course_users.size unless timeline.default?

app/views/course/reference_timelines/index.json.jbuilder

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ json.gamified current_course.gamified
33
json.defaultTimeline current_course.default_reference_timeline.id
44

55
json.timelines @timelines do |timeline|
6-
json.id timeline.id
7-
json.title timeline.title || t('.default_title')
8-
json.timesCount timeline.reference_times.size
9-
10-
json.weight timeline.weight if timeline.weight.present?
11-
json.default true if timeline.default?
12-
json.assignees timeline.course_users.size unless timeline.default?
6+
json.partial! 'reference_timeline', timeline: timeline
137
end
148

159
json.items @items do |item|

app/views/course/reference_timelines/show.json.jbuilder

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
en:
22
course:
33
reference_timelines:
4-
index:
4+
reference_timeline:
55
default_title: 'Default Timeline'

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@
444444
post 'toggle_satisfiability_type' => 'learning_map#toggle_satisfiability_type'
445445
end
446446

447-
resources :reference_timelines, path: 'timelines', except: [:new, :edit] do
447+
resources :reference_timelines, path: 'timelines', except: [:new, :edit, :show] do
448448
resources :reference_times, path: 'times', only: [:create, :update, :destroy]
449449
end
450450
end

spec/controllers/course/reference_timelines_controller_spec.rb

-61
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828
end
2929
end
3030

31-
describe '#show' do
32-
subject { get :show, as: :json, params: { course_id: course, id: timeline } }
33-
34-
context 'when the user is a manager of the course' do
35-
let(:user) { create(:course_manager, course: course).user }
36-
37-
it { is_expected.to render_template(:show) }
38-
end
39-
40-
context 'when the user is a student' do
41-
let(:user) { create(:course_student, course: course).user }
42-
43-
it { expect { subject }.to raise_error(CanCan::AccessDenied) }
44-
end
45-
end
46-
4731
describe '#create' do
4832
subject do
4933
post :create, as: :json, params: {
@@ -65,51 +49,6 @@
6549
expect(new_timeline.weight).to eq(2)
6650
end
6751

68-
context 'when an array of times are given' do
69-
let(:item) { create(:course_lesson_plan_item, course: course) }
70-
# Convert Ruby Time to string because of differences in micro/nano-second precision between
71-
# database and in-memory time representations. See https://stackoverflow.com/a/20403290.
72-
let(:start_time) { 1.day.from_now.to_s }
73-
let(:bonus_end_time) { 2.days.from_now.to_s }
74-
let(:end_time) { 3.days.from_now.to_s }
75-
76-
subject do
77-
post :create, as: :json, params: {
78-
course_id: course,
79-
reference_timeline: {
80-
title: title,
81-
reference_times_attributes: [
82-
{
83-
lesson_plan_item_id: item.id,
84-
start_at: start_time,
85-
bonus_end_at: bonus_end_time,
86-
end_at: end_time
87-
}
88-
]
89-
}
90-
}
91-
end
92-
93-
it 'creates the timeline, the given times, and their associations' do
94-
expect { subject }.
95-
to change { course.reference_timelines.size }.by(1).
96-
and change { item.reference_times.size }.by(1)
97-
98-
is_expected.to have_http_status(:ok)
99-
is_expected.to render_template(partial: '_reference_timeline')
100-
101-
new_timeline = assigns(:reference_timeline)
102-
expect(new_timeline.title).to eq(title)
103-
expect(new_timeline.reference_times.size).to eq(1)
104-
105-
new_time = new_timeline.reference_times.first
106-
expect(new_time.lesson_plan_item.id).to eq(item.id)
107-
expect(new_time.start_at).to eq(start_time)
108-
expect(new_time.bonus_end_at).to eq(bonus_end_time)
109-
expect(new_time.end_at).to eq(end_time)
110-
end
111-
end
112-
11352
context 'when cannot be saved' do
11453
before do
11554
allow(timeline).to receive(:save).and_return(false)

spec/models/course/reference_timeline_spec.rb

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
dependent(:restrict_with_error)
1717
}
1818

19-
it { should accept_nested_attributes_for(:reference_times) }
20-
2119
describe '#validations' do
2220
let(:title) { 'Test Timeline Title' }
2321

0 commit comments

Comments
 (0)