BigW Consortium Gitlab

todo.rb 1.68 KB
Newer Older
1
class Todo < ActiveRecord::Base
2 3 4 5 6
  ASSIGNED          = 1
  MENTIONED         = 2
  BUILD_FAILED      = 3
  MARKED            = 4
  APPROVAL_REQUIRED = 5 # This is an EE-only feature
7

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

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

22 23
  delegate :name, :email, to: :author, prefix: true, allow_nil: true

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

28 29 30 31 32
  default_scope { reorder(id: :desc) }

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

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

38 39 40
    state :pending
    state :done
  end
41

42 43
  after_save :keep_around_commit

44 45 46 47
  def build_failed?
    action == BUILD_FAILED
  end

Robert Schilling committed
48 49 50 51
  def action_name
    ACTION_NAMES[action]
  end

52 53 54 55 56 57
  def body
    if note.present?
      note.note
    else
      target.title
    end
58
  end
59 60 61 62 63 64 65 66

  def for_commit?
    target_type == "Commit"
  end

  # override to return commits, which are not active record
  def target
    if for_commit?
67
      project.commit(commit_id) rescue nil
68 69 70 71 72
    else
      super
    end
  end

73
  def target_reference
74
    if for_commit?
75
      target.short_id
76 77 78 79
    else
      target.to_reference
    end
  end
80 81 82 83 84 85

  private

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