BigW Consortium Gitlab

list.rb 1008 Bytes
Newer Older
1 2 3 4
class List < ActiveRecord::Base
  belongs_to :board
  belongs_to :label

5
  enum list_type: { label: 1, closed: 2 }
6

7 8
  validates :board, :list_type, presence: true
  validates :label, :position, presence: true, if: :label?
9
  validates :label_id, uniqueness: { scope: :board_id }, if: :label?
10
  validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :label?
11

12 13 14
  before_destroy :can_be_destroyed

  scope :destroyable, -> { where(list_type: list_types[:label]) }
15
  scope :movable, -> { where(list_type: list_types[:label]) }
16 17 18 19

  def destroyable?
    label?
  end
20

21 22 23 24
  def movable?
    label?
  end

25
  def title
26
    label? ? label.name : list_type.humanize
27
  end
28

29 30 31 32 33 34 35 36 37 38 39
  def as_json(options = {})
    super(options).tap do |json|
      if options.has_key?(:label)
        json[:label] = label.as_json(
          project: board.project,
          only: [:id, :title, :description, :color]
        )
      end
    end
  end

40 41 42
  private

  def can_be_destroyed
43
    destroyable?
44
  end
45
end