GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

todo.rb 1.25 KB
Newer Older
1
class Todo < ActiveRecord::Base
2 3 4
  ASSIGNED     = 1
  MENTIONED    = 2
  BUILD_FAILED = 3
5
  MARKED       = 4
6

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
7
  belongs_to :author, class_name: "User"
8
  belongs_to :note
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
9 10 11 12
  belongs_to :project
  belongs_to :target, polymorphic: true, touch: true
  belongs_to :user

13 14
  delegate :name, :email, to: :author, prefix: true, allow_nil: true

15
  validates :action, :project, :target_type, :user, presence: true
16 17
  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
18

19 20 21 22 23
  default_scope { reorder(id: :desc) }

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

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
24
  state_machine :state, initial: :pending do
25
    event :done do
26
      transition [:pending] => :done
27 28
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
29 30 31
    state :pending
    state :done
  end
32

33 34 35 36
  def build_failed?
    action == BUILD_FAILED
  end

37 38 39 40 41 42
  def body
    if note.present?
      note.note
    else
      target.title
    end
43
  end
44 45 46 47 48 49 50 51

  def for_commit?
    target_type == "Commit"
  end

  # override to return commits, which are not active record
  def target
    if for_commit?
52
      project.commit(commit_id) rescue nil
53 54 55 56 57
    else
      super
    end
  end

58
  def target_reference
59
    if for_commit?
60
      target.short_id
61 62 63 64
    else
      target.to_reference
    end
  end
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
65
end