2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-27 19:32:32 +00:00
|
|
|
class List < ActiveRecord::Base
|
|
|
|
belongs_to :board
|
|
|
|
belongs_to :label
|
|
|
|
|
2018-08-07 17:29:06 +00:00
|
|
|
enum list_type: { backlog: 0, label: 1, closed: 2, assignee: 3, milestone: 4 }
|
2016-07-27 19:32:32 +00:00
|
|
|
|
2016-07-28 22:26:16 +00:00
|
|
|
validates :board, :list_type, presence: true
|
|
|
|
validates :label, :position, presence: true, if: :label?
|
2016-08-03 17:21:22 +00:00
|
|
|
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
|
2018-06-07 20:54:24 +00:00
|
|
|
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :movable?
|
2016-07-31 23:45:56 +00:00
|
|
|
|
2016-08-16 14:31:21 +00:00
|
|
|
before_destroy :can_be_destroyed
|
|
|
|
|
2018-06-07 20:54:24 +00:00
|
|
|
scope :destroyable, -> { where(list_type: list_types.slice(*destroyable_types).values) }
|
|
|
|
scope :movable, -> { where(list_type: list_types.slice(*movable_types).values) }
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def destroyable_types
|
|
|
|
[:label]
|
|
|
|
end
|
|
|
|
|
|
|
|
def movable_types
|
|
|
|
[:label]
|
|
|
|
end
|
|
|
|
end
|
2016-08-16 14:31:21 +00:00
|
|
|
|
|
|
|
def destroyable?
|
2018-08-07 17:29:06 +00:00
|
|
|
self.class.destroyable_types.include?(list_type&.to_sym)
|
2016-08-16 14:31:21 +00:00
|
|
|
end
|
2016-07-31 23:48:00 +00:00
|
|
|
|
2016-08-16 17:38:43 +00:00
|
|
|
def movable?
|
2018-08-07 17:29:06 +00:00
|
|
|
self.class.movable_types.include?(list_type&.to_sym)
|
2016-08-16 17:38:43 +00:00
|
|
|
end
|
|
|
|
|
2016-07-31 23:45:56 +00:00
|
|
|
def title
|
2016-08-16 02:07:59 +00:00
|
|
|
label? ? label.name : list_type.humanize
|
2016-07-31 23:45:56 +00:00
|
|
|
end
|
2016-07-31 23:48:00 +00:00
|
|
|
|
2016-10-14 23:51:41 +00:00
|
|
|
def as_json(options = {})
|
|
|
|
super(options).tap do |json|
|
2017-06-02 17:11:26 +00:00
|
|
|
if options.key?(:label)
|
2016-10-14 23:51:41 +00:00
|
|
|
json[:label] = label.as_json(
|
|
|
|
project: board.project,
|
2018-05-14 20:40:48 +00:00
|
|
|
only: [:id, :title, :description, :color],
|
|
|
|
methods: [:text_color]
|
2016-10-14 23:51:41 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-31 23:48:00 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def can_be_destroyed
|
2016-08-16 14:31:21 +00:00
|
|
|
destroyable?
|
2016-07-31 23:48:00 +00:00
|
|
|
end
|
2016-07-27 19:32:32 +00:00
|
|
|
end
|