Class: Strongbolt::UserGroupsController

Inherits:
StrongboltController show all
Defined in:
app/controllers/strongbolt/user_groups_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 16

def create
  @user_group = UserGroup.create! user_group_params

  flash[:success] = 'Use group was successfully created!'
  redirect_to user_group_path(@user_group)
rescue ActiveRecord::RecordInvalid
  flash[:danger] = 'User Group could not be created, please review the errors below'
  redirect_to new_user_group_path
rescue ActionController::ParameterMissing => e
  flash[:danger] = "User Group could not be created: ERROR #{e}"
  redirect_to new_user_group_path
end

#destroyObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 43

def destroy
  @user_group = UserGroup.find params[:id]
  @user_group.destroy!

  flash[:success] = "User group #{@user_group.name} successfully deleted"

  redirect_to user_groups_path
rescue ActiveRecord::DeleteRestrictionError
  flash[:danger] = "User group #{@user_group.name} cannot be deleted because #{@user_group.users.size} users belong to it"

  redirect_to user_group_path(@user_group)
end

#editObject



56
57
58
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 56

def edit
  @user_group = UserGroup.find params[:id]
end

#indexObject



3
4
5
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 3

def index
  @user_groups = UserGroup.all
end

#newObject



60
61
62
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 60

def new
  @user_group = UserGroup.new
end

#showObject



7
8
9
10
11
12
13
14
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 7

def show
  @user_group = UserGroup.find params[:id]
  # We select the users not yet in the user group
  @users = Strongbolt.user_class_constant
                     .joins("LEFT JOIN strongbolt_user_groups_users sugu ON sugu.user_id = #{Strongbolt.user_class_constant.table_name}.id")
                     .joins('LEFT JOIN strongbolt_user_groups sug ON sug.id = sugu.user_group_id')
                     .where('sug.id IS NULL OR sug.id != ?', @user_group.id)
end

#updateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/strongbolt/user_groups_controller.rb', line 29

def update
  @user_group = UserGroup.find params[:id]
  @user_group.update_attributes! user_group_params

  flash[:success] = 'User group was successfully updated!'
  redirect_to user_group_path params[:id]
rescue ActiveRecord::RecordInvalid
  flash[:danger] = 'User Group could not be modified, please review the errors below'
  redirect_to edit_user_group_path(params[:id])
rescue ActionController::ParameterMissing => e
  flash[:danger] = "User Group could not be updated: ERROR #{e}"
  redirect_to edit_user_group_path(params[:id])
end