todo.rb 5.69 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Todo < ApplicationRecord
Felipe Artur's avatar
Felipe Artur committed
4
  include Sortable
5
  include FromUnion
Felipe Artur's avatar
Felipe Artur committed
6

7 8 9 10 11
  # Time to wait for todos being removed when not visible for user anymore.
  # Prevents TODOs being removed by mistake, for example, removing access from a user
  # and giving it back again.
  WAIT_FOR_DELETE    = 1.hour

12 13 14 15 16 17 18
  ASSIGNED           = 1
  MENTIONED          = 2
  BUILD_FAILED       = 3
  MARKED             = 4
  APPROVAL_REQUIRED  = 5 # This is an EE-only feature
  UNMERGEABLE        = 6
  DIRECTLY_ADDRESSED = 7
19

Robert Schilling's avatar
Robert Schilling committed
20 21 22 23
  ACTION_NAMES = {
    ASSIGNED => :assigned,
    MENTIONED => :mentioned,
    BUILD_FAILED => :build_failed,
24
    MARKED => :marked,
25
    APPROVAL_REQUIRED => :approval_required,
26 27
    UNMERGEABLE => :unmergeable,
    DIRECTLY_ADDRESSED => :directly_addressed
Douwe Maan's avatar
Douwe Maan committed
28
  }.freeze
Robert Schilling's avatar
Robert Schilling committed
29

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
30
  belongs_to :author, class_name: "User"
31
  belongs_to :note
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
32
  belongs_to :project
33
  belongs_to :group
34 35 36 37 38 39 40
  belongs_to :target, -> {
    if self.klass.respond_to?(:with_api_entity_associations)
      self.with_api_entity_associations
    else
      self
    end
  }, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations
41

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
42
  belongs_to :user
43
  belongs_to :issue, -> { where("target_type = 'Issue'") }, foreign_key: :target_id
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
44

45 46
  delegate :name, :email, to: :author, prefix: true, allow_nil: true

47
  validates :action, :target_type, :user, presence: true
48
  validates :author, presence: true
49 50
  validates :target_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
51 52
  validates :project, presence: true, unless: :group_id
  validates :group, presence: true, unless: :project_id
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
53

54 55
  scope :pending, -> { with_state(:pending) }
  scope :done, -> { with_state(:done) }
56 57 58 59 60
  scope :for_action, -> (action) { where(action: action) }
  scope :for_author, -> (author) { where(author: author) }
  scope :for_project, -> (project) { where(project: project) }
  scope :for_group, -> (group) { where(group: group) }
  scope :for_type, -> (type) { where(target_type: type) }
61 62
  scope :for_target, -> (id) { where(target_id: id) }
  scope :for_commit, -> (id) { where(commit_id: id) }
63
  scope :with_entity_associations, -> { preload(:target, :author, :note, group: :route, project: [:route, { namespace: :route }]) }
64
  scope :joins_issue_and_assignees, -> { left_joins(issue: :assignees) }
65

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
66
  state_machine :state, initial: :pending do
67
    event :done do
68
      transition [:pending] => :done
69 70
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
71 72 73
    state :pending
    state :done
  end
74

75
  after_save :keep_around_commit, if: :commit_id
76

Felipe Artur's avatar
Felipe Artur committed
77
  class << self
78 79 80 81 82 83 84 85 86 87 88 89 90 91
    # Returns all todos for the given group and its descendants.
    #
    # group - A `Group` to retrieve todos for.
    #
    # Returns an `ActiveRecord::Relation`.
    def for_group_and_descendants(group)
      groups = group.self_and_descendants

      from_union([
        for_project(Project.for_group(groups)),
        for_group(groups)
      ])
    end

92
    # Returns `true` if the current user has any todos for the given target with the optional given state.
93 94
    #
    # target - The value of the `target_type` column, such as `Issue`.
95 96 97
    # state - The value of the `state` column, such as `pending` or `done`.
    def any_for_target?(target, state = nil)
      state.nil? ? exists?(target: target) : exists?(target: target, state: state)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    end

    # Updates the state of a relation of todos to the new state.
    #
    # new_state - The new state of the todos.
    #
    # Returns an `Array` containing the IDs of the updated todos.
    def update_state(new_state)
      # Only update those that are not really on that state
      base = where.not(state: new_state).except(:order)
      ids = base.pluck(:id)

      base.update_all(state: new_state)

      ids
    end

115 116 117
    # Priority sorting isn't displayed in the dropdown, because we don't show
    # milestones, but still show something if the user has a URL with that
    # selected.
118
    def sort_by_attribute(method)
119 120 121 122 123 124 125 126
      sorted =
        case method.to_s
        when 'priority', 'label_priority' then order_by_labels_priority
        else order_by(method)
        end

      # Break ties with the ID column for pagination
      sorted.order(id: :desc)
Felipe Artur's avatar
Felipe Artur committed
127 128 129 130 131 132
    end

    # Order by priority depending on which issue/merge request the Todo belongs to
    # Todos with highest priority first then oldest todos
    # Need to order by created_at last because of differences on Mysql and Postgres when joining by type "Merge_request/Issue"
    def order_by_labels_priority
133
      params = {
134
        target_type_column: "todos.target_type",
135 136 137 138 139
        target_column: "todos.target_id",
        project_column: "todos.project_id"
      }

      highest_priority = highest_label_priority(params).to_sql
Felipe Artur's avatar
Felipe Artur committed
140

141 142 143
      select("#{table_name}.*, (#{highest_priority}) AS highest_priority")
        .order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC'))
        .order('todos.created_at')
Felipe Artur's avatar
Felipe Artur committed
144 145 146
    end
  end

147
  def resource_parent
148 149 150
    project
  end

151 152 153 154
  def unmergeable?
    action == UNMERGEABLE
  end

155 156 157 158
  def build_failed?
    action == BUILD_FAILED
  end

159 160 161 162
  def assigned?
    action == ASSIGNED
  end

Robert Schilling's avatar
Robert Schilling committed
163 164 165 166
  def action_name
    ACTION_NAMES[action]
  end

167 168 169 170 171 172
  def body
    if note.present?
      note.note
    else
      target.title
    end
173
  end
174 175 176 177 178 179 180 181

  def for_commit?
    target_type == "Commit"
  end

  # override to return commits, which are not active record
  def target
    if for_commit?
182
      project.commit(commit_id) rescue nil
183 184 185 186 187
    else
      super
    end
  end

188
  def target_reference
189
    if for_commit?
190
      target.reference_link_text
191
    else
192
      target.to_reference
193 194
    end
  end
195

196 197 198 199 200 201 202 203
  def self_added?
    author == user
  end

  def self_assigned?
    assigned? && self_added?
  end

204 205 206 207 208
  private

  def keep_around_commit
    project.repository.keep_around(self.commit_id)
  end
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
209
end
210 211

Todo.prepend_if_ee('EE::Todo')