Add pagination to numerous API endpoints
parent
c0fab4a38c
commit
de140d02a5
2
Gemfile
2
Gemfile
|
@ -24,7 +24,7 @@ gem "scenic"
|
||||||
gem "secure_headers"
|
gem "secure_headers"
|
||||||
gem "tzinfo" # For validation of user selected timezone names
|
gem "tzinfo" # For validation of user selected timezone names
|
||||||
gem "valid_url"
|
gem "valid_url"
|
||||||
# gem "farady", "~> 1.0.0"
|
gem "kaminari"
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
gem "climate_control"
|
gem "climate_control"
|
||||||
|
|
13
Gemfile.lock
13
Gemfile.lock
|
@ -153,6 +153,18 @@ GEM
|
||||||
json (2.3.0)
|
json (2.3.0)
|
||||||
jsonapi-renderer (0.2.2)
|
jsonapi-renderer (0.2.2)
|
||||||
jwt (2.2.1)
|
jwt (2.2.1)
|
||||||
|
kaminari (1.2.0)
|
||||||
|
activesupport (>= 4.1.0)
|
||||||
|
kaminari-actionview (= 1.2.0)
|
||||||
|
kaminari-activerecord (= 1.2.0)
|
||||||
|
kaminari-core (= 1.2.0)
|
||||||
|
kaminari-actionview (1.2.0)
|
||||||
|
actionview
|
||||||
|
kaminari-core (= 1.2.0)
|
||||||
|
kaminari-activerecord (1.2.0)
|
||||||
|
activerecord
|
||||||
|
kaminari-core (= 1.2.0)
|
||||||
|
kaminari-core (1.2.0)
|
||||||
loofah (2.4.0)
|
loofah (2.4.0)
|
||||||
crass (~> 1.0.2)
|
crass (~> 1.0.2)
|
||||||
nokogiri (>= 1.5.9)
|
nokogiri (>= 1.5.9)
|
||||||
|
@ -320,6 +332,7 @@ DEPENDENCIES
|
||||||
google-cloud-storage (~> 1.11)
|
google-cloud-storage (~> 1.11)
|
||||||
hashdiff
|
hashdiff
|
||||||
jwt
|
jwt
|
||||||
|
kaminari
|
||||||
mutations
|
mutations
|
||||||
passenger
|
passenger
|
||||||
pg
|
pg
|
||||||
|
|
|
@ -80,6 +80,16 @@ module Api
|
||||||
{ root: false, user: current_user }
|
{ root: false, user: current_user }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def maybe_paginate(collection)
|
||||||
|
page = params[:page]
|
||||||
|
per = params[:per]
|
||||||
|
|
||||||
|
if page && per
|
||||||
|
render json: collection.page(page).per(per)
|
||||||
|
else
|
||||||
|
render json: collection
|
||||||
|
end
|
||||||
|
end
|
||||||
private
|
private
|
||||||
|
|
||||||
def clean_expired_farm_events
|
def clean_expired_farm_events
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class AlertsController < Api::AbstractController
|
class AlertsController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: current_device.alerts
|
maybe_paginate current_device.alerts
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Api
|
||||||
before_action :clean_expired_farm_events, only: [:index]
|
before_action :clean_expired_farm_events, only: [:index]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: current_device.farm_events
|
maybe_paginate current_device.farm_events
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: farmware_envs
|
maybe_paginate farmware_envs
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class FarmwareInstallationsController < Api::AbstractController
|
class FarmwareInstallationsController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: farmware_installations
|
maybe_paginate farmware_installations
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class PeripheralsController < Api::AbstractController
|
class PeripheralsController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: current_device.peripherals
|
maybe_paginate current_device.peripherals
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class PinBindingsController < Api::AbstractController
|
class PinBindingsController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: pin_bindings
|
maybe_paginate pin_bindings
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class PlantTemplatesController < Api::AbstractController
|
class PlantTemplatesController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: current_device.plant_templates
|
maybe_paginate current_device.plant_templates
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Api
|
||||||
before_action :clean_expired_farm_events, only: [:destroy]
|
before_action :clean_expired_farm_events, only: [:destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: your_point_groups
|
maybe_paginate your_point_groups
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -20,7 +20,7 @@ module Api
|
||||||
.where("discarded_at < ?", Time.now - HARD_DELETE_AFTER)
|
.where("discarded_at < ?", Time.now - HARD_DELETE_AFTER)
|
||||||
.destroy_all
|
.destroy_all
|
||||||
|
|
||||||
render json: points(params.fetch(:filter) { "kept" })
|
maybe_paginate points(params.fetch(:filter) { "kept" })
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Api
|
||||||
before_action :clean_expired_farm_events, only: [:destroy]
|
before_action :clean_expired_farm_events, only: [:destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: your_regimens
|
maybe_paginate your_regimens
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class SavedGardensController < Api::AbstractController
|
class SavedGardensController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: current_device.saved_gardens
|
maybe_paginate current_device.saved_gardens
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: readings
|
maybe_paginate(readings)
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Api
|
module Api
|
||||||
class SensorsController < Api::AbstractController
|
class SensorsController < Api::AbstractController
|
||||||
def index
|
def index
|
||||||
render json: current_device.sensors
|
maybe_paginate current_device.sensors
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Api
|
||||||
class ToolsController < Api::AbstractController
|
class ToolsController < Api::AbstractController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: tools
|
maybe_paginate tools
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: webcams
|
maybe_paginate webcams
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -82,6 +82,14 @@ describe Api::SensorReadingsController do
|
||||||
expect(keys).to include(:x, :y, :z, :value, :pin)
|
expect(keys).to include(:x, :y, :z, :value, :pin)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "paginates sensor readings" do
|
||||||
|
sign_in user
|
||||||
|
SensorReading.destroy_all
|
||||||
|
FactoryBot.create_list(:sensor_reading, 30, device: user.device)
|
||||||
|
get :index, params: { format: :json, page: 2, per: 5 }
|
||||||
|
expect(json.length).to eq(5)
|
||||||
|
end
|
||||||
|
|
||||||
it "destroys a reading" do
|
it "destroys a reading" do
|
||||||
sign_in user
|
sign_in user
|
||||||
SensorReading.destroy_all
|
SensorReading.destroy_all
|
||||||
|
|
Loading…
Reference in New Issue