BigW Consortium Gitlab

todo.rb 3.16 KB
Newer Older
1
class Todo < ActiveRecord::Base
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 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
20
  }.freeze
Robert Schilling committed
21

22
  belongs_to :author, class_name: "User"
23
  belongs_to :note
24
  belongs_to :project
25
  belongs_to :target, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations
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 32
  validates :target_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
33

34 35 36 37 38
  default_scope { reorder(id: :desc) }

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

39
  state_machine :state, initial: :pending do
40
    event :done do
41
      transition [:pending] => :done
42 43
    end

44 45 46
    state :pending
    state :done
  end
47

48 49
  after_save :keep_around_commit

Felipe Artur committed
50
  class << self
51 52 53
    # 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.
Felipe Artur committed
54
    def sort(method)
55 56 57 58
      case method.to_s
      when 'priority', 'label_priority' then order_by_labels_priority
      else order_by(method)
      end
Felipe Artur committed
59 60 61 62 63 64
    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
65
      params = {
66
        target_type_column: "todos.target_type",
67 68 69 70 71
        target_column: "todos.target_id",
        project_column: "todos.project_id"
      }

      highest_priority = highest_label_priority(params).to_sql
Felipe Artur committed
72

73 74 75
      select("#{table_name}.*, (#{highest_priority}) AS highest_priority")
        .order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC'))
        .order('todos.created_at')
Felipe Artur committed
76 77 78
    end
  end

79 80 81 82
  def unmergeable?
    action == UNMERGEABLE
  end

83 84 85 86
  def build_failed?
    action == BUILD_FAILED
  end

87 88 89 90
  def assigned?
    action == ASSIGNED
  end

Robert Schilling committed
91 92 93 94
  def action_name
    ACTION_NAMES[action]
  end

95 96 97 98 99 100
  def body
    if note.present?
      note.note
    else
      target.title
    end
101
  end
102 103 104 105 106 107 108 109

  def for_commit?
    target_type == "Commit"
  end

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

116
  def target_reference
117
    if for_commit?
118
      target.reference_link_text(full: true)
119
    else
120
      target.to_reference(full: true)
121 122
    end
  end
123

124 125 126 127 128 129 130 131
  def self_added?
    author == user
  end

  def self_assigned?
    assigned? && self_added?
  end

132 133 134 135 136
  private

  def keep_around_commit
    project.repository.keep_around(self.commit_id)
  end
137
end