2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class List < ApplicationRecord
|
2021-03-08 18:12:59 +05:30
|
|
|
include Boards::Listable
|
2019-12-04 20:38:33 +05:30
|
|
|
include Importable
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
belongs_to :board
|
|
|
|
belongs_to :label
|
2019-12-04 20:38:33 +05:30
|
|
|
has_many :list_user_preferences
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
enum list_type: { backlog: 0, label: 1, closed: 2, assignee: 3, milestone: 4, iteration: 5 }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
validates :board, :list_type, presence: true, unless: :importing?
|
2016-09-13 17:45:13 +05:30
|
|
|
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
scope :preload_associated_models, -> { preload(:board, label: :priorities) }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
alias_method :preferences, :list_user_preferences
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
class << self
|
2019-12-21 20:55:43 +05:30
|
|
|
def preload_preferences_for_user(lists, user)
|
|
|
|
return unless user
|
|
|
|
|
|
|
|
lists.each { |list| list.preferences_for(user) }
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def preferences_for(user)
|
|
|
|
return preferences.build unless user
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
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-12-04 20:38:33 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
ListUserPreference.where(list_id: list_ids.uniq, user_id: user_ids.uniq).find_each do |preference|
|
|
|
|
loader.call({ list_id: preference.list_id, user_id: preference.user_id }, preference)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def update_preferences_for(user, preferences = {})
|
|
|
|
return unless user
|
|
|
|
|
|
|
|
preferences_for(user).update(preferences)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def collapsed?(user)
|
|
|
|
preferences = preferences_for(user)
|
|
|
|
|
|
|
|
preferences.collapsed?
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def as_json(options = {})
|
|
|
|
super(options).tap do |json|
|
2019-12-04 20:38:33 +05:30
|
|
|
json[:collapsed] = false
|
|
|
|
|
|
|
|
if options.key?(:collapsed)
|
2020-05-24 23:13:21 +05:30
|
|
|
json[:collapsed] = collapsed?(options[:current_user])
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
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,
|
2018-10-15 14:42:47 +05:30
|
|
|
only: [:id, :title, :description, :color],
|
|
|
|
methods: [:text_color]
|
2016-11-03 12:29:30 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
List.prepend_if_ee('::EE::List')
|