Skip to content

Commit

Permalink
Drop pagy
Browse files Browse the repository at this point in the history
  • Loading branch information
miharekar committed Nov 28, 2024
1 parent 9643ebe commit 8889b41
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 261 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ gem "mission_control-jobs"
gem "oj"
gem "omniauth-airtable"
gem "omniauth-rails_csrf_protection"
gem "pagy"
gem "pg"
gem "pghero"
gem "pg_lock"
Expand Down
4 changes: 1 addition & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ GEM
actionpack (>= 4.2)
omniauth (~> 2.0)
ostruct (0.6.1)
pagy (9.3.1)
parallel (1.26.3)
parser (3.3.6.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -370,7 +369,7 @@ GEM
rdoc (6.8.1)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.5.11)
reline (0.5.12)
io-console (~> 0.5)
require-hooks (0.2.2)
responders (3.1.1)
Expand Down Expand Up @@ -529,7 +528,6 @@ DEPENDENCIES
oj
omniauth-airtable
omniauth-rails_csrf_protection
pagy
pg
pg_lock
pghero
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/api/paginatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Api
module Paginatable
def paginate(relation)
limit = params[:items].presence.to_i
limit = 10 if limit.zero?
limit = [limit, 100].min
count = relation.count(:all)
pages = (count.to_f / limit).ceil
page = [params[:page].to_i, 1].max
page = pages if page > pages && pages.positive?

offset = (page - 1) * limit
records = relation.offset(offset).limit(limit)

[records, {count:, page:, limit:, pages:}]
end
end
end
14 changes: 5 additions & 9 deletions app/controllers/api/shots_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
module Api
class ShotsController < Api::BaseController
include Pagy::Backend
include Paginatable

before_action :verify_upload_access, only: %i[upload]
before_action :verify_write_access, only: %i[destroy]

def index
limit = params[:items].presence.to_i
limit = 10 if limit.zero?
limit = 100 if limit.to_i > 100

shots = Current.user.present? ? Current.user.shots : Shot.visible
shots = shots.non_premium unless Current.user&.premium?
shots = shots.by_start_time.select(:id, :start_time, :user_id)

pagy, shots = pagy(shots, limit:)
shots, paging = paginate(shots)
data = shots.map { |s| {clock: s.start_time.to_i, id: s.id} }
render json: {data:, paging: pagy_metadata(pagy)}
rescue Pagy::VariableError
render json: {data:, paging:}
rescue StandardError => e
Appsignal.report_error(e)
render json: {error: "Could not paginate"}, status: :unprocessable_entity
end

Expand Down
248 changes: 0 additions & 248 deletions config/initializers/pagy.rb

This file was deleted.

19 changes: 19 additions & 0 deletions test/controllers/api/shots_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ class ShotsControllerTest < ActionDispatch::IntegrationTest
json_response = response.parsed_body
assert_equal 5, json_response["data"].length
assert_includes json_response, "paging"
assert_equal %w[count page limit pages], json_response["paging"].keys
assert_equal 5, json_response["paging"]["count"]
assert_equal 1, json_response["paging"]["page"]
assert_equal 10, json_response["paging"]["limit"]
assert_equal 1, json_response["paging"]["pages"]

shot_data = json_response["data"].first
assert_equal %w[clock id], shot_data.keys

get api_shots_url(items: 2, page: 3), headers: auth_headers(user), as: :json
assert_response :success

json_response = response.parsed_body
assert_equal 1, json_response["data"].length

assert_equal 5, json_response["paging"]["count"]
assert_equal 3, json_response["paging"]["page"]
assert_equal 2, json_response["paging"]["limit"]
assert_equal 3, json_response["paging"]["pages"]

shot_data = json_response["data"].first
assert_equal %w[clock id], shot_data.keys
Expand Down

0 comments on commit 8889b41

Please sign in to comment.