Module: ModelExtensions::Order

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

Defined Under Namespace

Modules: Validations Classes: NonNilHash, RequestOptionForValidation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
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
# File 'app/api/model_extensions/order.rb', line 43

def self.included(base)
  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: 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


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/api/model_extensions/order.rb', line 127

def request_options_structured
  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['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


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

def request_options_structured=(values)
  @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']
      attributes['fragment_size_required_to']   = json['fragment_size_required', 'to']
      attributes['pcr_cycles']                  = json['pcr_cycles']
      attributes[:bait_library_name]            = json['bait_library']
      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)

123
124
125
# File 'app/api/model_extensions/order.rb', line 123

def request_type_multiplier
  yield(request_types.last.to_s.to_sym) unless request_types.blank?
end

#request_type_objectsObject


167
168
169
170
171
# File 'app/api/model_extensions/order.rb', line 167

def request_type_objects
  return [] if request_types.blank?

  ::RequestType.find(request_types)
end

#validate_new_record(assets) ⇒ Object

Raises:

  • (StandardError)

37
38
39
40
41
# File 'app/api/model_extensions/order.rb', line 37

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

  true
end