Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise unless Ransack ignore_unknown_conditions? #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/jsonapi/filtering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def self.extract_attributes_and_predicates(requested_field)
.detect_and_strip_from_string!(field_name)
predicates << Ransack::Predicate.named(predicate)
end
unless predicates.present? || Ransack.options[:ignore_unknown_conditions]
raise ArgumentError, "No valid predicates for #{requested_field}"
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think this is handled already by Ransack:
https://github.com/stas/jsonapi.rb/blob/master/lib/jsonapi/patches.rb#L6

Just reset the option in your config and it should work, no?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stas: It seems to be ignored, from my testing.


[field_name.split(/_and_|_or_/), predicates.reverse]
end
Expand Down
24 changes: 24 additions & 0 deletions spec/filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@
expect(predicates[1].name).to eq('eq')
end
end

context 'with an invalidate predicate' do
context 'with default configuration' do
it 'does not return any predicates' do
attributes, predicates = JSONAPI::Filtering
.extract_attributes_and_predicates('attr1_eqx')
expect(attributes).to eq(['attr1_eqx'])
expect(predicates.size).to eq(0)
end
end
context 'with ignore_unknown_conditions as false' do
before do
Ransack.configure { |c| c.ignore_unknown_conditions = false }
end
after do
Ransack.configure { |c| c.ignore_unknown_conditions = true }
end
it 'raises an error' do
expect do
JSONAPI::Filtering.extract_attributes_and_predicates('attr1_eqx')
end.to raise_error ArgumentError
end
end
end
end

describe 'GET /users' do
Expand Down