todo.rb 3.27 KB
Newer Older
1
class Todo < ActiveRecord::Base
Felipe Artur's avatar
Felipe Artur committed
2 3
  include Sortable

4 5 6 7 8 9 10
  ASSIGNED           = 1
  MENTIONED          = 2
  BUILD_FAILED       = 3
  MARKED             = 4
  APPROVAL_REQUIRED  = 5 # This is an EE-only feature
  UNMERGEABLE        = 6
  DIRECTLY_ADDRESSED = 7
11

Robert Schilling's avatar
Robert Schilling committed
12 13 14 15
  ACTION_NAMES = {
    ASSIGNED => :assigned,
    MENTIONED => :mentioned,
    BUILD_FAILED => :build_failed,
16
    MARKED => :marked,
17
    APPROVAL_REQUIRED => :approval_required,
18 19
    UNMERGEABLE => :unmergeable,
    DIRECTLY_ADDRESSED => :directly_addressed
Douwe Maan's avatar
Douwe Maan committed
20
  }.freeze
Robert Schilling's avatar
Robert Schilling committed
21

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
22
  belongs_to :author, class_name: "User"
23
  belongs_to :note
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
24
  belongs_to :project
25
  belongs_to :target, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
26 27
  belongs_to :user

28 29
  delegate :name, :email, to: :author, prefix: true, allow_nil: true

30
  validates :action, :project, :target_type, :user, presence: true
31
  validates :author, presence: true
32 33
  validates :target_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
34

35 36 37
  scope :pending, -> { with_state(:pending) }
  scope :done, -> { with_state(:done) }

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
38
  state_machine :state, initial: :pending do
39
    event :done do
40
      transition [:pending] => :done
41 42
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
43 44 45
    state :pending
    state :done
  end
46

47 48
  after_save :keep_around_commit

Felipe Artur's avatar
Felipe Artur committed
49
  class << self
50 51 52
    # 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.
53
    def sort_by_attribute(method)
54 55 56 57 58 59 60 61
      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
62 63 64 65 66 67
    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
68
      params = {
69
        target_type_column: "todos.target_type",
70 71 72 73 74
        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
75

76 77 78
      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
79 80 81
    end
  end

82 83 84 85
  def unmergeable?
    action == UNMERGEABLE
  end

86 87 88 89
  def build_failed?
    action == BUILD_FAILED
  end

90 91 92 93
  def assigned?
    action == ASSIGNED
  end

Robert Schilling's avatar
Robert Schilling committed
94 95 96 97
  def action_name
    ACTION_NAMES[action]
  end

98 99 100 101 102 103
  def body
    if note.present?
      note.note
    else
      target.title
    end
104
  end
105 106 107 108 109 110 111 112

  def for_commit?
    target_type == "Commit"
  end

  # override to return commits, which are not active record
  def target
    if for_commit?
113
      project.commit(commit_id) rescue nil
114 115 116 117 118
    else
      super
    end
  end

119
  def target_reference
120
    if for_commit?
121
      target.reference_link_text(full: true)
122
    else
123
      target.to_reference(full: true)
124 125
    end
  end
126

127 128 129 130 131 132 133 134
  def self_added?
    author == user
  end

  def self_assigned?
    assigned? && self_added?
  end

135 136 137 138 139
  private

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