diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5939cc83438..71aaa9cde72 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 @@ -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) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 4f9b9547adc..893ac25517f 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -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 diff --git a/spec/factories/factories.rb b/spec/factories/factories.rb index 27b7792796b..beb1c2af286 100644 --- a/spec/factories/factories.rb +++ b/spec/factories/factories.rb @@ -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