Skip to content

Commit

Permalink
refactor controller
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 committed Apr 29, 2023
1 parent 33b46ce commit de9e1b1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
21 changes: 11 additions & 10 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# TermsController
# UsersController
class UsersController < ApplicationController
before_action :set_elevated_users, only: [:index, :list_generic_users]
before_action :set_user, only: [:edit, :update, :destroy]

layout 'administration'

def current_ability
Expand All @@ -13,12 +15,10 @@ def index
end

def edit
@user = User.find_by_id(params[:id])
authorize! :edit, @user
end

def update
@user = User.find_by_id(params[:id])
authorize! :update, @user
old_image_data = @user.image_data
@user.update(user_params)
Expand Down Expand Up @@ -48,20 +48,14 @@ def elevate
end
end

def list
search = User.search { fulltext params[:term] }
@users = search.results
end

def list_generic_users
authorize! :list_generic_user, @user
authorize! :list_generic_users, User.new
result = User.where.not(id: @elevated_users.pluck(:id))
.values_for_select
render json: result
end

def destroy
@user = User.find_by_id(params[:id])
authorize! :destroy, @user
@user.destroy unless @user.admin || @user.editor? || @user.teacher?
redirect_to users_path
Expand All @@ -79,6 +73,7 @@ def teacher
end

def fill_user_select
authorize! :fill_user_select, User.new
if params[:q]
result = User.preferred_name_or_email_like(params[:q])
.values_for_select
Expand All @@ -104,6 +99,12 @@ def user_params
:current_lecture_id,:image)
end

def set_user
@user = User.find_by_id(params[:id])
return unless @user.nil?
redirect_to :root, alert: I18n.t('controllers.no_medium')
end

def set_elevated_users
@elevated_users = User.where(admin: true).or(User.proper_editors)
.or(User.teachers)
Expand Down
3 changes: 1 addition & 2 deletions app/views/users/_basics.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<div class="form-group">
<%= f.label :name, t('basics.display_name') %>
<%= helpdesk(t('admin.user.info.display_name'), false) %>
<%= f.text_field :name, { class: 'form-control',
disabled: true } %>
<%= f.text_field :name, { class: 'form-control' } %>
<div class="invalid-feedback" id="user-name-error">
</div>
</div>
Expand Down
4 changes: 0 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,6 @@
to: 'users#fill_user_select',
as: 'fill_user_select'

get 'users/list',
to: 'users#list',
as: 'list_users'

get 'users/delete_account',
to: 'users#delete_account',
as: 'delete_account'
Expand Down

0 comments on commit de9e1b1

Please sign in to comment.