# == Schema Information
#
# Table name: issue_tags_relates
#
#  id           :integer          not null, primary key
#  issue_id     :integer
#  issue_tag_id :integer
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#
# Indexes
#
#  index_issue_tags_relates_on_issue_id_and_issue_tag_id  (issue_id,issue_tag_id)
#

class IssueTagsRelate < ApplicationRecord
  belongs_to :issue
  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
