From 1afd676b9be5b8c09f02b207e79e5a37f1225355 Mon Sep 17 00:00:00 2001 From: Claudio Maradonna Date: Mon, 20 Mar 2023 15:43:09 +0100 Subject: [PATCH] fix #2: now you can sort by scope and attributes mixed; --- lib/jsonapi/scopes/sorts.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/jsonapi/scopes/sorts.rb b/lib/jsonapi/scopes/sorts.rb index ee7a6ce..badfa3c 100644 --- a/lib/jsonapi/scopes/sorts.rb +++ b/lib/jsonapi/scopes/sorts.rb @@ -29,9 +29,20 @@ def apply_sort(params = {}, options = { allowed: [], default: {} }) raise InvalidAttributeError, "#{field} is not valid as sort attribute." unless allowed_fields.include?(field) end - order = ordered_fields.presence || default_order + res = self - self.order(order) + order_to_follow = ordered_fields.presence || default_order + order_to_follow.each do |single_order_q, direction| + if single_order_q.to_s.include? 'scope:' + scope_name = single_order_q.to_s.split(':').last + + res = send(scope_name.to_sym, direction) + else + res = order("#{single_order_q}": direction) + end + end + + res end private