File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 31
31
32
32
class User < ActiveRecord ::Base
33
33
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
34
40
end
35
41
36
42
class Note < ActiveRecord ::Base
@@ -83,7 +89,7 @@ class UsersController < ActionController::Base
83
89
def index
84
90
allowed_fields = [
85
91
:first_name , :last_name , :created_at ,
86
- :notes_created_at , :notes_quantity
92
+ :notes_created_at , :notes_quantity , :created_before
87
93
]
88
94
options = { sort_with_expressions : true }
89
95
Original file line number Diff line number Diff line change 96
96
expect ( response_json [ 'data' ] [ 0 ] ) . to have_id ( second_user . id . to_s )
97
97
end
98
98
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
99
122
end
100
123
end
101
124
end
You can’t perform that action at this time.
0 commit comments