From 23f0cc92c2941f35b465221620f4855b9131cee3 Mon Sep 17 00:00:00 2001 From: BoHung Chiu Date: Tue, 22 Aug 2023 23:07:05 +0800 Subject: [PATCH] Fix bug. --- .../admin/ruling_timers_controller.rb | 2 +- app/models/ruling_timer_task.rb | 14 ++++++-------- .../admin/ruling_timers/_task_table.html.erb | 2 +- config/routes.rb | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/ruling_timers_controller.rb b/app/controllers/admin/ruling_timers_controller.rb index a53baaa..3aa41c1 100644 --- a/app/controllers/admin/ruling_timers_controller.rb +++ b/app/controllers/admin/ruling_timers_controller.rb @@ -47,7 +47,7 @@ class Admin::RulingTimersController < OrbitMemberController end def timer_management @timer_temps = RulingTimerTemp.all.page(params[:page]).per(15) - @active_users = @timer_temps.map{|t| t.user} + @active_users = @timer_temps.map{|t| t.user}.compact end def task_management @tasks = RulingTimerTask.all.desc(:created_at).page(params[:page]).per(10) diff --git a/app/models/ruling_timer_task.rb b/app/models/ruling_timer_task.rb index 467af1c..8e4594e 100644 --- a/app/models/ruling_timer_task.rb +++ b/app/models/ruling_timer_task.rb @@ -69,20 +69,18 @@ class RulingTimerTask end after_save do if self.ruling_timer_project_id_change && !@already_save - if self.ruling_timer_project_id_change[0] - old_project = RulingTimerProject.find(self.ruling_timer_project_id_change[0]) rescue nil - if old_project - old_project.task_count -= 1 - @already_save = true - old_project.save(:validate=>false) - end + if self.ruling_timer_project_id_was + RulingTimerProject.where(:id=>self.ruling_timer_project_id_was).update_all({"$inc"=>{"task_count"=>-1}}) + old_project = RulingTimerProject.where(:id=>self.ruling_timer_project_id_was).first + old_project.all_user_ids = old_project.ruling_timer_tasks.pluck(:user_ids).uniq + old_project.save end new_project = self.ruling_timer_project - new_project.task_count += 1 new_project.all_user_ids += self.get_all_user_ids new_project.all_user_ids.uniq @already_save = true new_project.save(:validate=>false) + RulingTimerProject.where(:id=>self.ruling_timer_project_id).update_all({"$inc"=>{"task_count"=>1}}) end if self.ruling_timer_section_id_change && !@already_save need_create_notify = true diff --git a/app/views/admin/ruling_timers/_task_table.html.erb b/app/views/admin/ruling_timers/_task_table.html.erb index e34bc73..f803570 100644 --- a/app/views/admin/ruling_timers/_task_table.html.erb +++ b/app/views/admin/ruling_timers/_task_table.html.erb @@ -27,7 +27,7 @@ <% user_ids = task.user_ids user_bson_ids = user_ids.map{|id| BSON::ObjectId(id)} %> - <% users = User.find(user_ids).index_by(&:id).slice(*user_bson_ids).values %> + <% users = User.where(:id.in=>user_ids).index_by(&:id).slice(*user_bson_ids).values %> <% users.each do |user| %> <% sub_task = task.ruling_timer_sub_tasks.where(:user=>user).first %> <% next if sub_task.nil? %> diff --git a/config/routes.rb b/config/routes.rb index 90c6d9e..f8cae0c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,6 +51,25 @@ Rails.application.routes.draw do category.title_translations = I18n.available_locales.map{|l| [l.to_s,I18n.with_locale(l){I18n.t("ruling_timer.personal_project")}]}.to_h category.save end + RulingTimerTemp.all.to_a.each do |t| + if t.user.nil? # user has been removed + user_id = t.user_id.to_s + RulingTimerTask.where(:user_ids=>user_id).to_a.each do |task| + task.ruling_timer_sub_tasks.where(:user_id=>user_id).destroy + if task.owner_id.to_s == user_id + task.owner_id = nil + end + task.helper_ids.delete(user_id) + task.user_ids.delete(user_id) + task.save + end + RulingTimerProject.where(:all_user_ids=>user_id).each do |proj| + proj.all_user_ids.delete(user_id) + proj.save + end + t.destroy + end + end rescue => e puts e.to_s end