Class: Admin::GroupsController

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

Overview

This controller manage Group model for admin User

Instance Method Summary collapse

Instance Method Details

#createObject

POST /admin/groups


32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/admin/groups_controller.rb', line 32

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

  if @group.save
    @status = { success: 'Gruppo creato' }
    redirect_to list_admin_groups_path
  else
    @status = { error: 'Creazione gruppo fallita' }
    render :new
  end
end

#destroyObject

DELETE /admin/groups/1


58
59
60
61
62
63
64
# File 'app/controllers/admin/groups_controller.rb', line 58

def destroy
  @group.destroy
  respond_to do |format|
    format.html { redirect_to groups_url, notice: 'Group was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#editObject

GET /admin/groups/1/edit


27
28
29
# File 'app/controllers/admin/groups_controller.rb', line 27

def edit
  @users = @group.users.pluck :username, :id
end

#filter_paramsObject (private)

filter params for search groups


79
80
81
# File 'app/controllers/admin/groups_controller.rb', line 79

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

#group_paramsObject (private)

Filter params for create/change a Ticket istance


84
85
86
# File 'app/controllers/admin/groups_controller.rb', line 84

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

#indexObject

GET /admin/groups


9
# File 'app/controllers/admin/groups_controller.rb', line 9

def index; end

#listObject

GET /admin/groups/list


12
13
14
15
# File 'app/controllers/admin/groups_controller.rb', line 12

def list
  @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


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

def new
  @group = Group.new
  @users = []
end

#set_groupObject (private)

Set Group when needed


69
70
71
# File 'app/controllers/admin/groups_controller.rb', line 69

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

#set_usersObject (private)

preset users value


74
75
76
# File 'app/controllers/admin/groups_controller.rb', line 74

def set_users
  @users = @group.users
end

#showObject

GET /admin/groups/1


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

def show; end

#updateObject

PATCH/PUT /admin/groups/1


46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/admin/groups_controller.rb', line 46

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