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 buit, 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 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


122
123
124
125
126
127
128
129
130
# File 'app/models/request_type.rb', line 122

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


132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/request_type.rb', line 132

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


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/request_type.rb', line 96

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


110
111
112
# File 'app/models/request_type.rb', line 110

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

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


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

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

#create_target_asset!(&block) ⇒ Object


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

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


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

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

#extract_metadata_from_hash(request_options) ⇒ Object


152
153
154
155
156
157
158
159
160
# File 'app/models/request_type.rb', line 152

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, _| not common_attributes.include?(k) }
end

#find_product_for_request(request) ⇒ Object


174
175
176
# File 'app/models/request_type.rb', line 174

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

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


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

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

#request_attributesObject


187
188
189
# File 'app/models/request_type.rb', line 187

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

#request_classObject


144
145
146
# File 'app/models/request_type.rb', line 144

def request_class
  request_class_name&.constantize
end

#request_class=(request_class) ⇒ Object


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

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

#validator_for(request_option) ⇒ Object

Returns the validator for a given option.


179
180
181
182
183
184
185
# File 'app/models/request_type.rb', line 179

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