2016-09-13 17:45:13 +05:30
|
|
|
class List < ActiveRecord::Base
|
|
|
|
belongs_to :board
|
|
|
|
belongs_to :label
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
enum list_type: { backlog: 0, label: 1, closed: 2 }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
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: :label?
|
|
|
|
|
|
|
|
before_destroy :can_be_destroyed
|
|
|
|
|
|
|
|
scope :destroyable, -> { where(list_type: list_types[:label]) }
|
|
|
|
scope :movable, -> { where(list_type: list_types[:label]) }
|
|
|
|
|
|
|
|
def destroyable?
|
|
|
|
label?
|
|
|
|
end
|
|
|
|
|
|
|
|
def movable?
|
|
|
|
label?
|
|
|
|
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,
|
|
|
|
only: [:id, :title, :description, :color]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def can_be_destroyed
|
|
|
|
destroyable?
|
|
|
|
end
|
|
|
|
end
|