Class: RequestType

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

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
]

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


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

has_many :requests, inverse_of: :request_type

Class Method Details

.create_assetObject


107
108
109
110
111
112
113
114
115
# File 'app/models/request_type.rb', line 107

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


117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/request_type.rb', line 117

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


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/request_type.rb', line 81

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


95
96
97
# File 'app/models/request_type.rb', line 95

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

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


103
104
105
# File 'app/models/request_type.rb', line 103

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

#create_target_asset!(&block) ⇒ Object


147
148
149
150
151
152
153
# File 'app/models/request_type.rb', line 147

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


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

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

#extract_metadata_from_hash(request_options) ⇒ Object


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

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


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

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

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


99
100
101
# File 'app/models/request_type.rb', line 99

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

#request_attributesObject


172
173
174
# File 'app/models/request_type.rb', line 172

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

#request_classObject


129
130
131
# File 'app/models/request_type.rb', line 129

def request_class
  request_class_name&.constantize
end

#request_class=(request_class) ⇒ Object


133
134
135
# File 'app/models/request_type.rb', line 133

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.


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

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