gitlab-org--gitlab-foss/app/models/list.rb

60 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class List < ApplicationRecord
2016-07-27 15:32:32 -04:00
belongs_to :board
belongs_to :label
enum list_type: { backlog: 0, label: 1, closed: 2, assignee: 3, milestone: 4 }
2016-07-27 15:32:32 -04:00
validates :board, :list_type, presence: true
validates :label, :position, presence: true, if: :label?
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :movable?
before_destroy :can_be_destroyed
scope :destroyable, -> { where(list_type: list_types.slice(*destroyable_types).values) }
scope :movable, -> { where(list_type: list_types.slice(*movable_types).values) }
2018-09-10 13:26:33 -04:00
scope :preload_associations, -> { preload(:board, :label) }
class << self
def destroyable_types
[:label]
end
def movable_types
[:label]
end
end
def destroyable?
self.class.destroyable_types.include?(list_type&.to_sym)
end
def movable?
self.class.movable_types.include?(list_type&.to_sym)
end
def title
label? ? label.name : list_type.humanize
end
def as_json(options = {})
super(options).tap do |json|
if options.key?(:label)
json[:label] = label.as_json(
project: board.project,
only: [:id, :title, :description, :color],
methods: [:text_color]
)
end
end
end
private
def can_be_destroyed
throw(:abort) unless destroyable?
end
2016-07-27 15:32:32 -04:00
end