Browse Source

Refactor filtered_users into module

pull/600/head
baarkerlounger 3 years ago
parent
commit
68162cc038
  1. 10
      app/controllers/modules/users_filter.rb
  2. 11
      app/controllers/organisations_controller.rb
  3. 11
      app/controllers/users_controller.rb

10
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

11
app/controllers/organisations_controller.rb

@ -1,6 +1,7 @@
class OrganisationsController < ApplicationController class OrganisationsController < ApplicationController
include Pagy::Backend include Pagy::Backend
include Modules::CaseLogsFilter include Modules::CaseLogsFilter
include Modules::UsersFilter
before_action :authenticate_user!, except: [:index] before_action :authenticate_user!, except: [:index]
before_action :find_resource, except: [:index] before_action :find_resource, except: [:index]
@ -17,7 +18,7 @@ class OrganisationsController < ApplicationController
end end
def users def users
@pagy, @users = pagy(filtered_users) @pagy, @users = pagy(filtered_users(@organisation.users))
render "users/index" render "users/index"
end end
@ -58,14 +59,6 @@ class OrganisationsController < ApplicationController
private 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 def org_params
params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone) params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone)
end end

11
app/controllers/users_controller.rb

@ -2,6 +2,7 @@ class UsersController < ApplicationController
include Pagy::Backend include Pagy::Backend
include Devise::Controllers::SignInOut include Devise::Controllers::SignInOut
include Helpers::Email include Helpers::Email
include Modules::UsersFilter
before_action :authenticate_user! before_action :authenticate_user!
before_action :find_resource, except: %i[new create] before_action :find_resource, except: %i[new create]
before_action :authenticate_scope!, except: %i[new] before_action :authenticate_scope!, except: %i[new]
@ -9,7 +10,7 @@ class UsersController < ApplicationController
def index def index
redirect_to users_organisation_path(current_user.organisation) unless current_user.support? 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| respond_to do |format|
format.html format.html
@ -77,14 +78,6 @@ class UsersController < ApplicationController
private 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 def format_error_messages
errors = @user.errors.to_hash errors = @user.errors.to_hash
@user.errors.clear @user.errors.clear

Loading…
Cancel
Save