Module: ModelExtensions::Order

Included in:
Order
Defined in:
app/api/model_extensions/order.rb

Overview

Included in Order The intent of this file was to provide methods specific to the V1 API

Defined Under Namespace

Modules: Validations Classes: NonNilHash, RequestOptionForValidation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:todo Metrics/MethodLength



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/api/model_extensions/order.rb', line 54

def self.included(base) # rubocop:todo Metrics/AbcSize
  base.class_eval do
    include Validations

    before_validation :merge_in_structured_request_options

    scope :include_study, -> { includes(study: :uuid_object) }
    scope :include_project, -> { includes(project: :uuid_object) }
    scope :include_assets, -> { includes(assets: [:uuid_object, { aliquots: Io::Aliquot::PRELOADS }]) }

    has_many :submitted_assets, -> { joins(:asset) }, inverse_of: :order
    has_many :assets, through: :submitted_assets, before_add: :validate_new_record do
      def <<(associated)
        return super(associated) if associated.is_a?(Receptacle)

        Rails.logger.warn("#{associated.class.name} passed to order.assets")
        super(associated&.receptacle)
      end
    end

    scope :that_submitted_asset_id, ->(asset_id) { where(submitted_assets: { asset_id: }).joins(:submitted_assets) }

    validate :extended_validation
    def extended_validation
      extended_validators.reduce(true) { |valid, validator| validator.validate_order(self) && valid }
    end

    # The API can create submissions but we have to prevent someone from changing the study
    # and the project once they have been set.
    validates_each(:study, :project, on: :update) do |record, attr, _value|
      record.errors.add(attr, 'cannot be changed') if record.send(:"will_save_change_to_#{attr}_id?")
    end

    def extended_validators
      ExtendedValidator.for_submission(self)
    end
  end
end

Instance Method Details

#request_options_structuredObject

rubocop:todo Metrics/MethodLength



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/api/model_extensions/order.rb', line 139

def request_options_structured # rubocop:todo Metrics/AbcSize
  NonNilHash
    .new(:stringify_keys)
    .tap do |json|
      NonNilHash
        .new
        .deep_merge(request_options)
        .tap do |attributes|
          json['read_length'] = attributes[:read_length].try(:to_i)
          json['library_type'] = attributes[:library_type]
          json['fragment_size_required', 'from'] = attributes[:fragment_size_required_from].try(:to_i)
          json['fragment_size_required', 'to'] = attributes[:fragment_size_required_to].try(:to_i)
          json['pcr_cycles'] = attributes[:pcr_cycles].try(:to_i)
          json['bait_library'] = attributes[:bait_library_name]
          json['primer_panel_name'] = attributes[:primer_panel_name]
          json['sequencing_type'] = attributes[:sequencing_type]
          json['insert_size'] = attributes[:insert_size].try(:to_i)
          request_type_multiplier { |id| json['number_of_lanes'] = attributes[:multiplier, id] }
        end
    end
    .to_hash
end

#request_options_structured=(values) ⇒ Object

rubocop:todo Metrics/MethodLength



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/api/model_extensions/order.rb', line 165

def request_options_structured=(values) # rubocop:todo Metrics/AbcSize
  @request_options_structured =
    NonNilHash
      .new
      .tap do |attributes|
        NonNilHash
          .new(:stringify_keys)
          .deep_merge(values)
          .tap do |json|
            # NOTE: Be careful with the names here to ensure that they match up, exactly with what is in a template.
            # If the template uses symbol names then these need to be symbols too.
            attributes[:read_length] = json['read_length']
            attributes['library_type'] = json['library_type']
            attributes['fragment_size_required_from'] = json['fragment_size_required', 'from'] ||
              json['fragment_size_required_from']
            attributes['fragment_size_required_to'] = json['fragment_size_required', 'to'] ||
              json['fragment_size_required_to']
            attributes['pcr_cycles'] = json['pcr_cycles']
            attributes[:bait_library_name] = json['bait_library']
            attributes[:primer_panel_name] = json['primer_panel_name']
            attributes[:sequencing_type] = json['sequencing_type']
            attributes[:insert_size] = json['insert_size']
            request_type_multiplier { |id| attributes[:multiplier, id] = json['number_of_lanes'] }
          end
      end
      .to_hash
end

#request_type_multiplier {|request_types.last.to_s.to_sym| ... } ⇒ Object

Yields:

  • (request_types.last.to_s.to_sym)


134
135
136
# File 'app/api/model_extensions/order.rb', line 134

def request_type_multiplier
  yield(request_types.last.to_s.to_sym) if request_types.present?
end

#request_type_objectsObject



201
202
203
204
205
# File 'app/api/model_extensions/order.rb', line 201

def request_type_objects
  return [] if request_types.blank?

  ::RequestType.find(request_types)
end

#validate_new_record(assets) ⇒ Object



45
46
47
48
49
50
51
# File 'app/api/model_extensions/order.rb', line 45

def validate_new_record(assets)
  if (not new_record?) && asset_group? && assets.present?
    raise StandardError, 'requested action is not supported on this resource'
  end

  true
end