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


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

def cross_project
  false
end

#cross_studyObject


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

def cross_study
  false
end

#current_abilityObject


211
212
213
# File 'app/models/submission/submission_creator.rb', line 211

def current_ability
  @current_ability ||= Ability.new(@user)
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

Note:

Following the creation of an order, this will actually be the last order

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


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

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


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

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

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

  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


74
75
76
77
# File 'app/models/submission/submission_creator.rb', line 74

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


64
65
66
67
68
# File 'app/models/submission/submission_creator.rb', line 64

def order_params
  @order_params = @order_params.to_hash if @order_params.instance_of?(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


80
81
82
83
84
# File 'app/models/submission/submission_creator.rb', line 80

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


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

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

#pre_capture_plex_levelObject


70
71
72
# File 'app/models/submission/submission_creator.rb', line 70

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

#product_linesObject


197
198
199
# File 'app/models/submission/submission_creator.rb', line 197

def product_lines
  SubmissionTemplate.grouped_by_product_lines
end

#projectObject


86
87
88
# File 'app/models/submission/submission_creator.rb', line 86

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


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
121
122
# File 'app/models/submission/submission_creator.rb', line 92

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


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

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

#studyObject


175
176
177
# File 'app/models/submission/submission_creator.rb', line 175

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

#submissionObject


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

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

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

#templateObject

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


191
192
193
194
195
# File 'app/models/submission/submission_creator.rb', line 191

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


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

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

#template_nameObject


219
220
221
# File 'app/models/submission/submission_creator.rb', line 219

def template_name
  submission.orders.first.template_name
end

#url(view) ⇒ Object


215
216
217
# File 'app/models/submission/submission_creator.rb', line 215

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.


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

def user_valid_projects
  @user_valid_projects ||= Project.accessible_by(current_ability, :create_submission).valid
end

#wells_on_specified_plate_purpose_for(plate_purpose, samples) ⇒ Object

This is a legacy of the old controller…


154
155
156
157
158
159
160
161
# File 'app/models/submission/submission_creator.rb', line 154

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