Add pagination
This commit is contained in:
@@ -1,5 +1,20 @@
|
||||
class Api::V1::ParksController < ApplicationController
|
||||
DEFAULT_PAGE_SIZE = 10
|
||||
|
||||
def index
|
||||
render json: Park.all
|
||||
parks = Park
|
||||
.limit(per_page)
|
||||
.offset((page - 1) * per_page)
|
||||
render json: parks
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def per_page
|
||||
(params[:per_page].presence || DEFAULT_PAGE_SIZE).to_i
|
||||
end
|
||||
|
||||
def page
|
||||
(params[:page].presence || 1).to_i
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe "Api::V1::Parks", type: :request do
|
||||
describe "GET /index" do
|
||||
it "returns some parks" do
|
||||
it "returns parks" do
|
||||
get api_v1_parks_url
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.parsed_body).to include(
|
||||
@@ -16,5 +16,17 @@ RSpec.describe "Api::V1::Parks", type: :request do
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
context "with pagination" do
|
||||
it "respects page size param" do
|
||||
get api_v1_parks_url, params: { per_page: 1 }
|
||||
expect(response.parsed_body.size).to eq(1)
|
||||
end
|
||||
|
||||
it "respects page param" do
|
||||
get api_v1_parks_url, params: { per_page: 1, page: 2 }
|
||||
expect(response.parsed_body.first["code"]).to eq("crla")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user