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

62 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class List < ApplicationRecord
2016-09-13 17:45:13 +05:30
belongs_to :board
belongs_to :label
2019-10-12 21:52:04 +05:30
include Importable
2016-09-13 17:45:13 +05:30
2018-11-18 11:00:15 +05:30
enum list_type: { backlog: 0, label: 1, closed: 2, assignee: 3, milestone: 4 }
2016-09-13 17:45:13 +05:30
2019-10-12 21:52:04 +05:30
validates :board, :list_type, presence: true, unless: :importing?
2016-09-13 17:45:13 +05:30
validates :label, :position, presence: true, if: :label?
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
2018-11-08 19:23:39 +05:30
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :movable?
2016-09-13 17:45:13 +05:30
before_destroy :can_be_destroyed
2018-11-08 19:23:39 +05:30
scope :destroyable, -> { where(list_type: list_types.slice(*destroyable_types).values) }
scope :movable, -> { where(list_type: list_types.slice(*movable_types).values) }
2018-12-13 13:39:08 +05:30
scope :preload_associations, -> { preload(:board, :label) }
2019-09-30 21:07:59 +05:30
scope :ordered, -> { order(:list_type, :position) }
2018-11-08 19:23:39 +05:30
class << self
def destroyable_types
[:label]
end
def movable_types
[:label]
end
end
2016-09-13 17:45:13 +05:30
def destroyable?
2018-11-18 11:00:15 +05:30
self.class.destroyable_types.include?(list_type&.to_sym)
2016-09-13 17:45:13 +05:30
end
def movable?
2018-11-18 11:00:15 +05:30
self.class.movable_types.include?(list_type&.to_sym)
2016-09-13 17:45:13 +05:30
end
def title
label? ? label.name : list_type.humanize
end
2016-11-03 12:29:30 +05:30
def as_json(options = {})
super(options).tap do |json|
2017-09-10 17:25:29 +05:30
if options.key?(:label)
2016-11-03 12:29:30 +05:30
json[:label] = label.as_json(
project: board.project,
2018-10-15 14:42:47 +05:30
only: [:id, :title, :description, :color],
methods: [:text_color]
2016-11-03 12:29:30 +05:30
)
end
end
end
2016-09-13 17:45:13 +05:30
private
def can_be_destroyed
2019-02-15 15:39:39 +05:30
throw(:abort) unless destroyable?
2016-09-13 17:45:13 +05:30
end
end