From fc632d249ddc4ada57c016a2ddcb0ecd3c207e5a Mon Sep 17 00:00:00 2001 From: Oluwatoyosi Oyegoke <34948675+Tooyosi@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:50:02 +0100 Subject: [PATCH] add sort functionality to subjects and sort by id default (#4359) * add sort functionality to subjects and sort by id default * add controller test for id default * hound cleanups * hound cleanups --- app/controllers/api/v1/subjects_controller.rb | 2 ++ app/serializers/subject_serializer.rb | 2 ++ spec/controllers/api/v1/subjects_controller_spec.rb | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/app/controllers/api/v1/subjects_controller.rb b/app/controllers/api/v1/subjects_controller.rb index e14a4fe0f..db8a70789 100644 --- a/app/controllers/api/v1/subjects_controller.rb +++ b/app/controllers/api/v1/subjects_controller.rb @@ -13,6 +13,8 @@ class Api::V1::SubjectsController < Api::ApiController before_action :check_subject_limit, only: :create def index + params[:sort] = 'id' if params[:sort].blank? + case params[:sort] when 'queued' queued diff --git a/app/serializers/subject_serializer.rb b/app/serializers/subject_serializer.rb index 7615397ae..da96fe5df 100644 --- a/app/serializers/subject_serializer.rb +++ b/app/serializers/subject_serializer.rb @@ -9,6 +9,8 @@ class SubjectSerializer preload :locations, :project, :collections, :subject_sets + can_sort_by :id + def locations @model.ordered_locations.map do |loc| { diff --git a/spec/controllers/api/v1/subjects_controller_spec.rb b/spec/controllers/api/v1/subjects_controller_spec.rb index 703d58c39..cf5abfcc5 100644 --- a/spec/controllers/api/v1/subjects_controller_spec.rb +++ b/spec/controllers/api/v1/subjects_controller_spec.rb @@ -49,6 +49,10 @@ get :index end + it 'defaults sort parameter to id' do + expect(controller.params[:sort]).to eq('id') + end + it_behaves_like "an api response" it "should return 200" do @@ -85,6 +89,10 @@ get :index, params: request_params end + it 'does not default sort parameter to id' do + expect(controller.params[:sort]).not_to eq('id') + end + it "should return 200" do expect(response.status).to eq(200) end