2016-02-20 13:59:59 +00:00
|
|
|
class Todo < ActiveRecord::Base
|
2016-07-26 21:21:20 +00:00
|
|
|
include Sortable
|
|
|
|
|
2016-12-05 12:12:22 +00:00
|
|
|
ASSIGNED = 1
|
|
|
|
MENTIONED = 2
|
|
|
|
BUILD_FAILED = 3
|
|
|
|
MARKED = 4
|
|
|
|
APPROVAL_REQUIRED = 5 # This is an EE-only feature
|
|
|
|
UNMERGEABLE = 6
|
|
|
|
DIRECTLY_ADDRESSED = 7
|
2016-02-12 18:45:44 +00:00
|
|
|
|
2016-06-15 11:20:30 +00:00
|
|
|
ACTION_NAMES = {
|
|
|
|
ASSIGNED => :assigned,
|
|
|
|
MENTIONED => :mentioned,
|
|
|
|
BUILD_FAILED => :build_failed,
|
2016-07-11 10:02:11 +00:00
|
|
|
MARKED => :marked,
|
2016-12-14 14:37:31 +00:00
|
|
|
APPROVAL_REQUIRED => :approval_required,
|
2016-12-05 12:12:22 +00:00
|
|
|
UNMERGEABLE => :unmergeable,
|
|
|
|
DIRECTLY_ADDRESSED => :directly_addressed
|
2017-02-21 23:32:18 +00:00
|
|
|
}.freeze
|
2016-06-15 11:20:30 +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-07-03 23:58:58 +00:00
|
|
|
after_save :keep_around_commit
|
|
|
|
|
2016-07-26 21:21:20 +00:00
|
|
|
class << self
|
2017-03-10 11:10:48 +00:00
|
|
|
# Priority sorting isn't displayed in the dropdown, because we don't show
|
|
|
|
# milestones, but still show something if the user has a URL with that
|
|
|
|
# selected.
|
2016-07-26 21:21:20 +00:00
|
|
|
def sort(method)
|
2017-03-10 11:10:48 +00:00
|
|
|
case method.to_s
|
|
|
|
when 'priority', 'label_priority' then order_by_labels_priority
|
|
|
|
else order_by(method)
|
|
|
|
end
|
2016-07-26 21:21:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Order by priority depending on which issue/merge request the Todo belongs to
|
|
|
|
# Todos with highest priority first then oldest todos
|
|
|
|
# Need to order by created_at last because of differences on Mysql and Postgres when joining by type "Merge_request/Issue"
|
|
|
|
def order_by_labels_priority
|
2016-10-17 20:03:06 +00:00
|
|
|
params = {
|
2016-10-28 00:26:56 +00:00
|
|
|
target_type_column: "todos.target_type",
|
2016-10-17 20:03:06 +00:00
|
|
|
target_column: "todos.target_id",
|
|
|
|
project_column: "todos.project_id"
|
|
|
|
}
|
|
|
|
|
|
|
|
highest_priority = highest_label_priority(params).to_sql
|
2016-07-26 21:21:20 +00:00
|
|
|
|
2017-02-22 22:50:15 +00:00
|
|
|
select("#{table_name}.*, (#{highest_priority}) AS highest_priority").
|
|
|
|
order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC')).
|
|
|
|
order('todos.created_at')
|
2016-07-26 21:21:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-14 14:37:31 +00:00
|
|
|
def unmergeable?
|
|
|
|
action == UNMERGEABLE
|
|
|
|
end
|
|
|
|
|
2016-03-08 18:22:50 +00:00
|
|
|
def build_failed?
|
|
|
|
action == BUILD_FAILED
|
|
|
|
end
|
|
|
|
|
2017-04-10 15:51:15 +00:00
|
|
|
def assigned?
|
|
|
|
action == ASSIGNED
|
|
|
|
end
|
|
|
|
|
2016-06-15 11:20:30 +00:00
|
|
|
def action_name
|
|
|
|
ACTION_NAMES[action]
|
|
|
|
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?
|
2017-01-10 23:53:51 +00:00
|
|
|
target.reference_link_text(full: true)
|
2016-03-16 23:31:30 +00:00
|
|
|
else
|
2017-01-10 23:53:51 +00:00
|
|
|
target.to_reference(full: true)
|
2016-03-16 23:31:30 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-03 23:58:58 +00:00
|
|
|
|
2017-04-10 15:51:15 +00:00
|
|
|
def self_added?
|
|
|
|
author == user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self_assigned?
|
|
|
|
assigned? && self_added?
|
|
|
|
end
|
|
|
|
|
2016-07-03 23:58:58 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def keep_around_commit
|
|
|
|
project.repository.keep_around(self.commit_id)
|
|
|
|
end
|
2016-02-12 13:17:42 +00:00
|
|
|
end
|