2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class List < ApplicationRecord
|
2019-08-28 16:18:40 -04:00
|
|
|
include Importable
|
|
|
|
|
2016-07-27 15:32:32 -04:00
|
|
|
belongs_to :board
|
|
|
|
belongs_to :label
|
2019-08-28 16:18:40 -04:00
|
|
|
has_many :list_user_preferences
|
2016-07-27 15:32:32 -04:00
|
|
|
|
2018-08-07 13:29:06 -04:00
|
|
|
enum list_type: { backlog: 0, label: 1, closed: 2, assignee: 3, milestone: 4 }
|
2016-07-27 15:32:32 -04:00
|
|
|
|
2019-08-02 15:35:10 -04:00
|
|
|
validates :board, :list_type, presence: true, unless: :importing?
|
2016-07-28 18:26:16 -04:00
|
|
|
validates :label, :position, presence: true, if: :label?
|
2016-08-03 13:21:22 -04:00
|
|
|
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
|
2018-06-07 16:54:24 -04:00
|
|
|
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :movable?
|
2016-07-31 19:45:56 -04:00
|
|
|
|
2016-08-16 10:31:21 -04:00
|
|
|
before_destroy :can_be_destroyed
|
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
scope :destroyable, -> { where(list_type: list_types.slice(*destroyable_types).values) }
|
|
|
|
scope :movable, -> { where(list_type: list_types.slice(*movable_types).values) }
|
2019-08-28 16:18:40 -04:00
|
|
|
|
|
|
|
scope :preload_associations, -> (user) do
|
|
|
|
preload(:board, label: :priorities)
|
|
|
|
.with_preferences_for(user)
|
|
|
|
end
|
|
|
|
|
2019-06-26 18:32:51 -04:00
|
|
|
scope :ordered, -> { order(:list_type, :position) }
|
2018-06-07 16:54:24 -04:00
|
|
|
|
2019-08-28 16:18:40 -04:00
|
|
|
# Loads list with preferences for given user
|
|
|
|
# if preferences exists for user or not
|
|
|
|
scope :with_preferences_for, -> (user) do
|
|
|
|
return unless user
|
|
|
|
|
|
|
|
includes(:list_user_preferences).where(list_user_preferences: { user_id: [user.id, nil] })
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :preferences, :list_user_preferences
|
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
class << self
|
|
|
|
def destroyable_types
|
|
|
|
[:label]
|
|
|
|
end
|
|
|
|
|
|
|
|
def movable_types
|
|
|
|
[:label]
|
|
|
|
end
|
|
|
|
end
|
2016-08-16 10:31:21 -04:00
|
|
|
|
2019-08-28 16:18:40 -04:00
|
|
|
def preferences_for(user)
|
|
|
|
return preferences.build unless user
|
|
|
|
|
|
|
|
if preferences.loaded?
|
|
|
|
preloaded_preferences_for(user)
|
|
|
|
else
|
|
|
|
preferences.find_or_initialize_by(user: user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def preloaded_preferences_for(user)
|
|
|
|
user_preferences =
|
|
|
|
preferences.find do |preference|
|
|
|
|
preference.user_id == user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
user_preferences || preferences.build(user: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_preferences_for(user, preferences = {})
|
|
|
|
return unless user
|
|
|
|
|
|
|
|
preferences_for(user).update(preferences)
|
|
|
|
end
|
|
|
|
|
2016-08-16 10:31:21 -04:00
|
|
|
def destroyable?
|
2018-08-07 13:29:06 -04:00
|
|
|
self.class.destroyable_types.include?(list_type&.to_sym)
|
2016-08-16 10:31:21 -04:00
|
|
|
end
|
2016-07-31 19:48:00 -04:00
|
|
|
|
2016-08-16 13:38:43 -04:00
|
|
|
def movable?
|
2018-08-07 13:29:06 -04:00
|
|
|
self.class.movable_types.include?(list_type&.to_sym)
|
2016-08-16 13:38:43 -04:00
|
|
|
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|
|
2019-08-28 16:18:40 -04:00
|
|
|
json[:collapsed] = false
|
|
|
|
|
|
|
|
if options.key?(:collapsed)
|
|
|
|
preferences = preferences_for(options[:current_user])
|
|
|
|
|
|
|
|
json[:collapsed] = preferences.collapsed?
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if options.key?(:label)
|
2016-10-14 19:51:41 -04:00
|
|
|
json[:label] = label.as_json(
|
|
|
|
project: board.project,
|
2018-05-14 16:40:48 -04:00
|
|
|
only: [:id, :title, :description, :color],
|
|
|
|
methods: [:text_color]
|
2016-10-14 19:51:41 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-31 19:48:00 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def can_be_destroyed
|
2019-01-03 13:23:37 -05:00
|
|
|
throw(:abort) unless destroyable?
|
2016-07-31 19:48:00 -04:00
|
|
|
end
|
2016-07-27 15:32:32 -04:00
|
|
|
end
|