Skip to content

Commit d6906ec

Browse files
committed
fix failing tests to filter by _count attributes and other
1 parent debfec8 commit d6906ec

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

lib/jsonapi/filtering.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ module Filtering
88
#
99
# @param requested_field [String] the field to parse
1010
# @return [Array] with the fields and the predicate
11-
def self.extract_attributes_and_predicates(requested_field)
11+
def self.extract_attributes_and_predicates(requested_field, allowed_fields)
1212
predicates = []
1313
field_name = requested_field.to_s.dup
1414

1515
while Ransack::Predicate.detect_from_string(field_name).present? do
16+
# break if we have an exect match with an allowed_fields
17+
# we do not want to pick apart the string further
18+
break if allowed_fields.include?(field_name)
19+
1620
predicate = Ransack::Predicate
1721
.detect_and_strip_from_string!(field_name)
1822
predicates << Ransack::Predicate.named(predicate)
1923
end
20-
2124
[field_name.split(/_and_|_or_/), predicates.reverse]
2225
end
2326

@@ -54,7 +57,7 @@ def jsonapi_filter_params(allowed_fields)
5457

5558
requested.each_pair do |requested_field, to_filter|
5659
field_names, predicates = JSONAPI::Filtering
57-
.extract_attributes_and_predicates(requested_field)
60+
.extract_attributes_and_predicates(requested_field, allowed_fields)
5861

5962
wants_array = predicates.any? && predicates.map(&:wants_array).any?
6063

@@ -88,7 +91,7 @@ def jsonapi_sort_params(allowed_fields, options = {})
8891
end
8992

9093
field_names, predicates = JSONAPI::Filtering
91-
.extract_attributes_and_predicates(requested_field)
94+
.extract_attributes_and_predicates(requested_field, allowed_fields)
9295

9396
next unless (field_names - allowed_fields).empty?
9497
next if !options[:sort_with_expressions] && predicates.any?

spec/filtering_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
describe '#extract_attributes_and_predicate' do
55
context 'mixed attributes (and/or)' do
66
it 'extracts ANDs' do
7-
attributes, predicates = JSONAPI::Filtering
8-
.extract_attributes_and_predicates('attr1_and_attr2_eq')
7+
attributes, predicates =
8+
JSONAPI::Filtering.extract_attributes_and_predicates(
9+
'attr1_and_attr2_eq',
10+
['attr1', 'attr2']
11+
)
912
expect(attributes).to eq(['attr1', 'attr2'])
1013
expect(predicates.size).to eq(1)
1114
expect(predicates[0].name).to eq('eq')
@@ -15,7 +18,7 @@
1518
context 'handle attributes with underscore in name' do
1619
it 'detect _' do
1720
attributes, predicates = JSONAPI::Filtering
18-
.extract_attributes_and_predicates('notes_count_eq')
21+
.extract_attributes_and_predicates('notes_count_eq', ['notes_count'])
1922
expect(attributes).to eq(['notes_count'])
2023
expect(predicates.size).to eq(1)
2124
expect(predicates[0].name).to eq('eq')
@@ -25,7 +28,7 @@
2528
context 'mixed predicates' do
2629
it 'extracts in order' do
2730
attributes, predicates = JSONAPI::Filtering
28-
.extract_attributes_and_predicates('attr1_sum_eq')
31+
.extract_attributes_and_predicates('attr1_sum_eq', ['attr1'])
2932
expect(attributes).to eq(['attr1'])
3033
expect(predicates.size).to eq(2)
3134
expect(predicates[0].name).to eq('sum')

0 commit comments

Comments
 (0)