diff --git a/app/models/activity.rb b/app/models/activity.rb index 67f0c137f..683639219 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -66,10 +66,10 @@ class Activity < ApplicationRecord validates :type_of, presence: true validates :project, :user, presence: true - belongs_to :project, inverse_of: :activities - belongs_to :experiment, inverse_of: :activities - belongs_to :my_module, inverse_of: :activities - belongs_to :user, inverse_of: :activities + belongs_to :project, inverse_of: :activities, optional: true + belongs_to :experiment, inverse_of: :activities, optional: true + belongs_to :my_module, inverse_of: :activities, optional: true + belongs_to :user, inverse_of: :activities, optional: true private diff --git a/app/models/asset.rb b/app/models/asset.rb index 0a9c7c752..7430bc473 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -44,11 +44,15 @@ class Asset < ApplicationRecord # assign it to result validate :step_or_result - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', - class_name: 'User' - belongs_to :team + class_name: 'User', + optional: true + belongs_to :team, optional: true has_one :step_asset, inverse_of: :asset, dependent: :destroy diff --git a/app/models/asset_text_datum.rb b/app/models/asset_text_datum.rb index 9f8db238a..e79d832fe 100644 --- a/app/models/asset_text_datum.rb +++ b/app/models/asset_text_datum.rb @@ -3,7 +3,7 @@ class AssetTextDatum < ApplicationRecord validates :data, presence: true validates :asset, presence: true, uniqueness: true - belongs_to :asset, inverse_of: :asset_text_datum + belongs_to :asset, inverse_of: :asset_text_datum, optional: true after_save :update_ts_index diff --git a/app/models/checklist.rb b/app/models/checklist.rb index 12f8e7cc0..ba4e2e433 100644 --- a/app/models/checklist.rb +++ b/app/models/checklist.rb @@ -7,9 +7,15 @@ class Checklist < ApplicationRecord length: { maximum: Constants::TEXT_MAX_LENGTH } validates :step, presence: true - belongs_to :step, inverse_of: :checklists - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' + belongs_to :step, inverse_of: :checklists, optional: true + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true has_many :checklist_items, -> { order(:position) }, inverse_of: :checklist, diff --git a/app/models/checklist_item.rb b/app/models/checklist_item.rb index e3fe7804e..bc9f7b4c3 100644 --- a/app/models/checklist_item.rb +++ b/app/models/checklist_item.rb @@ -6,7 +6,15 @@ class ChecklistItem < ApplicationRecord validates :checklist, presence: true validates :checked, inclusion: { in: [true, false] } - belongs_to :checklist, inverse_of: :checklist_items - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' + belongs_to :checklist, + inverse_of: :checklist_items, + optional: true + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true end diff --git a/app/models/comment.rb b/app/models/comment.rb index 63cca8c96..2925196a4 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -7,9 +7,11 @@ class Comment < ApplicationRecord length: { maximum: Constants::TEXT_MAX_LENGTH } validates :user, presence: true - belongs_to :user, inverse_of: :comments - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', - class_name: 'User' + belongs_to :user, inverse_of: :comments, optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true def self.search( user, diff --git a/app/models/connection.rb b/app/models/connection.rb index eaee3a1e6..53cc3b5cc 100644 --- a/app/models/connection.rb +++ b/app/models/connection.rb @@ -1,4 +1,12 @@ class Connection < ApplicationRecord - belongs_to :to, :class_name => 'MyModule', :foreign_key => 'input_id', inverse_of: :inputs - belongs_to :from, :class_name => 'MyModule', :foreign_key => 'output_id', inverse_of: :outputs + belongs_to :to, + class_name: 'MyModule', + foreign_key: 'input_id', + inverse_of: :inputs, + optional: true + belongs_to :from, + class_name: 'MyModule', + foreign_key: 'output_id', + inverse_of: :outputs, + optional: true end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 6d706617c..184863bb6 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -8,11 +8,12 @@ class CustomField < ApplicationRecord 'Sample group', 'Added on', 'Added by'] } validates :user, :team, presence: true - belongs_to :user, inverse_of: :custom_fields - belongs_to :team, inverse_of: :custom_fields + belongs_to :user, inverse_of: :custom_fields, optional: true + belongs_to :team, inverse_of: :custom_fields, optional: true belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', - class_name: 'User' + class_name: 'User', + optional: true has_many :sample_custom_fields, inverse_of: :custom_field, dependent: :destroy after_create :update_samples_table_state diff --git a/app/models/experiment.rb b/app/models/experiment.rb index e1301c790..3df1cfc85 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -2,12 +2,21 @@ class Experiment < ApplicationRecord include ArchivableModel include SearchableModel - belongs_to :project, inverse_of: :experiments - belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' - belongs_to :last_modified_by, foreign_key: :last_modified_by_id, - class_name: 'User' - belongs_to :archived_by, foreign_key: :archived_by_id, class_name: 'User' - belongs_to :restored_by, foreign_key: :restored_by_id, class_name: 'User' + belongs_to :project, inverse_of: :experiments, optional: true + belongs_to :created_by, + foreign_key: :created_by_id, + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: :last_modified_by_id, + class_name: 'User', + optional: true + belongs_to :archived_by, + foreign_key: :archived_by_id, class_name: 'User', optional: true + belongs_to :restored_by, + foreign_key: :restored_by_id, + class_name: 'User', + optional: true has_many :my_modules, inverse_of: :experiment, dependent: :destroy has_many :my_module_groups, inverse_of: :experiment, dependent: :destroy diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 7316c80c7..274702c66 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -14,29 +14,54 @@ class MyModule < ApplicationRecord validates :experiment, presence: true validates :my_module_group, presence: true, if: "!my_module_group_id.nil?" - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User' - belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User' - belongs_to :experiment, inverse_of: :my_modules - belongs_to :my_module_group, inverse_of: :my_modules - has_many :results, inverse_of: :my_module, :dependent => :destroy - has_many :my_module_tags, inverse_of: :my_module, :dependent => :destroy + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :archived_by, + foreign_key: 'archived_by_id', + class_name: 'User', + optional: true + belongs_to :restored_by, + foreign_key: 'restored_by_id', + class_name: 'User', + optional: true + belongs_to :experiment, inverse_of: :my_modules, optional: true + belongs_to :my_module_group, inverse_of: :my_modules, optional: true + has_many :results, inverse_of: :my_module, dependent: :destroy + has_many :my_module_tags, inverse_of: :my_module, dependent: :destroy has_many :tags, through: :my_module_tags has_many :task_comments, foreign_key: :associated_id, dependent: :destroy - has_many :inputs, :class_name => 'Connection', :foreign_key => "input_id", inverse_of: :to, :dependent => :destroy - has_many :outputs, :class_name => 'Connection', :foreign_key => "output_id", inverse_of: :from, :dependent => :destroy + has_many :inputs, + class_name: 'Connection', + foreign_key: 'input_id', + inverse_of: :to, + dependent: :destroy + has_many :outputs, + class_name: 'Connection', + foreign_key: 'output_id', + inverse_of: :from, + dependent: :destroy has_many :my_modules, through: :outputs, source: :to - has_many :my_module_antecessors, through: :inputs, source: :from, class_name: 'MyModule' - has_many :sample_my_modules, inverse_of: :my_module, :dependent => :destroy + has_many :my_module_antecessors, + through: :inputs, + source: :from, + class_name: 'MyModule' + has_many :sample_my_modules, + inverse_of: :my_module, + dependent: :destroy has_many :samples, through: :sample_my_modules has_many :my_module_repository_rows, inverse_of: :my_module, dependent: :destroy has_many :repository_rows, through: :my_module_repository_rows - has_many :user_my_modules, inverse_of: :my_module, :dependent => :destroy + has_many :user_my_modules, inverse_of: :my_module, dependent: :destroy has_many :users, through: :user_my_modules has_many :activities, inverse_of: :my_module - has_many :report_elements, inverse_of: :my_module, :dependent => :destroy + has_many :report_elements, inverse_of: :my_module, dependent: :destroy has_many :protocols, inverse_of: :my_module, dependent: :destroy scope :is_archived, ->(is_archived) { where('archived = ?', is_archived) } diff --git a/app/models/my_module_group.rb b/app/models/my_module_group.rb index e025da745..251229da9 100644 --- a/app/models/my_module_group.rb +++ b/app/models/my_module_group.rb @@ -7,8 +7,11 @@ class MyModuleGroup < ApplicationRecord length: { maximum: Constants::NAME_MAX_LENGTH } validates :experiment, presence: true - belongs_to :experiment, inverse_of: :my_module_groups - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' + belongs_to :experiment, inverse_of: :my_module_groups, optional: true + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true has_many :my_modules, inverse_of: :my_module_group, dependent: :nullify def self.search(user, diff --git a/app/models/my_module_repository_row.rb b/app/models/my_module_repository_row.rb index 068fefca7..b03d754ae 100644 --- a/app/models/my_module_repository_row.rb +++ b/app/models/my_module_repository_row.rb @@ -1,7 +1,10 @@ class MyModuleRepositoryRow < ApplicationRecord - belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User' - belongs_to :repository_row - belongs_to :my_module + belongs_to :assigned_by, + foreign_key: 'assigned_by_id', + class_name: 'User', + optional: true + belongs_to :repository_row, optional: true + belongs_to :my_module, optional: true validates :repository_row, :my_module, presence: true validates :repository_row, uniqueness: { scope: :my_module } diff --git a/app/models/my_module_tag.rb b/app/models/my_module_tag.rb index 733cdb027..9dac83c2e 100644 --- a/app/models/my_module_tag.rb +++ b/app/models/my_module_tag.rb @@ -2,7 +2,10 @@ class MyModuleTag < ApplicationRecord validates :my_module, :tag, presence: true validates :tag_id, uniqueness: { scope: :my_module_id } - belongs_to :my_module, inverse_of: :my_module_tags - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :tag, inverse_of: :my_module_tags + belongs_to :my_module, inverse_of: :my_module_tags, optional: true + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :tag, inverse_of: :my_module_tags, optional: true end diff --git a/app/models/notification.rb b/app/models/notification.rb index 5e51539f6..50e1b5eb3 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,7 +1,7 @@ class Notification < ApplicationRecord has_many :user_notifications, inverse_of: :notification, dependent: :destroy has_many :users, through: :user_notifications - belongs_to :generator_user, class_name: 'User' + belongs_to :generator_user, class_name: 'User', optional: true enum type_of: Extends::NOTIFICATIONS_TYPES diff --git a/app/models/project.rb b/app/models/project.rb index 05bae2dfd..3fcc8b0c3 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -12,10 +12,23 @@ class Project < ApplicationRecord validates :visibility, presence: true validates :team, presence: true - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User' - belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User' + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :archived_by, + foreign_key: 'archived_by_id', + class_name: 'User', + optional: true + belongs_to :restored_by, + foreign_key: 'restored_by_id', + class_name: 'User', + optional: true + belongs_to :team, inverse_of: :projects, optional: true has_many :user_projects, inverse_of: :project has_many :users, through: :user_projects has_many :experiments, inverse_of: :project @@ -24,7 +37,6 @@ class Project < ApplicationRecord has_many :tags, inverse_of: :project has_many :reports, inverse_of: :project, dependent: :destroy has_many :report_elements, inverse_of: :project, dependent: :destroy - belongs_to :team, inverse_of: :projects def self.search( user, diff --git a/app/models/project_comment.rb b/app/models/project_comment.rb index bf9afbb9f..c625176b0 100644 --- a/app/models/project_comment.rb +++ b/app/models/project_comment.rb @@ -1,6 +1,8 @@ class ProjectComment < Comment - belongs_to :project, foreign_key: :associated_id, - inverse_of: :project_comments + belongs_to :project, + foreign_key: :associated_id, + inverse_of: :project_comments, + optional: true validates :project, presence: true end diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 316e47fb4..148333707 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -85,18 +85,24 @@ class Protocol < ApplicationRecord belongs_to :added_by, foreign_key: 'added_by_id', class_name: 'User', - inverse_of: :added_protocols - belongs_to :my_module, inverse_of: :protocols - belongs_to :team, inverse_of: :protocols - belongs_to :parent, foreign_key: 'parent_id', class_name: 'Protocol' + inverse_of: :added_protocols, + optional: true + belongs_to :my_module, + inverse_of: :protocols, + optional: true + belongs_to :team, inverse_of: :protocols, optional: true + belongs_to :parent, + foreign_key: 'parent_id', + class_name: 'Protocol', + optional: true belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User', - inverse_of: :archived_protocols + inverse_of: :archived_protocols, optional: true belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User', - inverse_of: :restored_protocols + inverse_of: :restored_protocols, optional: true has_many :linked_children, class_name: 'Protocol', foreign_key: 'parent_id' diff --git a/app/models/protocol_keyword.rb b/app/models/protocol_keyword.rb index 951839f89..fbcf9ae07 100644 --- a/app/models/protocol_keyword.rb +++ b/app/models/protocol_keyword.rb @@ -5,7 +5,7 @@ class ProtocolKeyword < ApplicationRecord maximum: Constants::NAME_MAX_LENGTH } validates :team, presence: true - belongs_to :team, inverse_of: :protocol_keywords + belongs_to :team, inverse_of: :protocol_keywords, optional: true has_many :protocol_protocol_keywords, inverse_of: :protocol_keyword, diff --git a/app/models/protocol_protocol_keyword.rb b/app/models/protocol_protocol_keyword.rb index 44256d43b..f9b96d262 100644 --- a/app/models/protocol_protocol_keyword.rb +++ b/app/models/protocol_protocol_keyword.rb @@ -5,8 +5,10 @@ class ProtocolProtocolKeyword < ApplicationRecord validates :protocol, presence: true validates :protocol_keyword, presence: true - belongs_to :protocol, inverse_of: :protocol_protocol_keywords - belongs_to :protocol_keyword, inverse_of: :protocol_protocol_keywords + belongs_to :protocol, inverse_of: :protocol_protocol_keywords, optional: true + belongs_to :protocol_keyword, + inverse_of: :protocol_protocol_keywords, + optional: true private diff --git a/app/models/report.rb b/app/models/report.rb index 2735a56fb..aa44da7a8 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -10,9 +10,12 @@ class Report < ApplicationRecord validates :project, presence: true validates :user, presence: true - belongs_to :project, inverse_of: :reports - belongs_to :user, inverse_of: :reports - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' + belongs_to :project, inverse_of: :reports, optional: true + belongs_to :user, inverse_of: :reports, optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true # Report either has many report elements (if grouped by timestamp), # or many module elements (if grouped by module) diff --git a/app/models/report_element.rb b/app/models/report_element.rb index 6b05b99a8..8ae6e2fe4 100644 --- a/app/models/report_element.rb +++ b/app/models/report_element.rb @@ -12,7 +12,7 @@ class ReportElement < ApplicationRecord validates :type_of, presence: true validate :has_one_of_referenced_elements - belongs_to :report, inverse_of: :report_elements + belongs_to :report, inverse_of: :report_elements, optional: true # Hierarchical structure representation has_many :children, @@ -20,18 +20,18 @@ class ReportElement < ApplicationRecord class_name: 'ReportElement', foreign_key: 'parent_id', dependent: :destroy - belongs_to :parent, class_name: 'ReportElement' + belongs_to :parent, class_name: 'ReportElement', optional: true # References to various report entities - belongs_to :project, inverse_of: :report_elements - belongs_to :experiment, inverse_of: :report_elements - belongs_to :my_module, inverse_of: :report_elements - belongs_to :step, inverse_of: :report_elements - belongs_to :result, inverse_of: :report_elements - belongs_to :checklist, inverse_of: :report_elements - belongs_to :asset, inverse_of: :report_elements - belongs_to :table, inverse_of: :report_elements - belongs_to :repository, inverse_of: :report_elements + belongs_to :project, inverse_of: :report_elements, optional: true + belongs_to :experiment, inverse_of: :report_elements, optional: true + belongs_to :my_module, inverse_of: :report_elements, optional: true + belongs_to :step, inverse_of: :report_elements, optional: true + belongs_to :result, inverse_of: :report_elements, optional: true + belongs_to :checklist, inverse_of: :report_elements, optional: true + belongs_to :asset, inverse_of: :report_elements, optional: true + belongs_to :table, inverse_of: :report_elements, optional: true + belongs_to :repository, inverse_of: :report_elements, optional: true def has_children? children.length > 0 diff --git a/app/models/repository.rb b/app/models/repository.rb index 533c1bfde..855bbc523 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1,6 +1,9 @@ class Repository < ApplicationRecord - belongs_to :team - belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' + belongs_to :team, optional: true + belongs_to :created_by, + foreign_key: :created_by_id, + class_name: 'User', + optional: true has_many :repository_columns has_many :repository_rows has_many :repository_table_states, diff --git a/app/models/repository_cell.rb b/app/models/repository_cell.rb index 62983bf70..a3c12dccc 100644 --- a/app/models/repository_cell.rb +++ b/app/models/repository_cell.rb @@ -1,7 +1,7 @@ class RepositoryCell < ApplicationRecord - belongs_to :repository_row - belongs_to :repository_column - belongs_to :value, polymorphic: true, dependent: :destroy + belongs_to :repository_row, optional: true + belongs_to :repository_column, optional: true + belongs_to :value, polymorphic: true, dependent: :destroy, optional: true validates :repository_column, presence: true validate :repository_column_data_type diff --git a/app/models/repository_column.rb b/app/models/repository_column.rb index 00aa90bf3..da33d6d8d 100644 --- a/app/models/repository_column.rb +++ b/app/models/repository_column.rb @@ -1,6 +1,9 @@ class RepositoryColumn < ApplicationRecord - belongs_to :repository - belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' + belongs_to :repository, optional: true + belongs_to :created_by, + foreign_key: :created_by_id, + class_name: 'User', + optional: true has_many :repository_cells, dependent: :destroy has_many :repository_rows, through: :repository_cells diff --git a/app/models/repository_date_value.rb b/app/models/repository_date_value.rb index 0d2e6e397..ed1609d83 100644 --- a/app/models/repository_date_value.rb +++ b/app/models/repository_date_value.rb @@ -1,7 +1,12 @@ class RepositoryDateValue < ApplicationRecord - belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' - belongs_to :last_modified_by, foreign_key: :last_modified_by_id, - class_name: 'User' + belongs_to :created_by, + foreign_key: :created_by_id, + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: :last_modified_by_id, + class_name: 'User', + optional: true has_one :repository_cell, as: :value, dependent: :destroy accepts_nested_attributes_for :repository_cell diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index bd04f98f8..aa727b893 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -1,8 +1,13 @@ class RepositoryRow < ApplicationRecord - belongs_to :repository - belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' - belongs_to :last_modified_by, foreign_key: :last_modified_by_id, - class_name: 'User' + belongs_to :repository, optional: true + belongs_to :created_by, + foreign_key: :created_by_id, + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: :last_modified_by_id, + class_name: 'User', + optional: true has_many :repository_cells, dependent: :destroy has_many :repository_columns, through: :repository_cells has_many :my_module_repository_rows, diff --git a/app/models/repository_table_state.rb b/app/models/repository_table_state.rb index 643dcd5e7..37a37e9c3 100644 --- a/app/models/repository_table_state.rb +++ b/app/models/repository_table_state.rb @@ -1,6 +1,6 @@ class RepositoryTableState < ApplicationRecord - belongs_to :user, inverse_of: :repository_table_states - belongs_to :repository, inverse_of: :repository_table_states + belongs_to :user, inverse_of: :repository_table_states, optional: true + belongs_to :repository, inverse_of: :repository_table_states, optional: true validates :user, :repository, presence: true diff --git a/app/models/repository_text_value.rb b/app/models/repository_text_value.rb index 699f1ed18..2d22744c7 100644 --- a/app/models/repository_text_value.rb +++ b/app/models/repository_text_value.rb @@ -1,7 +1,12 @@ class RepositoryTextValue < ApplicationRecord - belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' - belongs_to :last_modified_by, foreign_key: :last_modified_by_id, - class_name: 'User' + belongs_to :created_by, + foreign_key: :created_by_id, + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: :last_modified_by_id, + class_name: 'User', + optional: true has_one :repository_cell, as: :value, dependent: :destroy accepts_nested_attributes_for :repository_cell diff --git a/app/models/result.rb b/app/models/result.rb index 87e55efc0..5eff41d8c 100644 --- a/app/models/result.rb +++ b/app/models/result.rb @@ -6,11 +6,20 @@ class Result < ApplicationRecord validates :name, length: { maximum: Constants::NAME_MAX_LENGTH } validate :text_or_asset_or_table - belongs_to :user, inverse_of: :results - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User' - belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User' - belongs_to :my_module, inverse_of: :results + belongs_to :user, inverse_of: :results, optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :archived_by, + foreign_key: 'archived_by_id', + class_name: 'User', + optional: true + belongs_to :restored_by, + foreign_key: 'restored_by_id', + class_name: 'User', + optional: true + belongs_to :my_module, inverse_of: :results, optional: true has_one :result_asset, inverse_of: :result, dependent: :destroy diff --git a/app/models/result_asset.rb b/app/models/result_asset.rb index 020a554f8..53f2bcfb7 100644 --- a/app/models/result_asset.rb +++ b/app/models/result_asset.rb @@ -1,8 +1,11 @@ class ResultAsset < ApplicationRecord validates :result, :asset, presence: true - belongs_to :result, inverse_of: :result_asset - belongs_to :asset, inverse_of: :result_asset, dependent: :destroy + belongs_to :result, inverse_of: :result_asset, optional: true + belongs_to :asset, + inverse_of: :result_asset, + dependent: :destroy, + optional: true def space_taken asset.present? ? asset.estimated_size : 0 diff --git a/app/models/result_comment.rb b/app/models/result_comment.rb index d1a398cdd..d3d858aa2 100644 --- a/app/models/result_comment.rb +++ b/app/models/result_comment.rb @@ -1,5 +1,8 @@ class ResultComment < Comment - belongs_to :result, foreign_key: :associated_id, inverse_of: :result_comments + belongs_to :result, + foreign_key: :associated_id, + inverse_of: :result_comments, + optional: true validates :result, presence: true end diff --git a/app/models/result_table.rb b/app/models/result_table.rb index 215974f14..26902412d 100644 --- a/app/models/result_table.rb +++ b/app/models/result_table.rb @@ -1,6 +1,9 @@ class ResultTable < ApplicationRecord validates :result, :table, presence: true - belongs_to :result, inverse_of: :result_table - belongs_to :table, inverse_of: :result_table, dependent: :destroy + belongs_to :result, inverse_of: :result_table, optional: true + belongs_to :table, + inverse_of: :result_table, + dependent: :destroy, + optional: true end diff --git a/app/models/result_text.rb b/app/models/result_text.rb index a54ff9981..719c2c026 100644 --- a/app/models/result_text.rb +++ b/app/models/result_text.rb @@ -4,6 +4,6 @@ class ResultText < ApplicationRecord presence: true, length: { maximum: Constants::RICH_TEXT_MAX_LENGTH } validates :result, presence: true - belongs_to :result, inverse_of: :result_text + belongs_to :result, inverse_of: :result_text, optional: true has_many :tiny_mce_assets, inverse_of: :result_text, dependent: :destroy end diff --git a/app/models/sample.rb b/app/models/sample.rb index a5e98ee8d..42aa3f496 100644 --- a/app/models/sample.rb +++ b/app/models/sample.rb @@ -7,11 +7,14 @@ class Sample < ApplicationRecord length: { maximum: Constants::NAME_MAX_LENGTH } validates :user, :team, presence: true - belongs_to :user, inverse_of: :samples - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :team, inverse_of: :samples - belongs_to :sample_group, inverse_of: :samples - belongs_to :sample_type, inverse_of: :samples + belongs_to :user, inverse_of: :samples, optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :team, inverse_of: :samples, optional: true + belongs_to :sample_group, inverse_of: :samples, optional: true + belongs_to :sample_type, inverse_of: :samples, optional: true has_many :sample_my_modules, inverse_of: :sample, dependent: :destroy has_many :my_modules, through: :sample_my_modules has_many :sample_custom_fields, inverse_of: :sample, dependent: :destroy diff --git a/app/models/sample_custom_field.rb b/app/models/sample_custom_field.rb index 8610a9ac9..9e8e20886 100644 --- a/app/models/sample_custom_field.rb +++ b/app/models/sample_custom_field.rb @@ -7,6 +7,6 @@ class SampleCustomField < ApplicationRecord length: { maximum: Constants::NAME_MAX_LENGTH } validates :custom_field, :sample, presence: true - belongs_to :custom_field, inverse_of: :sample_custom_fields - belongs_to :sample, inverse_of: :sample_custom_fields + belongs_to :custom_field, inverse_of: :sample_custom_fields, optional: true + belongs_to :sample, inverse_of: :sample_custom_fields, optional: true end diff --git a/app/models/sample_group.rb b/app/models/sample_group.rb index b094d5999..5840b67df 100644 --- a/app/models/sample_group.rb +++ b/app/models/sample_group.rb @@ -11,11 +11,15 @@ class SampleGroup < ApplicationRecord length: { maximum: Constants::COLOR_MAX_LENGTH } validates :team, presence: true - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', - class_name: 'User' - belongs_to :team, inverse_of: :sample_groups + class_name: 'User', + optional: true + belongs_to :team, inverse_of: :sample_groups, optional: true has_many :samples, inverse_of: :sample_groups scope :sorted, -> { order(name: :asc) } diff --git a/app/models/sample_my_module.rb b/app/models/sample_my_module.rb index 0337cd7ae..5ab6b0868 100644 --- a/app/models/sample_my_module.rb +++ b/app/models/sample_my_module.rb @@ -5,13 +5,14 @@ class SampleMyModule < ApplicationRecord validates :sample, :my_module, presence: true # One sample can only be assigned once to a specific module - validates_uniqueness_of :sample_id, :scope => :my_module_id + validates_uniqueness_of :sample_id, scope: :my_module_id - belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User' - belongs_to :sample, - inverse_of: :sample_my_modules - belongs_to :my_module, - inverse_of: :sample_my_modules + belongs_to :assigned_by, + foreign_key: 'assigned_by_id', + class_name: 'User', + optional: true + belongs_to :sample, inverse_of: :sample_my_modules, optional: true + belongs_to :my_module, inverse_of: :sample_my_modules, optional: true def increment_nr_of_module_samples my_module.increment!(:nr_of_assigned_samples) diff --git a/app/models/sample_type.rb b/app/models/sample_type.rb index 302904cfe..9e7e60e00 100644 --- a/app/models/sample_type.rb +++ b/app/models/sample_type.rb @@ -8,11 +8,15 @@ class SampleType < ApplicationRecord uniqueness: { scope: :team, case_sensitive: false } validates :team, presence: true - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', - class_name: 'User' - belongs_to :team, inverse_of: :sample_types + class_name: 'User', + optional: true + belongs_to :team, inverse_of: :sample_types, optional: true has_many :samples, inverse_of: :sample_types scope :sorted, -> { order(name: :asc) } diff --git a/app/models/samples_table.rb b/app/models/samples_table.rb index 25e77cf0c..c33cc1883 100644 --- a/app/models/samples_table.rb +++ b/app/models/samples_table.rb @@ -1,8 +1,8 @@ class SamplesTable < ApplicationRecord validates :user, :team, presence: true - belongs_to :user, inverse_of: :samples_tables - belongs_to :team, inverse_of: :samples_tables + belongs_to :user, inverse_of: :samples_tables, optional: true + belongs_to :team, inverse_of: :samples_tables, optional: true scope :find_status, ->(user, team) { where(user: user, team: team).pluck(:status) } diff --git a/app/models/step.rb b/app/models/step.rb index bddb065f1..3f451a5b3 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -11,9 +11,14 @@ class Step < ApplicationRecord validates :user, :protocol, presence: true validates :completed_on, presence: true, if: "completed?" - belongs_to :user, inverse_of: :steps - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :protocol, inverse_of: :steps + belongs_to :user, inverse_of: :steps, optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :protocol, + inverse_of: :steps, + optional: true has_many :checklists, inverse_of: :step, dependent: :destroy has_many :step_comments, foreign_key: :associated_id, dependent: :destroy diff --git a/app/models/step_asset.rb b/app/models/step_asset.rb index a5627b33b..60dfdbd33 100644 --- a/app/models/step_asset.rb +++ b/app/models/step_asset.rb @@ -1,6 +1,11 @@ class StepAsset < ApplicationRecord validates :step, :asset, presence: true - belongs_to :step, inverse_of: :step_assets - belongs_to :asset, inverse_of: :step_asset, dependent: :destroy + belongs_to :step, + inverse_of: :step_assets, + optional: true + belongs_to :asset, + inverse_of: :step_asset, + dependent: :destroy, + optional: true end diff --git a/app/models/step_comment.rb b/app/models/step_comment.rb index 176c6e58e..bc13fd337 100644 --- a/app/models/step_comment.rb +++ b/app/models/step_comment.rb @@ -1,5 +1,8 @@ class StepComment < Comment - belongs_to :step, foreign_key: :associated_id, inverse_of: :step_comments + belongs_to :step, + foreign_key: :associated_id, + inverse_of: :step_comments, + optional: true validates :step, presence: true end diff --git a/app/models/step_table.rb b/app/models/step_table.rb index 7df22ed65..b6e4d5cbd 100644 --- a/app/models/step_table.rb +++ b/app/models/step_table.rb @@ -1,6 +1,6 @@ class StepTable < ApplicationRecord validates :step, :table, presence: true - belongs_to :step, inverse_of: :step_tables - belongs_to :table, inverse_of: :step_table + belongs_to :step, inverse_of: :step_tables, optional: true + belongs_to :table, inverse_of: :step_table, optional: true end diff --git a/app/models/table.rb b/app/models/table.rb index 8c9fab158..9109b5e35 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -8,9 +8,15 @@ class Table < ApplicationRecord presence: true, length: { maximum: Constants::TABLE_JSON_MAX_SIZE_MB.megabytes } - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :team + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :team, optional: true has_one :step_table, inverse_of: :table has_one :step, through: :step_table diff --git a/app/models/tag.rb b/app/models/tag.rb index 2e86e2d9d..a1b1089ee 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -10,10 +10,16 @@ class Tag < ApplicationRecord length: { maximum: Constants::COLOR_MAX_LENGTH } validates :project, presence: true - belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' - belongs_to :project - has_many :my_module_tags, inverse_of: :tag, :dependent => :destroy + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true + belongs_to :project, optional: true + has_many :my_module_tags, inverse_of: :tag, dependent: :destroy has_many :my_modules, through: :my_module_tags def self.search(user, diff --git a/app/models/task_comment.rb b/app/models/task_comment.rb index 2c45cb458..9b14773e7 100644 --- a/app/models/task_comment.rb +++ b/app/models/task_comment.rb @@ -1,5 +1,8 @@ class TaskComment < Comment - belongs_to :my_module, foreign_key: :associated_id, inverse_of: :task_comments + belongs_to :my_module, + foreign_key: :associated_id, + inverse_of: :task_comments, + optional: true validates :my_module, presence: true end diff --git a/app/models/team.rb b/app/models/team.rb index d0933ef5c..1e61f1ea8 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -12,8 +12,14 @@ class Team < ApplicationRecord validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } validates :space_taken, presence: true - belongs_to :created_by, :foreign_key => 'created_by_id', :class_name => 'User' - belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User' + belongs_to :created_by, + foreign_key: 'created_by_id', + class_name: 'User', + optional: true + belongs_to :last_modified_by, + foreign_key: 'last_modified_by_id', + class_name: 'User', + optional: true has_many :user_teams, inverse_of: :team, dependent: :destroy has_many :users, through: :user_teams has_many :samples, inverse_of: :team diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index 8867ebe28..34fb9fbf0 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -1,12 +1,12 @@ class TinyMceAsset < ApplicationRecord attr_accessor :reference - before_create :set_reference + before_create :set_reference, optional: true after_create :update_estimated_size after_destroy :release_team_space - belongs_to :team, inverse_of: :tiny_mce_assets - belongs_to :step, inverse_of: :tiny_mce_assets - belongs_to :result_text, inverse_of: :tiny_mce_assets + belongs_to :team, inverse_of: :tiny_mce_assets, optional: true + belongs_to :step, inverse_of: :tiny_mce_assets, optional: true + belongs_to :result_text, inverse_of: :tiny_mce_assets, optional: true has_attached_file :image, styles: { large: [Constants::LARGE_PIC_FORMAT, :jpg] }, convert_options: { large: '-quality 100 -strip' } diff --git a/app/models/token.rb b/app/models/token.rb index c56805729..f199ef9a7 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -1,8 +1,10 @@ class Token < ApplicationRecord + validates :token, presence: true + validates :ttl, presence: true - validates :token, presence: true - validates :ttl, presence: true - - belongs_to :user, foreign_key: 'user_id', class_name: 'User', inverse_of: :tokens - + belongs_to :user, + foreign_key: 'user_id', + class_name: 'User', + inverse_of: :tokens, + optional: true end diff --git a/app/models/user_my_module.rb b/app/models/user_my_module.rb index 4ae80c0aa..3fd70fe1d 100644 --- a/app/models/user_my_module.rb +++ b/app/models/user_my_module.rb @@ -1,7 +1,10 @@ class UserMyModule < ApplicationRecord validates :user, :my_module, presence: true - belongs_to :user, inverse_of: :user_my_modules - belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User' - belongs_to :my_module, inverse_of: :user_my_modules + belongs_to :user, inverse_of: :user_my_modules, optional: true + belongs_to :assigned_by, + foreign_key: 'assigned_by_id', + class_name: 'User', + optional: true + belongs_to :my_module, inverse_of: :user_my_modules, optional: true end diff --git a/app/models/user_notification.rb b/app/models/user_notification.rb index fd949d9e0..98593e5d7 100644 --- a/app/models/user_notification.rb +++ b/app/models/user_notification.rb @@ -1,8 +1,8 @@ class UserNotification < ApplicationRecord include NotificationsHelper - belongs_to :user - belongs_to :notification + belongs_to :user, optional: true + belongs_to :notification, optional: true after_save :send_email diff --git a/app/models/user_project.rb b/app/models/user_project.rb index 50a462bb5..6dd2879cf 100644 --- a/app/models/user_project.rb +++ b/app/models/user_project.rb @@ -5,9 +5,12 @@ class UserProject < ApplicationRecord validates :user, presence: true validates :project, presence: true - belongs_to :user, inverse_of: :user_projects - belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User' - belongs_to :project, inverse_of: :user_projects + belongs_to :user, inverse_of: :user_projects, optional: true + belongs_to :assigned_by, + foreign_key: 'assigned_by_id', + class_name: 'User', + optional: true + belongs_to :project, inverse_of: :user_projects, optional: true before_destroy :destroy_associations diff --git a/app/models/user_team.rb b/app/models/user_team.rb index ce0a3ca63..4d5605cf0 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -5,9 +5,12 @@ class UserTeam < ApplicationRecord validates :user, presence: true validates :team, presence: true - belongs_to :user, inverse_of: :user_teams - belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User' - belongs_to :team, inverse_of: :user_teams + belongs_to :user, inverse_of: :user_teams, optional: true + belongs_to :assigned_by, + foreign_key: 'assigned_by_id', + class_name: 'User', + optional: true + belongs_to :team, inverse_of: :user_teams, optional: true before_destroy :destroy_associations after_create :create_samples_table_state diff --git a/app/models/wopi_action.rb b/app/models/wopi_action.rb index d8b34109d..4fba8006a 100644 --- a/app/models/wopi_action.rb +++ b/app/models/wopi_action.rb @@ -1,5 +1,8 @@ class WopiAction < ApplicationRecord - belongs_to :wopi_app, foreign_key: 'wopi_app_id', class_name: 'WopiApp' + belongs_to :wopi_app, + foreign_key: 'wopi_app_id', + class_name: 'WopiApp', + optional: true validates :action, :extension, :urlsrc, :wopi_app, presence: true def self.find_action(extension, activity) diff --git a/app/models/wopi_app.rb b/app/models/wopi_app.rb index 615772302..08413bec9 100644 --- a/app/models/wopi_app.rb +++ b/app/models/wopi_app.rb @@ -1,7 +1,8 @@ class WopiApp < ApplicationRecord belongs_to :wopi_discovery, foreign_key: 'wopi_discovery_id', - class_name: 'WopiDiscovery' + class_name: 'WopiDiscovery', + optional: true has_many :wopi_actions, class_name: 'WopiAction', foreign_key: 'wopi_app_id', diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index 0c6115e1e..73f25fd8a 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -16,7 +16,7 @@ require 'fileutils' # end class ZipExport < ApplicationRecord - belongs_to :user + belongs_to :user, optional: true has_attached_file :zip_file validates_attachment :zip_file, content_type: { content_type: 'application/zip' } diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb index 2cdeb718e..7fdc8322f 100644 --- a/config/initializers/paperclip.rb +++ b/config/initializers/paperclip.rb @@ -23,6 +23,7 @@ if ENV['PAPERCLIP_STORAGE'] == "s3" url: ':s3_domain_url', path: '/:class/:attachment/:id_partition/:hash/:style/:filename', storage: :s3, + s3_region: ENV['AWS_REGION'], s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com", s3_protocol: 'https', s3_credentials: {