diff --git a/app/controllers/api/v1/issues/issue_tags_controller.rb b/app/controllers/api/v1/issues/issue_tags_controller.rb index 99fc7d45c..fe2ecceab 100644 --- a/app/controllers/api/v1/issues/issue_tags_controller.rb +++ b/app/controllers/api/v1/issues/issue_tags_controller.rb @@ -4,8 +4,8 @@ class Api::V1::Issues::IssueTagsController < Api::V1::BaseController before_action :require_operate_above, only: [:create, :update, :destroy] def index - @issue_tags = @issue_tags.ransack(name_cont: params[:keyword]).result if params[:keyword].present? @issue_tags = @project.issue_tags.reorder("#{sort_by} #{sort_direction}") + @issue_tags = @issue_tags.ransack(name_cont: params[:keyword]).result if params[:keyword].present? if params[:only_name] @issue_tags = kaminary_select_paginate(@issue_tags.select(:id, :name, :color)) else diff --git a/app/models/issue.rb b/app/models/issue.rb index fa25c06a6..9fe7b0a9a 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -73,8 +73,8 @@ class Issue < ApplicationRecord has_many :issue_participants, dependent: :destroy has_many :participants, through: :issue_participants has_many :show_participants, -> {joins(:issue_participants).where.not(issue_participants: {participant_type: "atme"}).distinct}, through: :issue_participants, source: :participant - has_many :show_assigners, -> {joins(:issue_assigners).order("issue_assigners.created_at desc").distinct.limit(5)}, through: :issue_assigners, source: :assigner - has_many :show_issue_tags, -> {joins(:issue_tags_relates).order("issue_tags_relates.created_at desc").distinct.limit(3)}, through: :issue_tags_relates, source: :issue_tag + has_many :show_assigners, -> {joins(:issue_assigners).distinct}, through: :issue_assigners, source: :assigner + has_many :show_issue_tags, -> {joins(:issue_tags_relates).distinct}, through: :issue_tags_relates, source: :issue_tag has_many :comment_journals, -> {where.not(notes: nil)}, class_name: "Journal", :as => :journalized has_many :operate_journals, -> {where(notes: nil)}, class_name: "Journal", :as => :journalized diff --git a/app/models/issue_tag.rb b/app/models/issue_tag.rb index 5381da9be..100373e80 100644 --- a/app/models/issue_tag.rb +++ b/app/models/issue_tag.rb @@ -48,5 +48,9 @@ class IssueTag < ApplicationRecord $redis_cache.hset("project_init_issue_tags", project_id, 1) end + def reset_counter_field + self.update_column(:issues_count, issue_issues.size) + self.update_column(:pull_requests_count, pull_request_issues.size) + end end diff --git a/app/models/issue_tags_relate.rb b/app/models/issue_tags_relate.rb index 13eaa780f..fc753e95b 100644 --- a/app/models/issue_tags_relate.rb +++ b/app/models/issue_tags_relate.rb @@ -18,12 +18,26 @@ class IssueTagsRelate < ApplicationRecord belongs_to :issue_tag after_create :increment_issue_tags_counter_cache + after_destroy :decrement_issue_tags_counter_cache def increment_issue_tags_counter_cache + Rails.logger.info "11111" + Rails.logger.info self.issue.issue_classify + if self.issue.issue_classify == "issue" IssueTag.increment_counter :issues_count, issue_tag_id else IssueTag.increment_counter :pull_requests_count, issue_tag_id end end + + def decrement_issue_tags_counter_cache + Rails.logger.info "2222222" + Rails.logger.info self.issue.issue_classify + if self.issue.issue_classify == "issue" + IssueTag.decrement_counter :issues_count, issue_tag_id + else + IssueTag.decrement_counter :pull_requests_count, issue_tag_id + end + end end diff --git a/app/services/api/v1/issues/list_service.rb b/app/services/api/v1/issues/list_service.rb index e5a96a481..f88e1fc63 100644 --- a/app/services/api/v1/issues/list_service.rb +++ b/app/services/api/v1/issues/list_service.rb @@ -81,7 +81,7 @@ class Api::V1::Issues::ListService < ApplicationService issues = issues.opened end - scope = issues.includes(:priority, :issue_status, :user, :show_assigners, :show_issue_tags, :version, :issue_tags, :comment_journals) + scope = issues.includes(:priority, :issue_status, :user, :show_assigners, :show_issue_tags, :version, :comment_journals) scope = scope.reorder("#{sort_by} #{sort_direction}").distinct diff --git a/app/services/api/v1/issues/update_service.rb b/app/services/api/v1/issues/update_service.rb index 9f6a1f736..6a472a9fc 100644 --- a/app/services/api/v1/issues/update_service.rb +++ b/app/services/api/v1/issues/update_service.rb @@ -57,7 +57,7 @@ class Api::V1::Issues::UpdateService < ApplicationService build_atme_participants if @atme_receivers.present? @updated_issue.assigners = @assigners || User.none unless assigner_ids.nil? @updated_issue.attachments = @attachments || Attachment.none unless attachment_ids.nil? - @updated_issue.issue_tags = @issue_tags || IssueTag.none unless issue_tag_ids.nil? + @updated_issue.issue_tags_relates.destroy_all & @updated_issue.issue_tags = @issue_tags || IssueTag.none unless issue_tag_ids.nil? @updated_issue.issue_tags_value = @issue_tags.order("id asc").pluck(:id).join(",") unless issue_tag_ids.nil? @updated_issue.updated_on = Time.now diff --git a/app/views/api/v1/issues/_detail.json.jbuilder b/app/views/api/v1/issues/_detail.json.jbuilder index a39cdde9e..193357ce1 100644 --- a/app/views/api/v1/issues/_detail.json.jbuilder +++ b/app/views/api/v1/issues/_detail.json.jbuilder @@ -1,7 +1,7 @@ json.(issue, :id, :subject, :project_issues_index, :description, :branch_name, :start_date, :due_date) json.created_at issue.created_on.strftime("%Y-%m-%d %H:%M") json.updated_at issue.updated_on.strftime("%Y-%m-%d %H:%M") -json.tags issue.issue_tags.each do |tag| +json.tags issue.show_issue_tags.each do |tag| json.partial! "api/v1/issues/issue_tags/simple_detail", locals: {tag: tag} end json.status do diff --git a/lib/tasks/upgrade_issue_generate_data.rake b/lib/tasks/upgrade_issue_generate_data.rake index 6d016a653..34a266f25 100644 --- a/lib/tasks/upgrade_issue_generate_data.rake +++ b/lib/tasks/upgrade_issue_generate_data.rake @@ -30,8 +30,7 @@ namespace :upgrade_issue_generate_data do IssuePriority.init_data IssueStatus.init_data IssueTag.order(created_at: :desc).find_each do |it| - it.update_column(:issues_count, it.issue_issues.size) - it.update_column(:pull_requests_count, it.pull_request_issues.size) + it.reset_counter_field end IssueTag.where(user_id: nil).destroy_all count = 0