Class: Admin::GroupsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/admin/groups_controller.rb

Overview

This controller manage Group model for admin User

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin!

Methods inherited from ApplicationController

#access_denied!, #default_url_options, #record_not_found!, #set_turbo, #switch_locale

Instance Method Details

#createObject

POST /admin/groups



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/admin/groups_controller.rb', line 27

def create
  @group = Group.new(group_params)
  set_users

  if @group.save
    @status = { success: "Gruppo creato" }
    redirect_to admin_groups_path
  else
    @status = { error: "Creazione gruppo fallita" }
    render action: "edit"
  end
end

#destroyObject

DELETE /admin/groups/1



53
54
55
56
# File 'app/controllers/admin/groups_controller.rb', line 53

def destroy
  @group.destroy
  redirect_to admin_groups_path, notice: "Group was successfully destroyed."
end

#editObject

GET /admin/groups/1/edit



22
23
24
# File 'app/controllers/admin/groups_controller.rb', line 22

def edit
  @users = User.editors.pluck :email, :id
end

#filter_paramsObject (private)

filter params for search groups



71
72
73
# File 'app/controllers/admin/groups_controller.rb', line 71

def filter_params
  params.fetch(:filter, {}).permit(:text)
end

#group_paramsObject (private)

Filter params for create/change a Ticket istance



76
77
78
# File 'app/controllers/admin/groups_controller.rb', line 76

def group_params
  params.require(:group).permit(:title, user_ids: [])
end

#indexObject

GET /admin/groups



10
11
12
13
# File 'app/controllers/admin/groups_controller.rb', line 10

def index
  @text = [ "title ilike :text", { text: "%#{filter_params[:text]}%" } ] if filter_params[:text].present?
  @pagy, @groups = pagy(Group.all.where(@text))
end

#newObject

GET /admin/groups/new



16
17
18
19
# File 'app/controllers/admin/groups_controller.rb', line 16

def new
  @group = Group.new
  render action: "edit"
end

#set_groupObject (private)

Set Group when needed



61
62
63
# File 'app/controllers/admin/groups_controller.rb', line 61

def set_group
  @group = Group.find(params[:id])
end

#set_usersObject (private)

preset users value



66
67
68
# File 'app/controllers/admin/groups_controller.rb', line 66

def set_users
@users = User.editors.pluck :email, :id
end

#updateObject

PATCH/PUT /admin/groups/1



41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/admin/groups_controller.rb', line 41

def update
  @users = @group.users.pluck :email, :id
  if @group.update(group_params)
    @status = { success: "Gruppo aggiornato" }
    redirect_to admin_groups_path
  else
    @status = { error: "Aggiornamento gruppo fallito" }
    render :edit
  end
end