2024-12-20 17:10:26 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2025-02-11 13:54:42 +01:00
|
|
|
class Type < ApplicationRecord
|
2017-03-20 14:22:14 +01:00
|
|
|
# Work Package attributes for this type
|
2021-01-22 09:14:21 -05:00
|
|
|
# and constraints to specific attributes (by plugins).
|
2017-03-20 14:22:14 +01:00
|
|
|
include ::Type::Attributes
|
|
|
|
|
include ::Type::AttributeGroups
|
|
|
|
|
|
2020-12-08 10:47:29 +01:00
|
|
|
include ::Scopes::Scoped
|
|
|
|
|
|
2025-06-27 16:14:20 +02:00
|
|
|
attribute :patterns, WorkPackageTypes::Patterns::CollectionType.new
|
2024-12-11 19:59:40 +01:00
|
|
|
|
2025-02-17 12:47:46 +01:00
|
|
|
store_attribute :pdf_export_templates_config, :export_templates_disabled, :json
|
|
|
|
|
store_attribute :pdf_export_templates_config, :export_templates_order, :json
|
2025-02-11 13:02:13 +01:00
|
|
|
|
2011-05-30 20:52:25 +02:00
|
|
|
before_destroy :check_integrity
|
2013-07-24 16:28:59 +02:00
|
|
|
|
2024-12-20 17:10:26 +01:00
|
|
|
belongs_to :color, optional: true, class_name: "Color"
|
|
|
|
|
|
2013-08-26 11:25:19 +02:00
|
|
|
has_many :work_packages
|
2014-11-03 21:10:39 +01:00
|
|
|
has_many :workflows, dependent: :delete_all do
|
2017-05-24 06:59:44 +02:00
|
|
|
def copy_from_type(source_type)
|
2013-07-18 16:17:02 +02:00
|
|
|
Workflow.copy(source_type, nil, proxy_association.owner, nil)
|
2008-03-15 08:27:38 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2008-01-16 21:27:48 +00:00
|
|
|
has_and_belongs_to_many :projects
|
2013-07-24 16:28:59 +02:00
|
|
|
|
|
|
|
|
has_and_belongs_to_many :custom_fields,
|
2014-11-03 21:10:39 +01:00
|
|
|
class_name: "WorkPackageCustomField",
|
|
|
|
|
join_table: "#{table_name_prefix}custom_fields_types#{table_name_suffix}",
|
|
|
|
|
association_foreign_key: "custom_field_id"
|
2013-07-24 16:28:59 +02:00
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
acts_as_list
|
|
|
|
|
|
2025-12-18 11:23:25 +01:00
|
|
|
validates :name,
|
|
|
|
|
presence: true,
|
2025-12-18 12:01:34 +01:00
|
|
|
uniqueness: { case_sensitive: false },
|
|
|
|
|
length: { maximum: 255 }
|
2013-07-24 16:28:59 +02:00
|
|
|
|
2021-02-05 10:53:44 +01:00
|
|
|
scopes :milestone
|
2020-12-08 10:47:29 +01:00
|
|
|
|
2015-06-28 21:55:26 +02:00
|
|
|
default_scope { order("position ASC") }
|
2013-07-24 16:28:59 +02:00
|
|
|
|
2024-12-11 19:59:40 +01:00
|
|
|
scope :without_standard, -> { where(is_standard: false).order(:position) }
|
2024-12-20 17:10:26 +01:00
|
|
|
scope :default, -> { where(is_default: true) }
|
2026-02-19 13:20:35 +01:00
|
|
|
scope :visible, ->(user = User.current) {
|
|
|
|
|
if user.allowed_in_any_project?(:view_work_packages) || user.allowed_in_any_project?(:manage_types)
|
|
|
|
|
all
|
|
|
|
|
else
|
|
|
|
|
none
|
|
|
|
|
end
|
|
|
|
|
}
|
2013-08-06 11:33:21 +02:00
|
|
|
|
2024-12-20 17:10:26 +01:00
|
|
|
delegate :to_s, to: :name
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2018-02-07 09:16:12 +01:00
|
|
|
def <=>(other)
|
|
|
|
|
name <=> other.name
|
2007-12-07 10:26:07 +00:00
|
|
|
end
|
|
|
|
|
|
2026-03-17 15:52:01 +01:00
|
|
|
def self.statuses(types, role: nil, tab: nil) # rubocop:disable Metrics/AbcSize
|
2013-10-01 13:09:41 +02:00
|
|
|
workflow_table, status_table = [Workflow, Status].map(&:arel_table)
|
2021-02-11 16:02:18 +01:00
|
|
|
old_id_subselect, new_id_subselect = %i[old_status_id new_status_id].map do |foreign_key|
|
2026-03-12 17:52:08 +01:00
|
|
|
subquery = workflow_table.project(workflow_table[foreign_key]).where(workflow_table[:type_id].in(types))
|
|
|
|
|
subquery = subquery.where(workflow_table[:role_id].eq(role.id)) if role
|
2026-03-17 15:52:01 +01:00
|
|
|
subquery = apply_tab_condition(subquery, workflow_table, tab) if tab
|
2026-03-12 17:52:08 +01:00
|
|
|
subquery
|
2018-02-07 09:16:12 +01:00
|
|
|
end
|
2013-10-01 13:09:41 +02:00
|
|
|
Status.where(status_table[:id].in(old_id_subselect).or(status_table[:id].in(new_id_subselect)))
|
2013-09-24 13:10:45 +02:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2026-03-17 15:52:01 +01:00
|
|
|
def self.apply_tab_condition(subquery, workflow_table, tab)
|
|
|
|
|
case tab
|
|
|
|
|
when "author"
|
|
|
|
|
subquery.where(workflow_table[:author].eq(true))
|
|
|
|
|
when "assignee"
|
|
|
|
|
subquery.where(workflow_table[:assignee].eq(true))
|
|
|
|
|
else
|
|
|
|
|
subquery.where(workflow_table[:author].eq(false).and(workflow_table[:assignee].eq(false)))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-10-09 16:51:03 +02:00
|
|
|
def self.standard_type
|
2024-12-20 17:10:26 +01:00
|
|
|
where(is_standard: true).first
|
2014-01-29 11:53:44 +01:00
|
|
|
end
|
|
|
|
|
|
2016-03-23 15:42:39 +01:00
|
|
|
def self.enabled_in(project)
|
2024-12-20 17:10:26 +01:00
|
|
|
includes(:projects).where(projects: { id: project })
|
2016-03-23 15:42:39 +01:00
|
|
|
end
|
|
|
|
|
|
2026-03-17 15:52:01 +01:00
|
|
|
def statuses(include_default: false, role: nil, tab: nil)
|
2018-05-25 07:45:34 +02:00
|
|
|
if new_record?
|
|
|
|
|
Status.none
|
|
|
|
|
elsif include_default
|
2026-03-17 15:52:01 +01:00
|
|
|
self.class.statuses([id], role:, tab:).or(Status.where_default)
|
2018-05-22 08:38:24 +02:00
|
|
|
else
|
2026-03-17 15:52:01 +01:00
|
|
|
self.class.statuses([id], role:, tab:)
|
2018-05-22 08:38:24 +02:00
|
|
|
end
|
2009-12-18 15:41:32 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-24 16:28:59 +02:00
|
|
|
def enabled_in?(object)
|
2013-07-31 14:15:48 +02:00
|
|
|
object.types.include?(self)
|
2013-07-24 16:28:59 +02:00
|
|
|
end
|
|
|
|
|
|
2025-01-07 13:33:04 +01:00
|
|
|
def replacement_pattern_defined_for?(attribute)
|
|
|
|
|
enabled_patterns.key?(attribute)
|
2024-12-20 17:10:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def enabled_patterns
|
|
|
|
|
patterns.all_enabled
|
|
|
|
|
end
|
|
|
|
|
|
2025-02-17 12:47:46 +01:00
|
|
|
def pdf_export_templates
|
|
|
|
|
@pdf_export_templates ||= ::Type::PdfExportTemplates.new(self)
|
2025-02-11 13:02:13 +01:00
|
|
|
end
|
|
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
private
|
2014-01-03 08:34:06 +01:00
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
def check_integrity
|
2024-12-20 17:10:26 +01:00
|
|
|
throw :abort if is_standard?
|
|
|
|
|
throw :abort if WorkPackage.exists?(type_id: id)
|
|
|
|
|
|
|
|
|
|
true
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
|
|
|
|
end
|