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 =

rubocop:todo Metrics/ClassLength

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
# 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 { |message| submission.errors.add(:base, message) }
rescue Submission::ProjectValidation::Error => e
  submission.errors.add(:base, e.message)
end

#cross_projectObject



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

def cross_project
  false
end

#cross_studyObject



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

def cross_study
  false
end

#current_abilityObject



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

def current_ability
  @current_ability ||= Ability.new(@user)
end

#find_asset_groupObject



45
46
47
# File 'app/models/submission/submission_creator.rb', line 45

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.



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 rubocop:todo Metrics/MethodLength



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 126

def order_assets # rubocop:todo Metrics/AbcSize
  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?
  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
    { 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



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

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

def order_params
  @order_params = @order_params.to_hash if @order_params.instance_of?(ActiveSupport::HashWithIndifferentAccess)
  @order_params[:multiplier] = ActiveSupport::HashWithIndifferentAccess.new if @order_params &&
    @order_params[:multiplier].nil?
  @order_params
end

#ordersObject

Return the submission’s orders or a blank array



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

def orders
  return [] if submission.blank?

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

#per_order_settingsObject



41
42
43
# File 'app/models/submission/submission_creator.rb', line 41

def per_order_settings
  %i[pre_capture_plex_level gigabases_expected customer_accepts_responsibility]
end

#plate_purposeObject



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

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

#pre_capture_plex_levelObject



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

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

#product_linesObject



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

def product_lines
  SubmissionTemplate.grouped_by_product_lines
end

#projectObject



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

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 rubocop:todo Metrics/MethodLength



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 92

def save # rubocop:todo Metrics/AbcSize
  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 { |message| order.errors.add(:base, message) }
  end

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

#studiesObject



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

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

#studyObject



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

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

#submissionObject



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

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

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

#templateObject

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



187
188
189
190
191
# File 'app/models/submission/submission_creator.rb', line 187

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



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

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

#template_nameObject



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

def template_name
  submission.orders.first.template_name
end

#url(view) ⇒ Object



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

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.



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

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…



150
151
152
153
154
155
156
157
# File 'app/models/submission/submission_creator.rb', line 150

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