Add status to project and experiment [SCI-11797]

This commit is contained in:
Andrej 2025-04-16 10:30:06 +02:00
parent 8982329132
commit 38c379d065
5 changed files with 69 additions and 4 deletions

View file

@ -0,0 +1,63 @@
# frozen_string_literal: true
module TimeTrackable
extend ActiveSupport::Concern
included do
scope :not_started, -> { where(started_at: nil).where(completed_at: nil) }
scope :started, -> { where.not(started_at: nil).where(completed_at: nil) }
scope :completed, -> { where.not(completed_at: nil) }
end
def status=(status)
case status
when :not_started
self.started_at = nil
self.completed_at = nil
when :started
self.completed_at = nil
start
when :completed
complete
else
raise ArgumentError, 'Wrong status for TimeTrackable model!'
end
end
def not_started?
started_at.blank? && completed_at.blank?
end
def not_started
self.started_at = nil
self.completed_at = nil
end
def not_started!
update!(started_at: nil, completed_at: nil)
end
def started?
started_at.present? && completed_at.blank?
end
def start
self.started_at = DateTime.now
end
def start!
update!(started_at: DateTime.now)
end
def completed?
completed_at.present?
end
def complete
self.completed_at = DateTime.now
end
def complete!
update!(completed_at: DateTime.now)
end
end

View file

@ -13,6 +13,7 @@ class Experiment < ApplicationRecord
include PermissionCheckableModel
include Assignable
include Cloneable
include TimeTrackable
before_save -> { report_elements.destroy_all }, if: -> { !new_record? && project_id_changed? }

View file

@ -11,6 +11,7 @@ class Project < ApplicationRecord
include ViewableModel
include PermissionCheckableModel
include Assignable
include TimeTrackable
enum visibility: { hidden: 0, visible: 1 }

View file

@ -3,13 +3,13 @@
class AddManagementColumnsToProjectAndExperiment < ActiveRecord::Migration[7.0]
def change
add_column :projects, :started_at, :datetime
add_column :projects, :ended_at, :datetime
add_column :projects, :completed_at, :datetime
add_column :projects, :start_on, :date
add_column :projects, :description, :text
add_reference :projects, :supervised_by, foreign_key: { to_table: :users }
add_column :experiments, :started_at, :datetime
add_column :experiments, :ended_at, :datetime
add_column :experiments, :completed_at, :datetime
add_column :experiments, :due_date, :date
add_column :experiments, :start_on, :date

View file

@ -213,7 +213,7 @@ ActiveRecord::Schema[7.0].define(version: 2025_04_10_093420) do
t.datetime "updated_at", precision: nil, null: false
t.uuid "uuid"
t.datetime "started_at"
t.datetime "ended_at"
t.datetime "completed_at"
t.date "due_date"
t.date "start_on"
t.index "(('EX'::text || id)) gin_trgm_ops", name: "index_experiments_on_experiment_code", using: :gin
@ -577,7 +577,7 @@ ActiveRecord::Schema[7.0].define(version: 2025_04_10_093420) do
t.bigint "project_folder_id"
t.bigint "default_public_user_role_id"
t.datetime "started_at"
t.datetime "ended_at"
t.datetime "completed_at"
t.date "start_on"
t.text "description"
t.bigint "supervised_by_id"