Skip to content

Commit

Permalink
fix guillaumebriday#2: now you can sort by scope and attributes mixed;
Browse files Browse the repository at this point in the history
  • Loading branch information
FiloSpaTeam committed Mar 20, 2023
1 parent 973d24e commit 1afd676
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/jsonapi/scopes/sorts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1afd676

Please sign in to comment.