Class: Shipment::AuditForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, StripAttributes
Defined in:
app/models/shipment/audit_form.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/shipment/audit_form.rb', line 23

def execute
  updated_count = 0
  if attributes.present?
    shipments = Shipment.ransack(attributes).result
    Shipment.transaction do 
      shipments.find_each do |shp|
        # Instead of a save let's do a db straight shot in on transaction for speed
        if shp.run_audit
          updated_count += 1
          shp.update_columns(audited: true, 
                             audit_result: shp.audit_result, 
                             issues_count: shp.issues_count,
                             updated_at: Time.current)
        end
      end
    end
    return Result.new(success: true, updated_count: updated_count, message: "Audit was performed on #{updated_count} shipment(s)")
  else
    return Result.new(success: false, updated_count: 0, message: "No criteria to apply, cannot run audit")
  end
end

#to_keyObject



19
20
21
# File 'app/models/shipment/audit_form.rb', line 19

def to_key
  %w[audit_form]
end