File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3131
3232class User < ActiveRecord ::Base
3333 has_many :notes
34+ scope :created_before , -> ( date ) { where ( 'created_at < ?' , date ) }
35+
36+ def self . ransackable_scopes ( auth_object = nil )
37+ %i( created_before )
38+ end
3439end
3540
3641class Note < ActiveRecord ::Base
@@ -83,7 +88,8 @@ class UsersController < ActionController::Base
8388 def index
8489 allowed_fields = [
8590 :first_name , :last_name , :created_at ,
86- :notes_created_at , :notes_quantity
91+ :notes_created_at , :notes_quantity ,
92+ :created_before
8793 ]
8894 options = { sort_with_expressions : true }
8995
Original file line number Diff line number Diff line change 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+ binding . pry
118+ expect ( response ) . to have_http_status ( :ok )
119+ expect ( response_json [ 'data' ] . size ) . to eq ( 1 )
120+ expect ( response_json [ 'data' ] [ 0 ] ) . to have_id ( third_user . id . to_s )
121+ end
122+ end
99123 end
100124 end
101125end
You can’t perform that action at this time.
0 commit comments