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

150 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-04-02 18:10:28 +05:30
class Todo < ActiveRecord::Base
2016-09-13 17:45:13 +05:30
include Sortable
2017-08-17 22:00:37 +05:30
ASSIGNED = 1
MENTIONED = 2
BUILD_FAILED = 3
MARKED = 4
APPROVAL_REQUIRED = 5 # This is an EE-only feature
UNMERGEABLE = 6
DIRECTLY_ADDRESSED = 7
2016-08-24 12:49:21 +05:30
ACTION_NAMES = {
ASSIGNED => :assigned,
MENTIONED => :mentioned,
BUILD_FAILED => :build_failed,
MARKED => :marked,
2017-08-17 22:00:37 +05:30
APPROVAL_REQUIRED => :approval_required,
UNMERGEABLE => :unmergeable,
DIRECTLY_ADDRESSED => :directly_addressed
}.freeze
2016-04-02 18:10:28 +05:30
belongs_to :author, class_name: "User"
belongs_to :note
belongs_to :project
2018-11-18 11:00:15 +05:30
belongs_to :group
2017-09-10 17:25:29 +05:30
belongs_to :target, polymorphic: true, touch: true # rubocop:disable Cop/PolymorphicAssociations
2016-04-02 18:10:28 +05:30
belongs_to :user
delegate :name, :email, to: :author, prefix: true, allow_nil: true
2018-11-18 11:00:15 +05:30
validates :action, :target_type, :user, presence: true
2018-03-17 18:26:18 +05:30
validates :author, presence: true
2016-06-02 11:05:42 +05:30
validates :target_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?
2018-11-18 11:00:15 +05:30
validates :project, presence: true, unless: :group_id
validates :group, presence: true, unless: :project_id
2016-04-02 18:10:28 +05:30
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
2018-11-18 11:00:15 +05:30
after_save :keep_around_commit, if: :commit_id
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
class << self
2017-08-17 22:00:37 +05:30
# 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.
2018-05-09 12:01:36 +05:30
def sort_by_attribute(method)
2018-03-27 19:54:05 +05:30
sorted =
case method.to_s
when 'priority', 'label_priority' then order_by_labels_priority
else order_by(method)
end
# Break ties with the ID column for pagination
sorted.order(id: :desc)
2016-09-13 17:45:13 +05:30
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-11-03 12:29:30 +05:30
params = {
target_type_column: "todos.target_type",
target_column: "todos.target_id",
project_column: "todos.project_id"
}
highest_priority = highest_label_priority(params).to_sql
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
select("#{table_name}.*, (#{highest_priority}) AS highest_priority")
.order(Gitlab::Database.nulls_last_order('highest_priority', 'ASC'))
.order('todos.created_at')
2016-09-13 17:45:13 +05:30
end
end
2018-11-18 11:00:15 +05:30
def parent
project
end
2017-08-17 22:00:37 +05:30
def unmergeable?
action == UNMERGEABLE
end
2016-06-02 11:05:42 +05:30
def build_failed?
action == BUILD_FAILED
end
2017-08-17 22:00:37 +05:30
def assigned?
action == ASSIGNED
end
2016-08-24 12:49:21 +05:30
def action_name
ACTION_NAMES[action]
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?
2017-08-17 22:00:37 +05:30
target.reference_link_text(full: true)
2016-06-02 11:05:42 +05:30
else
2017-08-17 22:00:37 +05:30
target.to_reference(full: true)
2016-06-02 11:05:42 +05:30
end
end
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
def self_added?
author == user
end
def self_assigned?
assigned? && self_added?
end
2016-08-24 12:49:21 +05:30
private
def keep_around_commit
project.repository.keep_around(self.commit_id)
end
2016-04-02 18:10:28 +05:30
end