diff --git a/README.md b/README.md index 5130baa..48742cc 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 0000000..36fd330 --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -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 diff --git a/app/controllers/api/v1/parks/alerts_controller.rb b/app/controllers/api/v1/parks/alerts_controller.rb index 6bcc267..e564db4 100644 --- a/app/controllers/api/v1/parks/alerts_controller.rb +++ b/app/controllers/api/v1/parks/alerts_controller.rb @@ -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 diff --git a/spec/fixtures/alerts.yml b/spec/fixtures/alerts.yml new file mode 100644 index 0000000..2a60e3d --- /dev/null +++ b/spec/fixtures/alerts.yml @@ -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 diff --git a/spec/models/park_spec.rb b/spec/models/park_spec.rb new file mode 100644 index 0000000..322a646 --- /dev/null +++ b/spec/models/park_spec.rb @@ -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