BigW Consortium Gitlab

todo.rb 1.61 KB
Newer Older
1 2
# == Schema Information
#
3
# Table name: todos
4 5 6 7
#
#  id          :integer          not null, primary key
#  user_id     :integer          not null
#  project_id  :integer          not null
8
#  target_id   :integer
9 10
#  target_type :string           not null
#  author_id   :integer
11
#  action      :integer          not null
12 13 14
#  state       :string           not null
#  created_at  :datetime
#  updated_at  :datetime
15 16
#  note_id     :integer
#  commit_id   :string
17 18
#

19
class Todo < ActiveRecord::Base
20
  ASSIGNED  = 1
21
  MENTIONED = 2
22

23
  belongs_to :author, class_name: "User"
24
  belongs_to :note
25 26 27 28
  belongs_to :project
  belongs_to :target, polymorphic: true, touch: true
  belongs_to :user

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

31
  validates :action, :project, :target_type, :user, presence: true
32 33
  validates :target_id, presence: true, unless: :for_commit?
  validates :commit_id, presence: true, if: :for_commit?
34

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

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

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

45 46 47
    state :pending
    state :done
  end
48

49 50 51 52 53 54
  def body
    if note.present?
      note.note
    else
      target.title
    end
55
  end
56 57 58 59 60 61 62 63

  def for_commit?
    target_type == "Commit"
  end

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

70
  def target_reference
71
    if for_commit?
72
      target.short_id
73 74 75 76
    else
      target.to_reference
    end
  end
77
end