質問編集履歴

1

コントローラーを追記しました

2020/10/18 07:27

投稿

zoff77
zoff77

スコア19

test CHANGED
File without changes
test CHANGED
@@ -197,3 +197,229 @@
197
197
  end
198
198
 
199
199
  ```
200
+
201
+
202
+
203
+
204
+
205
+ ```rb
206
+
207
+ class AttendancesController < ApplicationController
208
+
209
+
210
+
211
+ before_action :set_user, only: [:edit_one_month]
212
+
213
+ before_action :logged_in_user, only: [:update, :edit_one_month]
214
+
215
+ before_action :admin_user, only: [:index,:destroy,:edit_basic_info]
216
+
217
+ before_action :set_one_month, only: [:edit_one_month]
218
+
219
+
220
+
221
+ UPDATE_ERROR_MSG = "勤怠登録に失敗しました。やり直してください。"
222
+
223
+
224
+
225
+
226
+
227
+ # 申請お知らせモーダル
228
+
229
+
230
+
231
+ def edit_overtime_notice
232
+
233
+ @user = User.find(params[:user_id])
234
+
235
+ @attendances = Attendance.where(indicater_check_superior: "申請中", indicater_check: @user.name).order(:user_id, :worked_on).group_by(&:user_id)
236
+
237
+ end
238
+
239
+
240
+
241
+
242
+
243
+ # お知らせモーダル更新
244
+
245
+ def update_overtime_notice
246
+
247
+ end
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+ # 申請モーダル
258
+
259
+ def edit_overtime_request
260
+
261
+ @user = User.find(params[:user_id])
262
+
263
+ @attendance = Attendance.find(params[:id])
264
+
265
+ @superior = User.where(superior: true).where.not( id: current_user.id )
266
+
267
+ end
268
+
269
+
270
+
271
+
272
+
273
+ def update_overtime_request
274
+
275
+ @user = User.find(params[:user_id])
276
+
277
+ @attendance = Attendance.find(params[:id])
278
+
279
+ if @attendance.update_attributes(overtime_params)
280
+
281
+ flash[:success] = "残業申請を受け付けました"
282
+
283
+ redirect_to user_url(@user)
284
+
285
+ end
286
+
287
+ end
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+ def update
296
+
297
+ @user = User.find(params[:user_id])
298
+
299
+ @attendance = Attendance.find(params[:id])
300
+
301
+ if @attendance.started_at.nil?
302
+
303
+ if @attendance.update_attributes(started_at: Time.current.change(sec: 0))
304
+
305
+ flash[:info] = "おはようございます!"
306
+
307
+ else
308
+
309
+ flash[:danger] = UPDATE_ERROR_MSG
310
+
311
+ end
312
+
313
+ elsif @attendance.finished_at.nil?
314
+
315
+ if @attendance.update_attributes(finished_at: Time.current.change(sec: 0))
316
+
317
+ flash[:info] = "お疲れ様でした。"
318
+
319
+ else
320
+
321
+ flash[:danger] = UPDATE_ERROR_MSG
322
+
323
+ end
324
+
325
+ end
326
+
327
+ redirect_to @user
328
+
329
+ end
330
+
331
+
332
+
333
+ def edit_one_month
334
+
335
+ end
336
+
337
+
338
+
339
+ def update_one_month
340
+
341
+ ActiveRecord::Base.transaction do # トランザクションを開始します。
342
+
343
+ attendances_params.each do |id, item|
344
+
345
+ attendance = Attendance.find(id)
346
+
347
+ attendance.update_attributes!(item)
348
+
349
+ end
350
+
351
+ end
352
+
353
+ flash[:success] = "1ヶ月分の勤怠情報を更新しました。"
354
+
355
+ redirect_to user_url(date: params[:date])
356
+
357
+ rescue ActiveRecord::RecordInvalid # トランザクションによるエラーの分岐です。
358
+
359
+ flash[:danger] = "無効な入力データがあった為、更新をキャンセルしました。"
360
+
361
+ redirect_to attendances_edit_one_month_user_url(date: params[:date])
362
+
363
+ end
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+ private
380
+
381
+ # 1ヶ月分の勤怠情報を扱います。
382
+
383
+ def attendances_params
384
+
385
+ params.require(:user).permit(attendances: [:started_at, :finished_at, :note])[:attendances]
386
+
387
+ end
388
+
389
+
390
+
391
+
392
+
393
+ # 申請モーダルの情報
394
+
395
+ def overtime_params
396
+
397
+ params.require(:attendance).permit(:overtime_finished_at, :tomorrow, :overtime_work,:indicater_check,:indicater_check_superior)
398
+
399
+ end
400
+
401
+
402
+
403
+ # 申請お知らせモーダルの情報
404
+
405
+ def overtime_notice_params
406
+
407
+ params.require(:attendance).permit(:indicater_reply, :change, :verification, :indicater_check_superior)
408
+
409
+ end
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+ end
422
+
423
+
424
+
425
+ ```