From f8f8e7ea54873c0bbbf86a7c06f98cca9b69f64c Mon Sep 17 00:00:00 2001 From: kalle saas Date: Wed, 10 Mar 2021 07:25:50 +0100 Subject: [PATCH 1/2] add failing specs to test for ransack scopes --- spec/dummy.rb | 8 +++++++- spec/filtering_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/spec/dummy.rb b/spec/dummy.rb index 6f0c434..8b1644e 100644 --- a/spec/dummy.rb +++ b/spec/dummy.rb @@ -31,6 +31,12 @@ class User < ActiveRecord::Base has_many :notes + + scope :created_before, ->(date) { where("created_at < ?", date) } + + def self.ransackable_scopes(_auth_object = nil) + [:created_before] + end end class Note < ActiveRecord::Base @@ -83,7 +89,7 @@ class UsersController < ActionController::Base def index allowed_fields = [ :first_name, :last_name, :created_at, - :notes_created_at, :notes_quantity + :notes_created_at, :notes_quantity, :created_before ] options = { sort_with_expressions: true } diff --git a/spec/filtering_spec.rb b/spec/filtering_spec.rb index f3bd80a..a078de0 100644 --- a/spec/filtering_spec.rb +++ b/spec/filtering_spec.rb @@ -96,6 +96,29 @@ expect(response_json['data'][0]).to have_id(second_user.id.to_s) end end + + context 'returns users filtered by scope' do + let(:params) do + third_user.update(created_at: '2013-01-01') + + { + filter: { created_before: '2013-02-01' } + } + end + + fit 'ensures ransack scopes are working properly' do + ransack = User.ransack({ created_before: '2013-02-01' }) + expected_sql = 'SELECT "users".* FROM "users" WHERE '\ + '(created_at < \'2013-02-01\')' + expect(ransack.result.to_sql).to eq(expected_sql) + end + + fit 'should return only' do + expect(response).to have_http_status(:ok) + expect(response_json['data'].size).to eq(1) + expect(response_json['data'][0]).to have_id(third_user.id.to_s) + end + end end end end From e9dc2e4cbbcd7b1734117b32668197a680e17dd1 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 24 Jun 2021 17:41:46 +0200 Subject: [PATCH 2/2] Do not filter out scopes from Ransack queries Scopes are special in that they don't have a predicate. This removes the check for the predicate, and thus makes scopes as filtering options work with JSONAPI.rb. --- lib/jsonapi/filtering.rb | 2 +- spec/dummy.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jsonapi/filtering.rb b/lib/jsonapi/filtering.rb index 70d47db..d09a619 100644 --- a/lib/jsonapi/filtering.rb +++ b/lib/jsonapi/filtering.rb @@ -62,7 +62,7 @@ def jsonapi_filter_params(allowed_fields) to_filter = to_filter.split(',') end - if predicates.any? && (field_names - allowed_fields).empty? + if (field_names - allowed_fields).empty? filtered[requested_field] = to_filter end end diff --git a/spec/dummy.rb b/spec/dummy.rb index 8b1644e..c8d6680 100644 --- a/spec/dummy.rb +++ b/spec/dummy.rb @@ -32,7 +32,7 @@ class User < ActiveRecord::Base has_many :notes - scope :created_before, ->(date) { where("created_at < ?", date) } + scope :created_before, ->(date) { where('created_at < ?', date) } def self.ransackable_scopes(_auth_object = nil) [:created_before]