2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Board < ApplicationRecord
|
2018-02-19 14:06:16 -05:00
|
|
|
belongs_to :group
|
2016-07-27 15:32:32 -04:00
|
|
|
belongs_to :project
|
|
|
|
|
2019-06-26 18:32:51 -04:00
|
|
|
has_many :lists, -> { ordered }, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
|
|
|
|
has_many :destroyable_lists, -> { destroyable.ordered }, class_name: "List"
|
2016-07-27 15:32:32 -04:00
|
|
|
|
2021-03-12 07:09:33 -05:00
|
|
|
validates :name, presence: true
|
2017-08-28 17:56:49 -04:00
|
|
|
validates :project, presence: true, if: :project_needed?
|
2018-02-19 14:06:16 -05:00
|
|
|
validates :group, presence: true, unless: :project
|
2017-08-28 17:56:49 -04:00
|
|
|
|
2019-06-13 18:20:03 -04:00
|
|
|
scope :with_associations, -> { preload(:destroyable_lists) }
|
2020-02-07 10:09:52 -05:00
|
|
|
|
|
|
|
# Sort by case-insensitive name, then ascending ids. This ensures that we will always
|
|
|
|
# get the same list/first board no matter how many other boards are named the same
|
|
|
|
scope :order_by_name_asc, -> { order(arel_table[:name].lower.asc).order(id: :asc) }
|
2020-01-17 10:08:37 -05:00
|
|
|
scope :first_board, -> { where(id: self.order_by_name_asc.limit(1).select(:id)) }
|
2019-06-13 18:20:03 -04:00
|
|
|
|
2017-08-28 17:56:49 -04:00
|
|
|
def project_needed?
|
2018-02-19 14:06:16 -05:00
|
|
|
!group
|
2017-08-28 17:56:49 -04:00
|
|
|
end
|
2016-09-26 13:39:41 -04:00
|
|
|
|
2019-10-17 08:07:33 -04:00
|
|
|
def resource_parent
|
|
|
|
@resource_parent ||= group || project
|
2017-08-31 15:34:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def group_board?
|
2018-02-19 14:06:16 -05:00
|
|
|
group_id.present?
|
2017-08-31 15:34:57 -04:00
|
|
|
end
|
|
|
|
|
2019-02-14 16:19:59 -05:00
|
|
|
def project_board?
|
|
|
|
project_id.present?
|
|
|
|
end
|
|
|
|
|
2018-06-29 06:51:48 -04:00
|
|
|
def scoped?
|
|
|
|
false
|
|
|
|
end
|
2021-02-12 04:08:48 -05:00
|
|
|
|
|
|
|
def self.to_type
|
|
|
|
name.demodulize
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_type
|
|
|
|
self.class.to_type
|
|
|
|
end
|
2021-05-17 14:10:42 -04:00
|
|
|
|
|
|
|
def disabled_for?(current_user)
|
|
|
|
namespace = group_board? ? resource_parent.root_ancestor : resource_parent.root_namespace
|
|
|
|
|
|
|
|
namespace.issue_repositioning_disabled? || !Ability.allowed?(current_user, :create_non_backlog_issues, self)
|
|
|
|
end
|
2016-07-27 15:32:32 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Board.prepend_mod_with('Board')
|