2016-02-20 13:59:59 +00:00
|
|
|
class Todo < ActiveRecord::Base
|
2016-03-08 18:22:50 +00:00
|
|
|
ASSIGNED = 1
|
|
|
|
MENTIONED = 2
|
|
|
|
BUILD_FAILED = 3
|
2016-02-12 18:45:44 +00:00
|
|
|
|
2016-02-12 13:17:42 +00:00
|
|
|
belongs_to :author, class_name: "User"
|
2016-02-17 19:45:32 +00:00
|
|
|
belongs_to :note
|
2016-02-12 13:17:42 +00:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :target, polymorphic: true, touch: true
|
|
|
|
belongs_to :user
|
|
|
|
|
2016-02-12 18:45:44 +00:00
|
|
|
delegate :name, :email, to: :author, prefix: true, allow_nil: true
|
|
|
|
|
2016-03-16 23:31:30 +00:00
|
|
|
validates :action, :project, :target_type, :user, presence: true
|
2016-03-18 13:27:26 +00:00
|
|
|
validates :target_id, presence: true, unless: :for_commit?
|
|
|
|
validates :commit_id, presence: true, if: :for_commit?
|
2016-02-12 13:17:42 +00:00
|
|
|
|
2016-02-12 18:45:44 +00:00
|
|
|
default_scope { reorder(id: :desc) }
|
|
|
|
|
|
|
|
scope :pending, -> { with_state(:pending) }
|
|
|
|
scope :done, -> { with_state(:done) }
|
|
|
|
|
2016-02-12 13:17:42 +00:00
|
|
|
state_machine :state, initial: :pending do
|
2016-02-15 19:49:28 +00:00
|
|
|
event :done do
|
2016-03-16 23:56:23 +00:00
|
|
|
transition [:pending] => :done
|
2016-02-15 19:49:28 +00:00
|
|
|
end
|
|
|
|
|
2016-02-12 13:17:42 +00:00
|
|
|
state :pending
|
|
|
|
state :done
|
|
|
|
end
|
2016-02-12 18:45:44 +00:00
|
|
|
|
2016-03-08 18:22:50 +00:00
|
|
|
def build_failed?
|
|
|
|
action == BUILD_FAILED
|
|
|
|
end
|
|
|
|
|
2016-02-18 19:16:39 +00:00
|
|
|
def body
|
|
|
|
if note.present?
|
|
|
|
note.note
|
|
|
|
else
|
|
|
|
target.title
|
|
|
|
end
|
2016-02-17 19:45:32 +00:00
|
|
|
end
|
2016-03-16 23:31:30 +00:00
|
|
|
|
|
|
|
def for_commit?
|
|
|
|
target_type == "Commit"
|
|
|
|
end
|
|
|
|
|
|
|
|
# override to return commits, which are not active record
|
|
|
|
def target
|
|
|
|
if for_commit?
|
2016-03-18 16:27:27 +00:00
|
|
|
project.commit(commit_id) rescue nil
|
2016-03-16 23:31:30 +00:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-18 16:24:47 +00:00
|
|
|
def target_reference
|
2016-03-16 23:31:30 +00:00
|
|
|
if for_commit?
|
2016-03-18 13:32:22 +00:00
|
|
|
target.short_id
|
2016-03-16 23:31:30 +00:00
|
|
|
else
|
|
|
|
target.to_reference
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 13:17:42 +00:00
|
|
|
end
|