Skip to content

Commit

Permalink
Ft pagination (#873)
Browse files Browse the repository at this point in the history
* added pagination to sites page

* changed no of sites to 50 per page

* added tests for pagination

* change name of test

* Commit changes made by code formatters

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
laurentb4 and github-actions[bot] authored Feb 3, 2025
1 parent 4c30958 commit 883affd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REGISTRY_URL=068084030754.dkr.ecr.eu-west-2.amazonaws.com
endif

UID=$(shell id -u)
DOCKER_COMPOSE = env ENV=${ENV} UID=$(UID) docker-compose -f docker-compose.yml
DOCKER_COMPOSE = env ENV=${ENV} UID=$(UID) docker compose -f docker-compose.yml
BUNDLE_FLAGS=

DOCKER_BUILD_CMD = BUNDLE_INSTALL_FLAGS="$(BUNDLE_FLAGS)" $(DOCKER_COMPOSE) build
Expand All @@ -33,7 +33,7 @@ shell-dev: ## Run application and start shell
.PHONY: start-db
start-db: ## start database
$(DOCKER_COMPOSE) up -d admin-db
ENV=${ENV} ./scripts/wait_for_db.sh
#ENV=${ENV} ./scripts/wait_for_db.sh

.PHONY: db-setup
db-setup: ## setup database
Expand Down
21 changes: 11 additions & 10 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ class SitesController < ApplicationController
before_action :set_site, only: [:show, :edit, :update, :destroy]

def index
@sites = Site.order(:fits_id).all
@sites = Site.order(:fits_id).page(params[:page]).per(50)
@navigation_crumbs = [["Home", root_path]]
@sites = if params[:query].present?
Site.where('name LIKE ? OR fits_id LIKE ? OR id IN (
SELECT sn.site_id
FROM subnets s
INNER JOIN shared_networks sn ON s.shared_network_id = sn.id
WHERE s.cidr_block LIKE ?
)', "%#{params[:query]}%", "%#{params[:query]}%", "%#{params[:query]}%")
else
Site.order(:fits_id).all
end
filtered_sites = Site.where('name LIKE ? OR fits_id LIKE ? OR id IN (
SELECT sn.site_id
FROM subnets s
INNER JOIN shared_networks sn ON s.shared_network_id = sn.id
WHERE s.cidr_block LIKE ?
)', "%#{params[:query]}%", "%#{params[:query]}%", "%#{params[:query]}%")
filtered_sites.page(params[:page]).per(10)
else
Site.order(:fits_id).page(params[:page]).per(50)
end
end

def show
Expand Down
2 changes: 2 additions & 0 deletions app/views/sites/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@
</tbody>
</table>
</div>

<%= paginate @sites %>
30 changes: 30 additions & 0 deletions spec/controllers/sites_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@
end
end

describe "site pagination" do
before do
sign_in create(:user, :viewer)
100.times do |i|
create :site, fits_id: "FITS#{i + 1}"
end
end

let(:per_page) { 50 }

it "displays 50 sites per page" do
first_page_sites = Site.page(1).per(per_page)

expect(first_page_sites.count).to eq(50)
expect(first_page_sites.first.fits_id).to eq("FITS1")
expect(first_page_sites.last.fits_id).to eq("FITS50")
end

it "handles pagination correctly for multiple pages" do
first_page = Site.page(1).per(per_page)
second_page = Site.page(2).per(per_page)

expect(first_page.count).to eq(50)
expect(second_page.count).to eq(50)

expect(first_page.last.fits_id).to eq("FITS50")
expect(second_page.first.fits_id).to eq("FITS51")
end
end

describe "GET show" do
before do
sign_in create(:user, :viewer)
Expand Down

0 comments on commit 883affd

Please sign in to comment.