debian-mirror-gitlab/app/models/todo.rb

65 lines
1.2 KiB
Ruby
Raw Normal View History

2016-04-02 18:10:28 +05:30
class Todo < ActiveRecord::Base
2016-06-02 11:05:42 +05:30
ASSIGNED = 1
MENTIONED = 2
BUILD_FAILED = 3
2016-04-02 18:10:28 +05:30
belongs_to :author, class_name: "User"
belongs_to :note
belongs_to :project
belongs_to :target, polymorphic: true, touch: true
belongs_to :user
delegate :name, :email, to: :author, prefix: true, allow_nil: true
2016-06-02 11:05:42 +05:30
validates :action, :project, :target_type, :user, presence: true
validates :target_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?
2016-04-02 18:10:28 +05:30
default_scope { reorder(id: :desc) }
scope :pending, -> { with_state(:pending) }
scope :done, -> { with_state(:done) }
state_machine :state, initial: :pending do
event :done do
2016-06-02 11:05:42 +05:30
transition [:pending] => :done
2016-04-02 18:10:28 +05:30
end
state :pending
state :done
end
2016-06-02 11:05:42 +05:30
def build_failed?
action == BUILD_FAILED
end
2016-04-02 18:10:28 +05:30
def body
if note.present?
note.note
else
target.title
end
end
2016-06-02 11:05:42 +05:30
def for_commit?
target_type == "Commit"
end
# override to return commits, which are not active record
def target
if for_commit?
project.commit(commit_id) rescue nil
else
super
end
end
def target_reference
if for_commit?
target.short_id
else
target.to_reference
end
end
2016-04-02 18:10:28 +05:30
end