Class: RequestType

Inherits:
ApplicationRecord show all
Includes:
Validation, SharedBehaviour::Named, Uuid::Uuidable
Defined in:
app/models/request_type.rb

Overview

orders use RequestTypes as a factory to construct requests. The list of request types to use is provided by Order#request_types and usually gets populated by the SubmissionTemplate. Once the request it built, request type identifies the type of Request and associates it with a particular Pipeline. In the case of external pipelines, such as Limber, other properties of Request such as its LibraryType may also be considered. Request types have associated validators which will be used to ensure that the associated requests have compatible Metadata::Metadata. In the case of library types, this uses the library_types association on the request type to provide the list of compatible library types. Currently the request type is also the means of associating the request with a particular team (product line) however this may belong better on request itself, and could be set either on the basis of the submission template used, or by a new 'team' option on the submission itself.

Defined Under Namespace

Modules: Validation Classes: DeprecatedError, PoolingMethod, RequestTypePlatePurpose, Validator

Constant Summary collapse

MORPHOLOGIES =
[
  LINEAR = 0, # one-to-one
  CONVERGENT = 1, # many-to-one
  DIVERGENT = 2 # one-to-many
  # we don't do many-to-many so far
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedBehaviour::Named

included

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods included from Validation

#delegate_validator, #request_type_validator

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Instance Attribute Details

#keyString

Returns A simple text identifier for the request type designed for programmatic use.

Returns:

  • (String)

    A simple text identifier for the request type designed for programmatic use


44
# File 'app/models/request_type.rb', line 44

has_many :requests, inverse_of: :request_type

Class Method Details

.create_assetObject


125
126
127
128
129
130
131
132
133
# File 'app/models/request_type.rb', line 125

def self.create_asset
  create_with(
    name: 'Create Asset',
    order: 1,
    asset_type: 'Asset',
    request_class_name: 'CreateAssetRequest',
    request_purpose: :internal
  ).find_or_create_by!(key: 'create_asset')
end

.external_multiplexed_library_creationObject


135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/request_type.rb', line 135

def self.external_multiplexed_library_creation
  create_with(
    asset_type: 'LibraryTube',
    for_multiplexing: true,
    initial_state: 'pending',
    order: 0,
    name: 'External Multiplexed Library Creation',
    request_class_name: 'ExternalLibraryCreationRequest',
    request_purpose: :standard
  ).find_or_create_by!(key: 'external_multiplexed_library_creation')
end

Instance Method Details

#construct_request(construct_method, attributes, klass = request_class) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/request_type.rb', line 99

def construct_request(construct_method, attributes, klass = request_class)
  raise RequestType::DeprecatedError if deprecated?

  new_request = klass.public_send(construct_method, attributes) do |request|
    request.request_type = self
    request.request_purpose ||= request_purpose
    yield(request) if block_given?
    request.billing_product = find_product_for_request(request)
  end
  # Prevent us caching all our requests
  requests.reset
  new_request
end

#create!(attributes = {}, &block) ⇒ Object


113
114
115
# File 'app/models/request_type.rb', line 113

def create!(attributes = {}, &block)
  construct_request(:create!, attributes, &block)
end

#create_control!(attributes = {}, &block) ⇒ Object


121
122
123
# File 'app/models/request_type.rb', line 121

def create_control!(attributes = {}, &block)
  construct_request(:create!, attributes, ControlRequest, &block)
end

#create_target_asset!(&block) ⇒ Object


173
174
175
176
177
178
179
# File 'app/models/request_type.rb', line 173

def create_target_asset!(&block)
  case
  when target_purpose.present?  then target_purpose.create!(&block).receptacle
  when target_asset_type.blank? then nil
  else                               target_asset_type.constantize.create!(&block)
  end
end

#default_library_typeObject


181
182
183
# File 'app/models/request_type.rb', line 181

def default_library_type
  library_types.find_by(library_types_request_types: { is_default: true })
end

#extract_metadata_from_hash(request_options) ⇒ Object


163
164
165
166
167
168
169
170
171
# File 'app/models/request_type.rb', line 163

def (request_options)
  # WARNING: we need a copy of the options (we delete stuff from attributes)
  return {} unless request_options

  attributes = request_options.symbolize_keys
  common_attributes = request_class::Metadata.attribute_details.map(&:name)
  common_attributes.concat(request_class::Metadata.association_details.map(&:assignable_attribute_name))
  attributes.delete_if { |k, _| common_attributes.exclude?(k) }
end

#find_product_for_request(request) ⇒ Object


185
186
187
# File 'app/models/request_type.rb', line 185

def find_product_for_request(request)
  billing_product_catalogue.find_product_for_request(request) if billing_product_catalogue.present?
end

#new(attributes = {}, &block) ⇒ Object


117
118
119
# File 'app/models/request_type.rb', line 117

def new(attributes = {}, &block)
  construct_request(:new, attributes, &block)
end

#product_line_name=(product_line_name) ⇒ Object


155
156
157
# File 'app/models/request_type.rb', line 155

def product_line_name=(product_line_name)
  self.product_line = ProductLine.find_or_create_by!(name: product_line_name)
end

#request_attributesObject


198
199
200
# File 'app/models/request_type.rb', line 198

def request_attributes
  request_class::Metadata.attribute_details + request_class::Metadata.association_details
end

#request_classObject


147
148
149
# File 'app/models/request_type.rb', line 147

def request_class
  request_class_name&.constantize
end

#request_class=(request_class) ⇒ Object


151
152
153
# File 'app/models/request_type.rb', line 151

def request_class=(request_class)
  self.request_class_name = request_class.name
end

#target_purpose_name=(target_purpose_name) ⇒ Object


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

def target_purpose_name=(target_purpose_name)
  self.target_purpose = Purpose.find_by!(name: target_purpose_name)
end

#validator_for(request_option) ⇒ Object

Returns the validator for a given option.


190
191
192
193
194
195
196
# File 'app/models/request_type.rb', line 190

def validator_for(request_option)
  if request_type_validators.loaded?
    request_type_validators.detect { |rtv| rtv.request_option == request_option.to_s }
  else
    request_type_validators.find_by(request_option: request_option.to_s)
  end || RequestType::Validator::NullValidator.new
end