Class: LabwhereReception

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/labwhere_reception.rb

Overview

A simple class to handle the behaviour from the labwhere reception controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_code, location_barcode, asset_barcodes) ⇒ LabwhereReception

Returns a new instance of LabwhereReception


18
19
20
21
22
# File 'app/models/labwhere_reception.rb', line 18

def initialize(user_code, location_barcode, asset_barcodes)
  @asset_barcodes = (asset_barcodes || []).map(&:strip)
  @location_barcode = location_barcode.try(:strip)
  @user_code = user_code.try(:strip)
end

Instance Attribute Details

#asset_barcodesObject (readonly)

Returns the value of attribute asset_barcodes


10
11
12
# File 'app/models/labwhere_reception.rb', line 10

def asset_barcodes
  @asset_barcodes
end

#location_barcodeObject (readonly)

Returns the value of attribute location_barcode


10
11
12
# File 'app/models/labwhere_reception.rb', line 10

def location_barcode
  @location_barcode
end

#user_codeObject (readonly)

Returns the value of attribute user_code


10
11
12
# File 'app/models/labwhere_reception.rb', line 10

def user_code
  @user_code
end

Instance Method Details

#assetsObject


63
64
65
# File 'app/models/labwhere_reception.rb', line 63

def assets
  @assets ||= Labware.with_barcode(asset_barcodes)
end

#idObject


24
# File 'app/models/labwhere_reception.rb', line 24

def id; nil; end

#missing_barcodesObject


67
68
69
70
71
72
73
# File 'app/models/labwhere_reception.rb', line 67

def missing_barcodes
  machine_barcodes = assets.map(&:machine_barcode).to_set
  human_barcodes = assets.map(&:human_barcode).to_set
  asset_barcodes.delete_if do |barcode|
    human_barcodes.include?(barcode) || machine_barcodes.include?(barcode)
  end
end

#new_record?Boolean

Returns:

  • (Boolean)

28
# File 'app/models/labwhere_reception.rb', line 28

def new_record?; true; end

#persisted?Boolean

Returns:

  • (Boolean)

26
# File 'app/models/labwhere_reception.rb', line 26

def persisted?; false; end

#saveObject

save attempts to perform the actions, and returns true if it was successful This maintains compatibility with rails


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/labwhere_reception.rb', line 36

def save
  return false unless valid?

  begin
    scan = LabWhereClient::Scan.create(
      location_barcode: location_barcode,
      user_code: user_code,
      labware_barcodes: asset_barcodes
    )

    unless scan.valid?
      errors.add(:scan, scan.error)
      return false
    end
  rescue LabWhereClient::LabwhereException => e
    errors.add(:base, 'Could not connect to Labwhere.')
    return false
  end

  assets.each do |asset|
    asset.events.create_scanned_into_lab!(location_barcode, user.)
    BroadcastEvent::LabwareReceived.create!(seed: asset, user: user, properties: { location_barcode: location_barcode })
  end

  valid?
end

#userObject


30
31
32
# File 'app/models/labwhere_reception.rb', line 30

def user
  @user ||= User.find_with_barcode_or_swipecard_code(@user_code)
end