summaryrefslogtreecommitdiff
path: root/app/models/board.rb
blob: 7938819b6e4f1c33d7997b3a075735ed3b4c445d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

class Board < ApplicationRecord
  belongs_to :group
  belongs_to :project

  has_many :lists, -> { ordered }, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
  has_many :destroyable_lists, -> { destroyable.ordered }, class_name: "List"

  validates :name, presence: true
  validates :project, presence: true, if: :project_needed?
  validates :group, presence: true, unless: :project

  scope :with_associations, -> { preload(:destroyable_lists) }

  # 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) }
  scope :first_board, -> { where(id: self.order_by_name_asc.limit(1).select(:id)) }

  def project_needed?
    !group
  end

  def resource_parent
    @resource_parent ||= group || project
  end

  def group_board?
    group_id.present?
  end

  def project_board?
    project_id.present?
  end

  def scoped?
    false
  end

  def self.to_type
    name.demodulize
  end

  def to_type
    self.class.to_type
  end

  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
end

Board.prepend_mod_with('Board')