Skip to content

Commit

Permalink
Add pagination option for all users and RSpec tests for cooresponding…
Browse files Browse the repository at this point in the history
… Rspec Tests

Added a fourth option to display all users on a single page. Modified the per_page parameter to dynamically set the number of users per page based on user input or default values. Added tests for all pagination options to verify the functionality and behavior of the paginate_list method.
  • Loading branch information
kevindai2002 authored Oct 29, 2024
1 parent 9454b84 commit 5b0d2ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def role

# For filtering the users list with proper search and pagination.
def paginate_list
paginate_options = { '1' => 25, '2' => 50, '3' => 100 }
paginate_options = { '1' => 25, '2' => 50, '3' => 100, '4' => User.count}

# If the above hash does not have a value for the key,
# it means that we need to show all the users on the page
Expand All @@ -259,7 +259,11 @@ def paginate_list

# The type of condition for the search depends on what the user has selected from the search_by dropdown
@search_by = params[:search_by]
@per_page = 3

# Sets the number of users to display per page based on the 'per_page' parameter from the request.
# If no 'per_page' parameter is provided, it defaults to '4', which corresponds to displaying all users on one page.
@per_page = params[:per_page] || '4'

# search for corresponding users
# users = User.search_users(role, user_id, letter, @search_by)

Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,31 @@
expect(response).to redirect_to('/tree_display/drill')
end
end

context '#paginate_list' do
before do
# Seed the database with a number of users for pagination tests
FactoryBot.create_list(:test_user, 110)
end

it 'displays 25 users per page when per_page is 25' do
get :list, params: { per_page: '1' }
expect(assigns(:paginated_users).length).to eq(25)
end

it 'displays 50 users per page when per_page is 50' do
get :list, params: { per_page: '2' }
expect(assigns(:paginated_users).length).to eq(50)
end

it 'displays 100 users per page when per_page is 100' do
get :list, params: { per_page: '3' }
expect(assigns(:paginated_users).length).to eq(100)
end

it 'displays all users on a single page when per_page is "all"' do
get :list, params: { per_page: '4' }
expect(assigns(:paginated_users).length).to eq(110)
end
end
end
4 changes: 2 additions & 2 deletions spec/factories/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@
end

factory :test_user, class: User do
username 'username'
sequence(:username) { |n| "username#{n}" }
name 'full name'
email 'abc@mailinator.com'
sequence(:email) { |n| "user#{n}@mailinator.com" }
end

factory :survey_deployment, class: SurveyDeployment do
Expand Down

0 comments on commit 5b0d2ec

Please sign in to comment.