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


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

def cross_project
  false
end

#cross_studyObject


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

def cross_study
  false
end

#current_abilityObject


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

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


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

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


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.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


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


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


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

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


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

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


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

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


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

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

#studyObject


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

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

#submissionObject


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

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

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

#templateObject

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


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

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


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

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

#template_nameObject


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

def template_name
  submission.orders.first.template_name
end

#url(view) ⇒ Object


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

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.


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

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…


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

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