Class: GroupsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#dev_only, #require_login

Methods included from SessionsHelper

#console?, #current_user, #debug?, #forget, #log_in, #log_out, #logged_in?, #remember, #toggle_console, #toggle_debug

Instance Method Details

#AddUserToGroupObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/groups_controller.rb', line 57

def AddUserToGroup
  @user = User.find_by(email: params[:email]);
  @group = Group.find(params[:groupId])

  if !@user.nil? && !@group.nil?
    if !@group.user_ids.include?(@user.id)
      if GroupsUser.create(user_id: @user.id, group_id: @group.id, role: "Standard")
        flash[:success] = "User added to group!"
        redirect_to @group
      end
    else
      flash[:danger] = "User already in group"
      redirect_to @group
    end
  else
    flash[:warning] = "User not found"
    redirect_to @group
  end
end

#createObject



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

def create
  @group = Group.new(group_params)

  if @group.save
    if GroupsUser.create(user_id: current_user.id, group_id: @group.id, role: "Admin")
      flash[:success] = "Creation Success for: Group"
      redirect_to @group
    end
  else
    render 'new'
  end
end

#DeleteUserFromGroupObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/groups_controller.rb', line 77

def DeleteUserFromGroup
  @user = User.find(params[:userId])
  @group = Group.find(params[:groupId])

  if !@user.nil? && !@group.nil?
    if @group.users.include?(@user)
      @user.groups.delete(@group)
      flash[:success] = "User removed from group"
      redirect_to @group
    else
      flash[:danger] = "User is already not in this group"
      redirect_to @group
    end
  else
    flash[:warning] = "Cannot find user"
    redirect_to @group
  end
end

#destroyObject



51
52
53
54
55
# File 'app/controllers/groups_controller.rb', line 51

def destroy
  @group.destroy

  redirect_to root_url
end

#editObject



26
27
# File 'app/controllers/groups_controller.rb', line 26

def edit
end

#indexObject



4
5
6
# File 'app/controllers/groups_controller.rb', line 4

def index
  @groups = current_user.groups
end

#newObject



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

def new
  @group = Group.new
end

#showObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/groups_controller.rb', line 8

def show
  users = @group.users
  users.each do |user|
    if @u = GroupsUser.find_by(group_id: @group.id, user_id: user.id)
      user.role = @u.role
    end
  end

  @users = users.sort_by(&:role)
  @meal = Meal.new
  @meals = @group.meals.order(:meal_date)
  @average = MetricsService.new(@meals.to_a).meals_average
end

#updateObject



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

def update
  if @group.update(group_params)
    flash[:success] = "Update Success for: Group"
    redirect_to @group
  else
    render 'edit'
  end
end