2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class List < ApplicationRecord
|
2021-01-12 10:10:37 -05:00
|
|
|
include Boards::Listable
|
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
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
enum list_type: { backlog: 0, label: 1, closed: 2, assignee: 3, milestone: 4, iteration: 5 }
|
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-08-03 13:21:22 -04:00
|
|
|
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
|
2019-08-28 16:18:40 -04:00
|
|
|
|
2019-11-20 07:06:01 -05:00
|
|
|
scope :preload_associated_models, -> { preload(:board, label: :priorities) }
|
2019-08-28 16:18:40 -04:00
|
|
|
|
|
|
|
alias_method :preferences, :list_user_preferences
|
|
|
|
|
|
|
|
def preferences_for(user)
|
|
|
|
return preferences.build unless user
|
|
|
|
|
2019-09-27 14:06:20 -04:00
|
|
|
BatchLoader.for(list_id: id, user_id: user.id).batch(default_value: preferences.build(user: user)) do |items, loader|
|
|
|
|
list_ids = items.map { |i| i[:list_id] }
|
|
|
|
user_ids = items.map { |i| i[:user_id] }
|
2019-08-28 16:18:40 -04:00
|
|
|
|
2019-10-07 14:06:24 -04:00
|
|
|
ListUserPreference.where(list_id: list_ids.uniq, user_id: user_ids.uniq).find_each do |preference|
|
2019-09-27 14:06:20 -04:00
|
|
|
loader.call({ list_id: preference.list_id, user_id: preference.user_id }, preference)
|
2019-08-28 16:18:40 -04:00
|
|
|
end
|
2019-09-27 14:06:20 -04:00
|
|
|
end
|
2019-08-28 16:18:40 -04:00
|
|
|
end
|
|
|
|
|
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)
|
2020-04-21 11:21:10 -04:00
|
|
|
json[:collapsed] = collapsed?(options[:current_user])
|
2019-08-28 16:18:40 -04:00
|
|
|
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-27 15:32:32 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
List.prepend_mod_with('List')
|