Class: Submission::SubmissionCreator

Inherits:
PresenterSkeleton show all
Defined in:
app/models/submission/submission_creator.rb

Overview

Used to handle the rendering of the submission/order pages in the web-based submission interface

Constant Summary collapse

SubmissionsCreaterError =
Class.new(StandardError)
IncorrectParamsException =
Class.new(SubmissionsCreaterError)
InvalidInputException =
Class.new(SubmissionsCreaterError)

Instance Attribute Summary

Attributes inherited from PresenterSkeleton

#id

Instance Method Summary collapse

Methods inherited from PresenterSkeleton

#cross_compatible?, #each_submission_warning, #initialize, #lanes_of_sequencing

Constructor Details

This class inherits a constructor from Submission::PresenterSkeleton

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Submission::PresenterSkeleton

Instance Method Details

#build_submission!Object


31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/submission/submission_creator.rb', line 31

def build_submission!
  submission.built!
rescue AASM::InvalidTransition
  submission.errors.add(:base, 'Submissions can not be edited once they are submitted for building.')
rescue ActiveRecord::RecordInvalid => e
  e.record.errors.full_messages.each do |message|
    submission.errors.add(:base, message)
  end
rescue Submission::ProjectValidation::Error => e
  submission.errors.add(:base, e.message)
end

#cross_projectObject


156
157
158
# File 'app/models/submission/submission_creator.rb', line 156

def cross_project
  false
end

#cross_studyObject


160
161
162
# File 'app/models/submission/submission_creator.rb', line 160

def cross_study
  false
end

#find_asset_groupObject


47
48
49
# File 'app/models/submission/submission_creator.rb', line 47

def find_asset_group
  AssetGroup.find(asset_group_id) if asset_group_id.present?
end

#orderObject

Returns the either the first order associated with the submission or creates a new blank order.


53
54
55
56
57
58
# File 'app/models/submission/submission_creator.rb', line 53

def order
  return @order if @order.present?
  return submission.orders.first if submission.present?

  @order = create_order
end

#order_assetsObject

this is more order_receptacles, asset_group is actually receptacle group


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/submission/submission_creator.rb', line 123

def order_assets
  input_methods = %i[asset_group_id sample_names_text barcodes_wells_text].select { |input_method| send(input_method).present? }

  raise InvalidInputException, 'No Samples found' if input_methods.empty?
  raise InvalidInputException, 'Samples cannot be added from multiple sources at the same time.' unless input_methods.size == 1

  case input_methods.first
  when :asset_group_id then { asset_group: find_asset_group }
  when :sample_names_text
    {
      assets: wells_on_specified_plate_purpose_for(
        plate_purpose,
        find_samples_from_text(sample_names_text)
      )
    }
  when :barcodes_wells_text
    {
      assets: find_assets_from_text(barcodes_wells_text)
    }

  else raise StandardError, "No way to determine assets for input choice #{input_methods.first}"
  end
end

#order_fieldsObject


72
73
74
75
# File 'app/models/submission/submission_creator.rb', line 72

def order_fields
  order.request_type_ids_list = order.request_types.map { |rt| [rt] } if order.input_field_infos.flatten.empty?
  order.input_field_infos.reject { |info| per_order_settings.include?(info.key) }
end

#order_paramsObject


62
63
64
65
66
# File 'app/models/submission/submission_creator.rb', line 62

def order_params
  @order_params = @order_params.to_hash if @order_params.class == HashWithIndifferentAccess
  @order_params[:multiplier] = HashWithIndifferentAccess.new if @order_params && @order_params[:multiplier].nil?
  @order_params
end

#ordersObject

Return the submission's orders or a blank array


78
79
80
81
82
# File 'app/models/submission/submission_creator.rb', line 78

def orders
  return [] if submission.blank?

  submission.try(:orders).map { |o| OrderPresenter.new(o) }
end

#per_order_settingsObject


43
44
45
# File 'app/models/submission/submission_creator.rb', line 43

def per_order_settings
  %i[pre_capture_plex_level gigabases_expected customer_accepts_responsibility]
end

#plate_purposeObject


164
165
166
# File 'app/models/submission/submission_creator.rb', line 164

def plate_purpose
  @plate_purpose ||= PlatePurpose.find(plate_purpose_id)
end

#pre_capture_plex_levelObject


68
69
70
# File 'app/models/submission/submission_creator.rb', line 68

def pre_capture_plex_level
  order.input_field_infos.detect { |ifi| ifi.key == :pre_capture_plex_level }&.default_value
end

#product_linesObject


190
191
192
# File 'app/models/submission/submission_creator.rb', line 190

def product_lines
  SubmissionTemplate.grouped_by_product_lines
end

#projectObject


84
85
86
# File 'app/models/submission/submission_creator.rb', line 84

def project
  @project ||= Project.find_by(name: @project_name)
end

#saveObject

Creates a new submission and adds an initial order on the submission using the parameters


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/submission/submission_creator.rb', line 90

def save
  begin
    ActiveRecord::Base.transaction do
      # Add assets to the order...
      new_order = create_order.tap { |o| o.update!(order_assets) }

      if submission.present?
        # The submission should be destroyed if we delete the last order on it so
        # we shouldn't see any empty submissions.

        submission.orders << new_order
      else
        @submission = new_order.create_submission(user: order.user, priority: priority)
      end

      new_order.save!
      @order = new_order
    end
  rescue Submission::ProjectValidation::Error => e
    order.errors.add(:base, e.message)
  rescue SubmissionsCreaterError, Asset::Finder::InvalidInputException => e
    order.errors.add(:base, e.message)
  rescue ActiveRecord::RecordInvalid => e
    e.record.errors.full_messages.each do |message|
      order.errors.add(:base, message)
    end
  end

  # Having got through that lot, return whether the save was successful or not
  order.errors.empty?
end

#studiesObject


172
173
174
175
# File 'app/models/submission/submission_creator.rb', line 172

def studies
  @studies ||= [study] if study.present?
  @studies ||= @user.interesting_studies.alphabetical
end

#studyObject


168
169
170
# File 'app/models/submission/submission_creator.rb', line 168

def study
  @study ||= (Study.find(@study_id) if @study_id.present?)
end

#submissionObject


177
178
179
180
181
# File 'app/models/submission/submission_creator.rb', line 177

def submission
  return nil unless id.present? || @submission

  @submission ||= Submission.find(id)
end

#templateObject

Returns the SubmissionTemplate (OrderTemplate) to be used for this Submission.


184
185
186
187
188
# File 'app/models/submission/submission_creator.rb', line 184

def template
  # We can't get the template from a saved order, have to find by name.... :(
  @template = SubmissionTemplate.find_by(name: order.template_name) if try(:submission).try(:orders).present?
  @template ||= SubmissionTemplate.find(@template_id)
end

#template_idObject


194
195
196
# File 'app/models/submission/submission_creator.rb', line 194

def template_id
  submission.try(:orders).try(:first).try(:id)
end

#template_nameObject


208
209
210
# File 'app/models/submission/submission_creator.rb', line 208

def template_name
  submission.orders.first.template_name
end

#url(view) ⇒ Object


204
205
206
# File 'app/models/submission/submission_creator.rb', line 204

def url(view)
  view.send(:submission_path, submission.presence || { id: 'DUMMY_ID' })
end

#user_valid_projectsObject

Returns an array of all the names of active projects associated with the current user.


200
201
202
# File 'app/models/submission/submission_creator.rb', line 200

def user_valid_projects
  @user_valid_projects ||= @user.valid_projects
end

#wells_on_specified_plate_purpose_for(plate_purpose, samples) ⇒ Object

This is a legacy of the old controller…


148
149
150
151
152
153
154
# File 'app/models/submission/submission_creator.rb', line 148

def wells_on_specified_plate_purpose_for(plate_purpose, samples)
  samples.map do |sample|
    # Prioritise the newest well
    sample.wells.on_plate_purpose(plate_purpose).order(id: :desc).first ||
      raise(InvalidInputException, "No #{plate_purpose.name} plate found with sample: #{sample.name}")
  end
end