File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 3131
3232class 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
3440end
3541
3642class 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
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+ 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
101124end
You can’t perform that action at this time.
0 commit comments