Module: SampleAccessioning

Extended by:
ActiveSupport::Concern, IncludeTag
Included in:
Sample
Defined in:
app/models/sample_accessioning.rb

Overview

technically should be called Sample::Accessioning, but keeping it next to sample for ease of maintenance

Constant Summary collapse

EVENTS =

Defines events related to sample accessioning which will be added to the Sample model See EventfulRecord.has_many_events for details

[
  [:assigned_accession_number!, Event::AccessioningEvent, :assigned_accession_number!],
  [:updated_accessioned_metadata!, Event::AccessioningEvent, :updated_accessioned_metadata!]
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IncludeTag

include_tag

Instance Attribute Details

#current_userObject

For attributing accessioning changes recorded in the SS events table



31
32
33
# File 'app/models/sample_accessioning.rb', line 31

def current_user
  @current_user
end

Class Method Details

.tagsObject



14
15
16
# File 'app/models/sample_accessioning.rb', line 14

def self.tags
  @tags ||= []
end

Instance Method Details

#accession_and_handle_validation_errorsObject

NOTE: this does not check whether the current user is permitted to accession the sample, nor if accessioning is enabled, as these belong in a controller or library, rather than the model.



102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/sample_accessioning.rb', line 102

def accession_and_handle_validation_errors
  event_user = current_user # the event_user for this sample must be set from the calling controller
  Accession.accession_sample(self, event_user, perform_now: true)

# Save error messages for later feedback to the user in a flash message
rescue Accession::InternalValidationError
  # validation errors have already been added to the sample in Accession::Sample.validate!
rescue Accession::Error, Faraday::Error => e
  message = Accession.user_error_message(e)
  errors.add(:base, message)
end

#accession_number?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/sample_accessioning.rb', line 63

def accession_number?
  ebi_accession_number.present?
end

#all_accessionable_studies_open?Boolean

Returns true if all accessionable studies are open, false otherwise.

Returns:

  • (Boolean)


138
139
140
# File 'app/models/sample_accessioning.rb', line 138

def all_accessionable_studies_open?
  studies_for_accessioning.all? { |study| study..open? }
end

#current_accession_statusObject



132
133
134
# File 'app/models/sample_accessioning.rb', line 132

def current_accession_status
  accession_sample_statuses.last
end

#ebi_accession_numberObject



59
60
61
# File 'app/models/sample_accessioning.rb', line 59

def ebi_accession_number
  .sample_ebi_accession_number
end

#ena_studyObject



114
115
116
# File 'app/models/sample_accessioning.rb', line 114

def ena_study
  studies.first
end

#should_be_accessioned?Boolean

Criteria for whether a sample should be accessioned. A sample should be accessioned if: - it is part of a single accessionable study - or all of the accessionable studies it is part of are open

Returns:

  • (Boolean)

    true if the sample should be accessioned, false otherwise



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/sample_accessioning.rb', line 84

def should_be_accessioned?
  # If updating this method, also update should_be_accessioned_warning used in app/views/samples/_studies.html.erb

  case studies_for_accessioning.size
  when 0
    # No accessionable studies
    false
  when 1
    # Always accession
    true
  else
    # Samples belonging to more than one accessionables study can only be accessioned if all studies are open
    all_accessionable_studies_open?
  end
end

#studies_for_accessioningArray<Study>

Returns an array of studies linked to this sample that are eligible for accessioning A study is eligible for accessioning if: - it is active - it is set to open or managed - it is not set to never release - it requires accessioning - it has an accession number

Returns:

  • (Array<Study>)

    the studies linked to this sample that are eligible for accessioning



75
76
77
# File 'app/models/sample_accessioning.rb', line 75

def studies_for_accessioning
  studies.select(&:samples_accessionable?)
end

#study_for_accessioningObject



118
119
120
121
# File 'app/models/sample_accessioning.rb', line 118

def study_for_accessioning
  # There should only be one study for accessioning, if we want to accession, so we return the only one
  studies_for_accessioning.first
end

#validate_sample_for_accessioning!Object

Validates that the sample and it's study are valid for ALL accessioning services accessioning



124
125
126
127
128
129
130
# File 'app/models/sample_accessioning.rb', line 124

def validate_sample_for_accessioning!
  accession_service = AccessionService.select_for_sample(self)
  (valid?(:accession) && valid?(accession_service.provider)) || raise(ActiveRecord::RecordInvalid, self)
rescue ActiveRecord::RecordInvalid => e
  ena_study.errors.full_messages.each { |message| errors.add(:base, "#{message} on study") } unless ena_study.nil?
  raise e
end