Skip to content

Commit f919b5d

Browse files
committed
sort courses api results by course name
test plan: - calls to /api/v1/courses should return results sorted by course name (or nickname if the student sets this) closes CNVS-35202 Change-Id: Ia675ec165c0c6b7175d780778a06731c8e6394a4 Reviewed-on: https://gerrit.instructure.com/103094 Tested-by: Jenkins Reviewed-by: Dan Minkevitch <dan@instructure.com> QA-Review: Heath Hales <hhales@instructure.com> Product-Review: Jeremy Stanley <jeremy@instructure.com>
1 parent 4ecc648 commit f919b5d

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

app/controllers/courses_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2755,9 +2755,11 @@ def courses_for_user(user, paginate_url: api_v1_courses_url)
27552755

27562756
hash = []
27572757

2758-
enrollments.sort_by!(&:course_id)
27592758
Canvas::Builders::EnrollmentDateBuilder.preload_state(enrollments)
27602759
enrollments_by_course = enrollments.group_by(&:course_id).values
2760+
enrollments_by_course.sort_by! do |course_enrollments|
2761+
Canvas::ICU.collation_key(course_enrollments.first.course.nickname_for(user))
2762+
end
27612763
enrollments_by_course = Api.paginate(enrollments_by_course, self, paginate_url) if api_request?
27622764
courses = enrollments_by_course.map(&:first).map(&:course)
27632765
preloads = [:account, :root_account]

spec/apis/v1/courses_api_spec.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,14 @@ def method_missing(method_name, *args)
375375
expect(courses.length).to eq 2
376376
end
377377

378-
it "returns course list in id sort order", priority: "2", test_id: 3058053 do
379-
json = api_call(:get, "/api/v1/courses.json",
380-
{controller: 'courses', action: 'index', format: 'json'})
381-
course_ids = json.map{ |c| c["id"]}
382-
expect(course_ids).to eq course_ids.sort
378+
it "returns course list ordered by name (including nicknames)" do
379+
c1 = course_with_student(course_name: 'def', active_all: true).course
380+
c2 = course_with_student(user: @student, course_name: 'abc', active_all: true).course
381+
c3 = course_with_student(user: @student, course_name: 'jkl', active_all: true).course
382+
c4 = course_with_student(user: @student, course_name: 'xyz', active_all: true).course
383+
@student.course_nicknames[c4.id] = 'ghi'; @student.save!
384+
json = api_call(:get, "/api/v1/courses.json", controller: 'courses', action: 'index', format: 'json')
385+
expect(json.map { |course| course['name'] }).to eq %w(abc def ghi jkl)
383386
end
384387

385388
describe "user index" do

0 commit comments

Comments
 (0)