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

Overview

Helper class scoped to Shipment.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#executeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/shipment/audit_form.rb', line 27

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
    Result.new(success: true, updated_count: updated_count, message: "Audit was performed on #{updated_count} shipment(s)")
  else
    Result.new(success: false, updated_count: 0, message: "No criteria to apply, cannot run audit")
  end
end

#to_keyObject



23
24
25
# File 'app/models/shipment/audit_form.rb', line 23

def to_key
  %w[audit_form]
end