BigW Consortium Gitlab

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

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

Robert Schilling committed
10 11 12 13
  ACTION_NAMES = {
    ASSIGNED => :assigned,
    MENTIONED => :mentioned,
    BUILD_FAILED => :build_failed,
14 15
    MARKED => :marked,
    APPROVAL_REQUIRED => :approval_required
Robert Schilling committed
16 17
  }

18
  belongs_to :author, class_name: "User"
19
  belongs_to :note
20 21 22 23
  belongs_to :project
  belongs_to :target, polymorphic: true, touch: true
  belongs_to :user

24 25
  delegate :name, :email, to: :author, prefix: true, allow_nil: true

26
  validates :action, :project, :target_type, :user, presence: true
27 28
  validates :target_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
29

30 31 32 33 34
  default_scope { reorder(id: :desc) }

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

35
  state_machine :state, initial: :pending do
36
    event :done do
37
      transition [:pending] => :done
38 39
    end

40 41 42
    state :pending
    state :done
  end
43

44 45
  after_save :keep_around_commit

Felipe Artur committed
46 47 48 49 50 51 52 53 54
  class << self
    def sort(method)
      method == "priority" ? order_by_labels_priority : order_by(method)
    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
55
      params = {
56
        target_type_column: "todos.target_type",
57 58 59 60 61
        target_column: "todos.target_id",
        project_column: "todos.project_id"
      }

      highest_priority = highest_label_priority(params).to_sql
Felipe Artur committed
62 63 64 65 66 67 68

      select("#{table_name}.*, (#{highest_priority}) AS highest_priority").
        order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC')).
        order('todos.created_at')
    end
  end

69 70 71 72
  def build_failed?
    action == BUILD_FAILED
  end

Robert Schilling committed
73 74 75 76
  def action_name
    ACTION_NAMES[action]
  end

77 78 79 80 81 82
  def body
    if note.present?
      note.note
    else
      target.title
    end
83
  end
84 85 86 87 88 89 90 91

  def for_commit?
    target_type == "Commit"
  end

  # override to return commits, which are not active record
  def target
    if for_commit?
92
      project.commit(commit_id) rescue nil
93 94 95 96 97
    else
      super
    end
  end

98
  def target_reference
99
    if for_commit?
100
      target.short_id
101 102 103 104
    else
      target.to_reference
    end
  end
105 106 107 108 109 110

  private

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