Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Mark some belongs_to association as optional

Christophe Bliard96415fd16c5f
diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb
index 1d186a419df..a70407d48ea 100644
--- a/app/models/enumeration.rb
+++ b/app/models/enumeration.rb
@@ -29,7 +29,7 @@
class Enumeration < ApplicationRecord
default_scope { order("#{Enumeration.table_name}.position ASC") }
- belongs_to :project
+ belongs_to :project, optional: true
acts_as_list scope: 'type = \'#{type}\''
acts_as_tree order: 'position ASC'
diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb
index 2c269bce9c5..c02108865ac 100644
--- a/app/models/menu_item.rb
+++ b/app/models/menu_item.rb
@@ -27,7 +27,7 @@
#++
class MenuItem < ApplicationRecord
- belongs_to :parent, class_name: 'MenuItem'
+ belongs_to :parent, class_name: 'MenuItem', optional: true
has_many :children, -> {
order('id ASC')
}, class_name: 'MenuItem', dependent: :destroy, foreign_key: :parent_id
diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb
index 81b04adcdcf..d2c7c3221cc 100644
--- a/app/models/notification_setting.rb
+++ b/app/models/notification_setting.rb
@@ -57,7 +57,7 @@ class NotificationSetting < ApplicationRecord
]
end
- belongs_to :project
+ belongs_to :project, optional: true
belongs_to :user
include Scopes::Scoped
diff --git a/app/models/type.rb b/app/models/type.rb
index 2dce33cd1fa..b6a017d9fec 100644
--- a/app/models/type.rb
+++ b/app/models/type.rb
@@ -53,6 +53,7 @@ class ::Type < ApplicationRecord
association_foreign_key: 'custom_field_id'
belongs_to :color,
+ optional: true,
class_name: 'Color'
acts_as_list
diff --git a/app/models/user.rb b/app/models/user.rb
index ba92a09603f..e3c3a76d260 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -54,7 +54,7 @@ class User < Principal
inverse_of: :user
has_one :rss_token, class_name: '::Token::RSS', dependent: :destroy
has_one :api_token, class_name: '::Token::API', dependent: :destroy
- belongs_to :auth_source
+ belongs_to :auth_source, optional: true
# Authorized OAuth grants
has_many :oauth_grants,
@@ -547,6 +547,7 @@ class User < Principal
u.mail = ''
u.status = User.statuses[:active]
end).save
+
raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
end
anonymous_user
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 4ef83ce8ccb..2dc8133fa27 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -49,11 +49,11 @@ class WorkPackage < ApplicationRecord
belongs_to :type
belongs_to :status, class_name: 'Status'
belongs_to :author, class_name: 'User'
- belongs_to :assigned_to, class_name: 'Principal'
- belongs_to :responsible, class_name: 'Principal'
- belongs_to :version
+ belongs_to :assigned_to, class_name: 'Principal', optional: true
+ belongs_to :responsible, class_name: 'Principal', optional: true
+ belongs_to :version, optional: true
belongs_to :priority, class_name: 'IssuePriority'
- belongs_to :category, class_name: 'Category'
+ belongs_to :category, class_name: 'Category', optional: true
has_many :time_entries, dependent: :delete_all
diff --git a/app/models/work_packages/costs.rb b/app/models/work_packages/costs.rb
index 54e28cdd25d..ed7510ec82a 100644
--- a/app/models/work_packages/costs.rb
+++ b/app/models/work_packages/costs.rb
@@ -30,7 +30,7 @@ module WorkPackages::Costs
extend ActiveSupport::Concern
included do
- belongs_to :budget, inverse_of: :work_packages
+ belongs_to :budget, inverse_of: :work_packages, optional: true
has_many :cost_entries, dependent: :delete_all
# disabled for now, implements part of ticket blocking