diff --git a/app/controllers/modules/users_filter.rb b/app/controllers/modules/users_filter.rb new file mode 100644 index 000000000..732c08d43 --- /dev/null +++ b/app/controllers/modules/users_filter.rb @@ -0,0 +1,10 @@ +module Modules::UsersFilter + def filtered_users(base_collection) + search_param = params["user-search-field"] + if search_param.present? + base_collection.search_by(search_param) + else + base_collection + end.filter_by_active.includes(:organisation) + end +end diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index d29d894c5..02de8d145 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -1,6 +1,7 @@ class OrganisationsController < ApplicationController include Pagy::Backend include Modules::CaseLogsFilter + include Modules::UsersFilter before_action :authenticate_user!, except: [:index] before_action :find_resource, except: [:index] @@ -17,7 +18,7 @@ class OrganisationsController < ApplicationController end def users - @pagy, @users = pagy(filtered_users) + @pagy, @users = pagy(filtered_users(@organisation.users)) render "users/index" end @@ -58,14 +59,6 @@ class OrganisationsController < ApplicationController private - def filtered_users - if (search_param = params["user-search-field"]) - User.search_by(search_param) - else - User.all - end.filter_by_active.includes(:organisation) - end - def org_params params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 52ded2ebe..0e02f2bf8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,6 +2,7 @@ class UsersController < ApplicationController include Pagy::Backend include Devise::Controllers::SignInOut include Helpers::Email + include Modules::UsersFilter before_action :authenticate_user! before_action :find_resource, except: %i[new create] before_action :authenticate_scope!, except: %i[new] @@ -9,7 +10,7 @@ class UsersController < ApplicationController def index redirect_to users_organisation_path(current_user.organisation) unless current_user.support? - @pagy, @users = pagy(filtered_users) + @pagy, @users = pagy(filtered_users(User.all)) respond_to do |format| format.html @@ -77,14 +78,6 @@ class UsersController < ApplicationController private - def filtered_users - if (search_param = params["user-search-field"]) - User.search_by(search_param) - else - User.all - end.filter_by_active.includes(:organisation) - end - def format_error_messages errors = @user.errors.to_hash @user.errors.clear