2016-07-27 15:32:32 -04:00
|
|
|
class List < ActiveRecord::Base
|
|
|
|
belongs_to :board
|
|
|
|
belongs_to :label
|
|
|
|
|
2017-01-27 00:23:07 -05:00
|
|
|
enum list_type: { label: 1, done: 2 }
|
2016-07-27 15:32:32 -04:00
|
|
|
|
2016-07-28 18:26:16 -04:00
|
|
|
validates :board, :list_type, presence: true
|
|
|
|
validates :label, :position, presence: true, if: :label?
|
2016-08-03 13:21:22 -04:00
|
|
|
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
|
2016-07-28 18:26:16 -04:00
|
|
|
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :label?
|
2016-07-31 19:45:56 -04:00
|
|
|
|
2016-08-16 10:31:21 -04:00
|
|
|
before_destroy :can_be_destroyed
|
|
|
|
|
|
|
|
scope :destroyable, -> { where(list_type: list_types[:label]) }
|
2016-08-16 13:38:43 -04:00
|
|
|
scope :movable, -> { where(list_type: list_types[:label]) }
|
2016-08-16 10:31:21 -04:00
|
|
|
|
|
|
|
def destroyable?
|
|
|
|
label?
|
|
|
|
end
|
2016-07-31 19:48:00 -04:00
|
|
|
|
2016-08-16 13:38:43 -04:00
|
|
|
def movable?
|
|
|
|
label?
|
|
|
|
end
|
|
|
|
|
2016-07-31 19:45:56 -04:00
|
|
|
def title
|
2016-08-15 22:07:59 -04:00
|
|
|
label? ? label.name : list_type.humanize
|
2016-07-31 19:45:56 -04:00
|
|
|
end
|
2016-07-31 19:48:00 -04:00
|
|
|
|
2016-10-14 19:51:41 -04:00
|
|
|
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
|
|
|
|
|
2016-07-31 19:48:00 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def can_be_destroyed
|
2016-08-16 10:31:21 -04:00
|
|
|
destroyable?
|
2016-07-31 19:48:00 -04:00
|
|
|
end
|
2016-07-27 15:32:32 -04:00
|
|
|
end
|