diff --git a/app/models/repository_base.rb b/app/models/repository_base.rb index e8e65f1e3..730731356 100644 --- a/app/models/repository_base.rb +++ b/app/models/repository_base.rb @@ -9,6 +9,7 @@ class RepositoryBase < ApplicationRecord belongs_to :team belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User' + belongs_to :archived_by, foreign_key: :archived_by_id, class_name: 'User', inverse_of: :repository, optional: true has_many :repository_columns, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy has_many :repository_rows, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy has_many :repository_table_states, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy @@ -17,10 +18,17 @@ class RepositoryBase < ApplicationRecord auto_strip_attributes :name, nullify: false validates :team, presence: true validates :created_by, presence: true + with_options if: :archived do + validates :archived_by, presence: true + validates :archived_on, presence: true + end # Not discarded default_scope -> { kept } + scope :active, -> { where(archived: false) } + scope :archived, -> { where(archived: true) } + def cell_preload_includes cell_includes = [] repository_columns.pluck(:data_type).each do |data_type| diff --git a/app/models/user.rb b/app/models/user.rb index 45907fcf0..1e07ca99d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -197,6 +197,9 @@ class User < ApplicationRecord class_name: 'Protocol', foreign_key: 'restored_by_id', inverse_of: :restored_by + has_many :archived_repositories, + class_name: 'RepositoryBase', + foreign_key: 'archived_by_id' has_many :assigned_my_module_repository_rows, class_name: 'MyModuleRepositoryRow', foreign_key: 'assigned_by_id' diff --git a/db/migrate/20200603125407_add_archive_to_repository.rb b/db/migrate/20200603125407_add_archive_to_repository.rb new file mode 100644 index 000000000..f08b99edc --- /dev/null +++ b/db/migrate/20200603125407_add_archive_to_repository.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AddArchiveToRepository < ActiveRecord::Migration[6.0] + def change + change_table :repositories, bulk: true do |t| + t.boolean :archived, default: false, null: false + t.datetime :archived_on + t.references :archived_by, index: true, foreign_key: { to_table: :users } + end + end +end