Skip to content

Commit f8f8e7e

Browse files
fluxsaasmamhoff
authored andcommitted
add failing specs to test for ransack scopes
1 parent edfb676 commit f8f8e7e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spec/dummy.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131

3232
class User < ActiveRecord::Base
3333
has_many :notes
34+
35+
scope :created_before, ->(date) { where("created_at < ?", date) }
36+
37+
def self.ransackable_scopes(_auth_object = nil)
38+
[:created_before]
39+
end
3440
end
3541

3642
class Note < ActiveRecord::Base
@@ -83,7 +89,7 @@ class UsersController < ActionController::Base
8389
def index
8490
allowed_fields = [
8591
:first_name, :last_name, :created_at,
86-
:notes_created_at, :notes_quantity
92+
:notes_created_at, :notes_quantity, :created_before
8793
]
8894
options = { sort_with_expressions: true }
8995

spec/filtering_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,29 @@
9696
expect(response_json['data'][0]).to have_id(second_user.id.to_s)
9797
end
9898
end
99+
100+
context 'returns users filtered by scope' do
101+
let(:params) do
102+
third_user.update(created_at: '2013-01-01')
103+
104+
{
105+
filter: { created_before: '2013-02-01' }
106+
}
107+
end
108+
109+
fit 'ensures ransack scopes are working properly' do
110+
ransack = User.ransack({ created_before: '2013-02-01' })
111+
expected_sql = 'SELECT "users".* FROM "users" WHERE '\
112+
'(created_at < \'2013-02-01\')'
113+
expect(ransack.result.to_sql).to eq(expected_sql)
114+
end
115+
116+
fit 'should return only' do
117+
expect(response).to have_http_status(:ok)
118+
expect(response_json['data'].size).to eq(1)
119+
expect(response_json['data'][0]).to have_id(third_user.id.to_s)
120+
end
121+
end
99122
end
100123
end
101124
end

0 commit comments

Comments
 (0)