Skip to content

Commit bb16143

Browse files
committed
Somewhat naive rails 6 compatible pagination
1 parent 99e91ed commit bb16143

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

Gemfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ when 'mongoid' then
77
gem 'mongoid-scroll'
88
gem 'mongoid-shell'
99
when 'activerecord' then
10-
gem 'activerecord', '~> 5.0.0'
11-
gem 'otr-activerecord', '~> 1.2.1'
12-
gem 'cursor_pagination' # rubocop:disable Bundler/OrderedGems
10+
gem 'activerecord'
11+
gem 'otr-activerecord'
1312
gem 'pg'
1413
when nil
1514
warn "Missing ENV['DATABASE_ADAPTER']."

lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class TeamsEndpoint < Grape::API
8484
if team
8585
team.ping_if_active!
8686

87-
team.update_attributes!(
87+
team.update!(
8888
oauth_version: oauth_version,
8989
oauth_scope: oauth_scope,
9090
activated_user_id: user_id,

lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ def paginate_by_cursor(coll, options)
3131
results = { results: [], next: nil }
3232
size = (params[:size] || 10).to_i
3333
results[:total_count] = coll.count(:all) if params[:total_count]
34-
coll = coll.offset(params[:offset].to_i) if params.key?(:offset)
34+
limited_coll = coll
35+
limited_coll = coll.offset(params[:offset].to_i) if params.key?(:offset)
3536
sort_options = {}
3637
sort_order(options).each do |order|
37-
sort_options[order[:column]] = { reverse: true } if order[:direction] == :desc
38+
sort_options[order[:column]] = order[:direction]
39+
end
40+
limited_coll = limited_coll.where(sort_options['id'] == :desc ? 'id <= ?' : 'id >= ?', params[:cursor]) if params[:cursor]
41+
limited_coll = limited_coll.order(sort_options).limit(size)
42+
results[:results] = limited_coll.to_a
43+
if sort_options['id'] == :desc
44+
results[:next] = limited_coll.last.id - 1 unless coll.where('id <= ?', limited_coll.last.id - 1).length.zero?
45+
else
46+
results[:next] = limited_coll.last.id + 1 unless coll.where('id >= ?', limited_coll.last.id + 1).length.zero?
3847
end
39-
coll = coll.cursor(params[:cursor], columns: sort_options).per(size)
40-
results[:results] = coll.to_a
41-
results[:next] = coll.next_cursor.to_s unless coll.last_page?
4248
results
4349
end
4450
end

lib/slack-ruby-bot-server/models/team/methods.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ module Methods
1313
validates_presence_of :team_id
1414

1515
def deactivate!
16-
update_attributes!(active: false)
16+
update!(active: false)
1717
end
1818

1919
def activate!(token)
20-
update_attributes!(active: true, token: token)
20+
update!(active: true, token: token)
2121
end
2222

2323
def to_s

0 commit comments

Comments
 (0)