From 1afd676b9be5b8c09f02b207e79e5a37f1225355 Mon Sep 17 00:00:00 2001 From: Claudio Maradonna Date: Mon, 20 Mar 2023 15:43:09 +0100 Subject: [PATCH 1/4] 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 From 6d9d1590ac131f9a1e6eeb5345b428d8e5bd027b Mon Sep 17 00:00:00 2001 From: Claudio Maradonna Date: Tue, 6 Feb 2024 13:02:00 +0100 Subject: [PATCH 2/4] convert back to string when the value is an Array; this actually fixes a lot of problems with Elasticsearch filters for example --- lib/jsonapi/scopes/filters.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/jsonapi/scopes/filters.rb b/lib/jsonapi/scopes/filters.rb index 028eea4..c722e35 100644 --- a/lib/jsonapi/scopes/filters.rb +++ b/lib/jsonapi/scopes/filters.rb @@ -20,6 +20,7 @@ def apply_filter(params) filtering_params.each do |key, value| value = value.to_s.split(',').reject(&:blank?) if value.include?(',') + value = value.join if value.is_a? Array raise InvalidAttributeError, "#{key} is not valid as filter attribute." unless @filters.include?(key.to_sym) From f2ae8e26c7886a6a0c49f39b417311392f016e21 Mon Sep 17 00:00:00 2001 From: Claudio Maradonna Date: Tue, 5 Mar 2024 10:37:18 +0100 Subject: [PATCH 3/4] remove join in filters that breaks date filters --- lib/jsonapi/scopes/filters.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/jsonapi/scopes/filters.rb b/lib/jsonapi/scopes/filters.rb index c722e35..028eea4 100644 --- a/lib/jsonapi/scopes/filters.rb +++ b/lib/jsonapi/scopes/filters.rb @@ -20,7 +20,6 @@ def apply_filter(params) filtering_params.each do |key, value| value = value.to_s.split(',').reject(&:blank?) if value.include?(',') - value = value.join if value.is_a? Array raise InvalidAttributeError, "#{key} is not valid as filter attribute." unless @filters.include?(key.to_sym) From e2047ec0be634b62e1117ea0eae7a8efeba2502d Mon Sep 17 00:00:00 2001 From: Claudio Maradonna Date: Thu, 11 Jul 2024 10:36:25 +0200 Subject: [PATCH 4/4] fix(filters): add safe operator for splitting values --- lib/jsonapi/scopes/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)