Class: Accession::SampleStatus

Inherits:
ApplicationRecord show all
Defined in:
app/models/accession/sample_status.rb

Overview

Designed to track the status of Sample accessioning requests in order to provide feedback to users. It could be expanded to track other accessionable types in the future.

Associations: belongs_to :sample - The sample being accessioned

Attributes: sample_id: integer - The ID of the sample being accessioned status: string - The current status of the accessioning request (eg: 'queued', 'failed') message: text - Any message associated with the status (eg: error messages)

Class Method Summary collapse

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.create_for_sample(sample, status = 'queued', message = nil) ⇒ Accession::SampleStatus

Creates a new Accession::SampleStatus record for the given sample with the specified status.

Parameters:

  • sample (Sample)

    The sample for which to create the status record.

  • status (String) (defaults to: 'queued')

    The status to set (default: 'queued').

  • message (String, nil) (defaults to: nil)

    An optional message to associate with the status.

Returns:



25
26
27
# File 'app/models/accession/sample_status.rb', line 25

def self.create_for_sample(sample, status = 'queued', message = nil)
  create!(sample:, status:, message:)
end

.find_latest!(sample, status: nil) ⇒ Accession::SampleStatus

Returns the most recent Accession::SampleStatus record for the given sample, optionally filtered by status.

Parameters:

  • sample (Sample)

    The sample for which to find the latest status.

  • status (String, nil) (defaults to: nil)

    Optional status to filter by (e.g., 'queued', 'failed').

Returns:

Raises:



35
36
37
38
39
# File 'app/models/accession/sample_status.rb', line 35

def self.find_latest!(sample, status: nil)
  scope = where(sample:)
  scope = scope.where(status:) if status
  scope.order(created_at: :desc).first!
end

.find_latest_and_update!(sample, status: nil, attributes: {}) ⇒ Accession::SampleStatus

Updates the most recent Accession::SampleStatus record for the given sample, optionally filtered by status.

Parameters:

  • sample (Sample)

    The sample for which to update the latest status.

  • status (String, nil) (defaults to: nil)

    Optional status to filter by (e.g., 'queued', 'failed').

  • attributes (Hash) (defaults to: {})

    The attributes to update on the status record.

Returns:

Raises:



48
49
50
51
52
53
54
55
# File 'app/models/accession/sample_status.rb', line 48

def self.find_latest_and_update!(sample, status: nil, attributes: {})
  # Wrap in a transaction to prevent race conditions
  transaction do
    status_record = find_latest!(sample, status:)
    status_record.update!(attributes)
    status_record
  end
end