diff --git a/lib/jsonapi/scopes/filters.rb b/lib/jsonapi/scopes/filters.rb index 028eea4..15e8edb 100644 --- a/lib/jsonapi/scopes/filters.rb +++ b/lib/jsonapi/scopes/filters.rb @@ -19,7 +19,7 @@ def apply_filter(params) filtering_params = params.dig(:filter) || {} filtering_params.each do |key, value| - value = value.to_s.split(',').reject(&:blank?) if value.include?(',') + value = value.to_s.split(',').reject(&:blank?) if value&.include?(',') raise InvalidAttributeError, "#{key} is not valid as filter attribute." unless @filters.include?(key.to_sym) 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