Class: Pairer::BoardsController

Inherits:
ApplicationController show all
Includes:
ActionView::Helpers::JavaScriptHelper
Defined in:
app/controllers/pairer/boards_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#render_404, #robots, #signed_in?

Instance Method Details

#createObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/pairer/boards_controller.rb', line 30

def create
  @board = Pairer::Board.new(password: params[:password], org_id: session[:pairer_current_org_id])

  if @board.save
    session[:pairer_current_board_id] = @board.to_param
    flash.notice = "Board created."
    redirect_to(action: :show, id: @board.to_param)
  else
    other_board = Pairer::Board.find_by(org_id: session[:pairer_current_org_id], password: params[:password])

    if other_board
      session[:pairer_current_board_id] = other_board.to_param
      flash.notice = "Existing board found."
      redirect_to(action: :show, id: other_board.to_param)
    else
      flash.alert = "Board not saved. Please choose a different password."
      redirect_to(action: :index)
    end
  end
end

#create_groupObject



145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/pairer/boards_controller.rb', line 145

def create_group
  @group = @board.groups.create!(board_iteration_number: @board.current_iteration_number)
  broadcast_changes

  if request.format.js?
    render_js_page_replace
  else
    redirect_to(action: :show)
  end
end

#create_personObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/pairer/boards_controller.rb', line 109

def create_person
  @person = @board.people.create(name: params[:name])

  if @person.persisted?
    broadcast_changes
  end

  if request.format.js?
    render_js_page_replace
  else
    redirect_to(action: :show)
  end
end

#delete_groupObject



169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/controllers/pairer/boards_controller.rb', line 169

def delete_group
  @group = @board.current_groups.find_by!(public_id: params.require(:group_id))

  @group.destroy!
  broadcast_changes

  if request.format.js?
    render_js_page_replace
  else
    redirect_to(action: :show)
  end
end

#delete_personObject



136
137
138
139
140
141
142
143
# File 'app/controllers/pairer/boards_controller.rb', line 136

def delete_person
  @person = @board.people.find_by!(public_id: params.require(:person_id))

  @person.destroy!
  broadcast_changes

  redirect_to(action: :show)
end

#destroyObject



97
98
99
100
101
# File 'app/controllers/pairer/boards_controller.rb', line 97

def destroy
  @board.destroy!
  flash.notice = "Board deleted."
  redirect_to(action: :index)
end

#indexObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/pairer/boards_controller.rb', line 15

def index
  if params[:password].present?
    @board = Pairer::Board.find_by(org_id: session[:pairer_current_org_id], password: params[:password])

    if @board
      session[:pairer_current_board_id] = @board.to_param
      redirect_to action: :show, id: session[:pairer_current_board_id]
    else
      flash.now.alert = "Board not found."

      render
    end
  end
end

#lock_groupObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/pairer/boards_controller.rb', line 156

def lock_group
  @group = @board.current_groups.find_by!(public_id: params.require(:group_id))

  @group.toggle!(:locked)
  broadcast_changes

  if request.format.js?
    render_js_page_replace
  else
    redirect_to(action: :show)
  end
end

#lock_personObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/pairer/boards_controller.rb', line 123

def lock_person
  @person = @board.people.find_by!(public_id: params.require(:person_id))

  @person.toggle!(:locked)
  broadcast_changes

  if request.format.js?
    render_js_page_replace
  else
    redirect_to(action: :show)
  end
end

#showObject



51
52
53
54
55
56
57
58
59
# File 'app/controllers/pairer/boards_controller.rb', line 51

def show
  session_key = :pairer_board_access_list

  access_list = session[session_key] || {}

  access_list[@board.public_id] = Time.now.to_s

  session[session_key] = access_list
end

#shuffleObject



103
104
105
106
107
# File 'app/controllers/pairer/boards_controller.rb', line 103

def shuffle
  @board.shuffle!
  broadcast_changes
  redirect_to(action: :show)
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/pairer/boards_controller.rb', line 61

def update
  if params[:add_role_name]
    params[:board] = {}
    params[:board][:roles] = @board.roles_array + [params[:add_role_name]]
  elsif params[:remove_role_name]
    params[:board] = {}
    params[:board][:roles] = @board.roles_array - [params[:remove_role_name]]
  end

  if params[:clear_board]
    @board.groups.destroy_all
    @board.update!(current_iteration_number: 0)
    saved = true
  else
    saved = @board.update(params.require(:board).permit(:name, :password, :group_size, :num_iterations_to_track, roles: []))
  end

  if saved
    if params.dig(:board, :password)
      flash.notice = "Password updated."
    else
      flash.notice = "Board updated."
    end

    broadcast_changes
  else
    if @board.errors[:num_iterations_to_track].present?
      flash.alert = "Number of Iterations to Track #{@board.errors[:num_iterations_to_track].first}"
    else
      flash.alert = "Board not saved."
    end
  end

  redirect_to(action: :show)
end

#update_groupObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/pairer/boards_controller.rb', line 182

def update_group
  @group = @board.current_groups.find_by!(public_id: params.require(:group_id))

  person_ids_was = @group.person_ids_array
  roles_was = @group.roles_array

  attrs = {
    person_ids: (params[:person_ids] if params[:person_ids].present?),
    roles: (params[:roles] if params[:roles].present?),
  }.compact

  @group.assign_attributes(attrs)

  new_person_ids = @group.person_ids_array - person_ids_was
  new_roles = @group.roles_array - roles_was

  if @group.changed?
    changed = true

    if params[:removal] == "true"
      @group.save!
    else
      ActiveRecord::Base.transaction do
        @group.save!

        if new_person_ids.any?
          @board.current_groups.detect do |g|
            next if g.id == @group.id

            if g.person_ids_array.intersection(new_person_ids).any?
              g.person_ids = g.person_ids_array - @group.person_ids_array
              g.save!
              true
            end
          end
        end

        if new_roles.any?
          @board.current_groups.detect do |g|
            next if g.id == @group.id

            if g.roles_array.intersection(new_roles).any?
              g.roles = g.roles_array - @group.roles_array
              g.save!
              true
            end
          end
        end
      end
    end

    broadcast_changes
  end

  if request.format.js?
    render_js_page_replace
  else
    redirect_to(action: :show)
  end
end