Add pagination to alerts index
This commit is contained in:
@@ -28,7 +28,8 @@ bin/rspec
|
||||
## TODO
|
||||
|
||||
- What additional cols to add to parks model?
|
||||
- Order results on parks#index
|
||||
- Order results on index actions
|
||||
- Return total pages for index actions
|
||||
- index states for efficient search?
|
||||
- full text search in sqlite?
|
||||
- normalize state on a separate table?
|
||||
|
||||
13
app/controllers/api/v1/base_controller.rb
Normal file
13
app/controllers/api/v1/base_controller.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class Api::V1::BaseController < ApplicationController
|
||||
DEFAULT_PAGE_SIZE = 10
|
||||
|
||||
private
|
||||
|
||||
def per_page
|
||||
(params[:per_page].presence || DEFAULT_PAGE_SIZE).to_i
|
||||
end
|
||||
|
||||
def page
|
||||
(params[:page].presence || 1).to_i
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,7 @@ module Api::V1::Parks
|
||||
before_action :set_park
|
||||
|
||||
def index
|
||||
render json: @park.alerts
|
||||
render json: @park.alerts.limit(per_page).offset((page - 1) * per_page)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
15
spec/fixtures/alerts.yml
vendored
Normal file
15
spec/fixtures/alerts.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
park_code: crla
|
||||
title: Fire Restrictions in Effect
|
||||
description: To ensure public safety and to provide the highest degree of protection to park resources, fire restrictions are in effect until further notice.
|
||||
category: caution
|
||||
indexed_date: 2025-07-04 22:07:59.0
|
||||
|
||||
two:
|
||||
park_code: olym
|
||||
title: Roads May Be Icy - Please Use Caution!
|
||||
description: Even though the sun is shining, the open roads in the park may still have icy patches, especially in the mornings and evenings when the temperatures drop. Please drive with caution.
|
||||
category: caution
|
||||
indexed_date: 2025-04-30 23:40:30.0
|
||||
15
spec/models/park_spec.rb
Normal file
15
spec/models/park_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Park, type: :model do
|
||||
describe "#alerts" do
|
||||
it "has alerts" do
|
||||
expect(parks(:one).alerts).to eq([alerts(:one)])
|
||||
end
|
||||
|
||||
it "destroys dependent alerts" do
|
||||
expect do
|
||||
parks(:one).destroy!
|
||||
end.to change(Alert, :count).by(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user