Class: UsersController

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

Overview

This controller manage the views refering to User model

before_action :#set_user, only: [:show, :edit, :update, :destroy, unlock, trash}

Instance Method Summary collapse

Instance Method Details

#createObject

POST /users

Create a new User set @user as new User with #user_params as param

Returns:

  • (Object)

    render users/index if @user is created or users/new if fail



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/users_controller.rb', line 58

def create
  @user = User.new(user_params)
  if @user.save
    UsersCheckJob.perform_now(username: @user.username)
    flash.now[:success] = 'Creazione avvenuta con successo'
    render turbo_stream: [
        turbo_stream.replace(:flashes, partial: "flashes"),
        turbo_stream.replace(:yield, partial: "users/show", locals: { user: @user.reload, tab: @tab })
    ]
  else
    flash.now[:error] =  write_errors(@user)
    render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes")
  end
end

#destroyObject

DELETE /users/1

lock a user

  • #set_user has set @user for destroy

Returns:

  • (Object)

    render users/list



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/users_controller.rb', line 94

def destroy
  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)
    format.turbo_stream { render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes") }
  end
end

#editObject

GET /users/1/edit

Render the form for edit a user #set_user has set @user for edit

Returns:

  • (Object)

    render users/edit



51
# File 'app/controllers/users_controller.rb', line 51

def edit; end

#indexObject

GET /users

render users index set @pagy, @users for the users pagination

Returns:

  • (Object)

    render users/index



16
# File 'app/controllers/users_controller.rb', line 16

def index; end

#listObject

GET /users/list

render users list set @pagy, @users for the users pagination

Returns:

  • (Object)

    render users/list



23
24
25
26
# File 'app/controllers/users_controller.rb', line 23

def list
  users
  flash.now[:success] = 'Caricamento completato'
end

#newObject

GET /users/new

Render the form for create a new user set @user as new User

Returns:

  • (Object)

    rendeer users/new



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

def new
  @user = User.new
end

#showObject

GET /users/1

render users show

Returns:

  • (Object)

    render users/show



32
33
34
35
# File 'app/controllers/users_controller.rb', line 32

def show
  @view = params[:view] || ''
  @current_ability.cannot(:manage, @user) if @view == 'modal'
end

#trashObject

DELETE /users/1/trash

destroy a user

  • #set_user has set @user for destroy

Returns:

  • (Object)

    render users/list



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

def trash
  @user.forced = 'true'
  if @user.destroy
    flash.now[:success] = 'Cancellazione avvenuta con successo'
    render turbo_stream: [
        turbo_stream.remove("user_#{@user.id}"),
        turbo_stream.replace(:flashes, partial: "flashes")
      ]
  else
    flash.now[:error] = write_errors(@user)
    render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes")
  end
end

#unlockObject

PATCH /users/1/unlock

unlock a user

  • #set_user has set @user for unlock

Returns:

  • (Object)

    render users/list



113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/users_controller.rb', line 113

def unlock
  if @user.enable!
    flash.now[:success] = 'Riattivazione 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

#updateObject

PATCH/PUT /users/1

update a user #set_user has set @user for update and update with #user_params as param

Returns:

  • (Object)

    render users/index if @user is updated or users/new if fail



79
80
81
82
83
84
85
86
# File 'app/controllers/users_controller.rb', line 79

def update
  if @user.update(user_params)
    flash.now[:success] = 'Modifica avvenuta con successo'
  else
    flash.now[:error] = write_errors(@user)
  end
  render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes")
end