Class: UsersController

Inherits:
ApplicationController show all
Includes:
Pagy::Backend
Defined in:
app/controllers/users_controller.rb

Overview

This controller manage the views refering to Risk model

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_in!, #doctor_in!, #powered_in!, #secretary_in!, #translate_errors

Instance Method Details

#createObject

POST /users

Add a new user manually



146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/users_controller.rb', line 146

def create
  @user = User.new(external_user_params)
  @user.label = "#{external_user_params[:lastname]} #{external_user_params[:name]}"
  if @user.save
    @users = User.where('label ilike ?', "%#{@user.label}%").order('label asc')
    @pagy, @users = pagy(@users, link_extra: "data-turbo-frame='users'")
    redirect_to users_url
  else
    render :new
  end
end

#destroyObject

DELETE /users/:id

set an User as locked

Returns:

  • (Object)

    redirect to /home/index



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/users_controller.rb', line 52

def destroy
  current_user = @user
  @user.author = current_user.label
  if @user.disable!
    flash.now[:success] = 'Disattivazione avvenuta con successo'
    render turbo_stream: [
      turbo_stream.replace("user_#{@user.id}", partial: 'users/user', locals: {user: @user, current_user: current_user}),
      turbo_stream.replace(:flashes, partial: "flashes")
    ]
  else
    flash.now[:error] = write_errors(@user)
    render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes")
  end
end

#edit_external_userObject

GET /user/:id/edit_external

render the form for make an external user

Returns:

  • (Object)

    render /users/new



125
126
127
# File 'app/controllers/users_controller.rb', line 125

def edit_external_user
  render partial: 'form'
end

#external_userObject

GET



130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/users_controller.rb', line 130

def external_user
  if request.post?
    create
  elsif request.put?
    @user.label = "#{external_user_params[:lastname]} #{external_user_params[:name]}"
    if @user.update(external_user_params)
      redirect_to users_url
    else
      render :edit_external_user
    end
  end
end

#indexObject

GET /users

render the list the all User

  • set @users_filtered with #users

  • set @pagy, @user for the @users pagination

Returns:

  • (Object)

    render users/index



18
19
20
# File 'app/controllers/users_controller.rb', line 18

def index
  # index
end

#listObject

GET /users/list

render the list the all User

  • set @users_filtered with #users

  • set @pagy, @user for the @users pagination

Returns:

  • (Object)

    render users/index



28
29
30
# File 'app/controllers/users_controller.rb', line 28

def list
  @pagy, @users = pagy(users, link_extra: "data-turbo-frame='users'")
end

#newObject

GET /users/new

render the form for make a new User

  • set @user as new User

Returns:

  • (Object)

    render /users/new



117
118
119
# File 'app/controllers/users_controller.rb', line 117

def new
  @user = User.new
end

#notifyNil

PUT /user/:id/notify

Send notify for the user’s analisy events and user’s vitis events

Returns:

  • (Nil)


105
106
107
108
109
110
# File 'app/controllers/users_controller.rb', line 105

def notify
  return if @user.email.blank?

  Notifier.user_event_analisys(@user, @user.events.future.analisys.first).deliver_now if @user.events.future.analisys.present?
  Notifier.user_event_visit(@user, @user.events.future.visits.first).deliver_now if @user.events.future.visits.present?
end

#remove_attachmentObject

DELETE /user/remove_attachment

delete the assegnazione file from user

Returns:

  • (Object)

    render /users/show



167
168
169
170
# File 'app/controllers/users_controller.rb', line 167

def remove_attachment
  @user.assegnazione.purge if @user.assegnazione.attached?
  render :show
end

#showObject

GET /users/:id

render the user details

  • set @user with #set_user method

  • set @analisys with user’s analisy events (Event)

  • set @visits with user’s visit events (Event)

  • set @audit with user’s audits (Audit)

Returns:

  • (Object)

    render users/show



40
41
42
43
44
# File 'app/controllers/users_controller.rb', line 40

def show
  @analisys = @user.events.analisys.future.to_a.uniq(&:id)
  @visits   = @user.events.visit.future.to_a.uniq(&:id)
  @audits   = @user.audits
end

#unlockObject

PATCH /users/:id/unlock

Set an User as unlock

render /users/show



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/users_controller.rb', line 73

def unlock
  if @user.enable!
    flash.now[:success] = 'Attivazione avvenuta con successo'
    render turbo_stream: [
      turbo_stream.replace("user_#{@user.id}", partial: 'users/user', locals: {user: @user, current_user: current_user}),
      turbo_stream.replace("user_#{@user.id}_show", partial: 'users/show', locals: {user: @user, analisys: @user.events.analisys.future.to_a.uniq(&:id), visits: @user.events.visit.future.to_a.uniq(&:id), view: @view}),
      turbo_stream.replace(:flashes, partial: "flashes")
    ]
  else
    flash.now[:error] = write_errors(@user)
    render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes")
  end
end

#updateObject

PATCH /users/:id

try to update an User

  • set @user with #set_user method

  • set @response true if @user is updated

Returns:

  • (Object)

    render users/index if @response is true otherwise render users/edit



93
94
95
96
97
98
99
# File 'app/controllers/users_controller.rb', line 93

def update
  if @user.update(user_params)
    render :show, status: :ok
  else
    render json: { error: translate_errors(@user.errors, scope: 'user').join(', '), status: :unprocessable_entity }, status: 500
  end
end

#userObject

render a single user



159
160
161
# File 'app/controllers/users_controller.rb', line 159

def user
  render @user
end