Skip to content

Instantly share code, notes, and snippets.

@cored
Created January 30, 2018 20:28
Show Gist options
  • Save cored/8b16e71dd91f412a66fd46486ae403ea to your computer and use it in GitHub Desktop.
Save cored/8b16e71dd91f412a66fd46486ae403ea to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: shipment_errors
#
# id :integer not null, primary key
# shipment_id :integer
# handled_by_id :integer
# message :text
# created_at :datetime
# deleted_at :datetime
# type :string(255)
# updated_at :datetime
# parent_shipment_type :string
# parent_shipment_id :integer
#
class ShipmentError < ActiveRecord::Base
acts_as_paranoid
belongs_to :parent_shipment, -> { with_deleted }, polymorphic: true
belongs_to :handled_by, class_name: 'User'
before_save :fill_in_deprecated_shipment_fields
validates_uniqueness_of :message, scope: [:parent_shipment_type, :parent_shipment_id, :deleted_at]
after_create :send_statsd_metric
scope :recent, -> { where 'shipment_errors.created_at > ?', Time.current - 24.hours }
class << self
def ransackable_scopes(auth_object=nil)
super + [:from_shipment_type]
end
def from_shipment_type(parent_shipment_type)
where(parent_shipment_type: parent_shipment_type)
end
end
scope :from_shipments_at_warehouse, lambda { |warehouse_id|
joins(
'INNER JOIN shipments
ON shipments.id = shipment_errors.parent_shipment_id
AND shipment_errors.parent_shipment_type = \'Shipment\''
).where('shipments.warehouse_id = ?', warehouse_id)
}
scope :from_bulk_shipments_at_warehouse, lambda { |warehouse_id|
joins(
'INNER JOIN bulk_shipments
ON bulk_shipments.id = shipment_errors.parent_shipment_id
AND shipment_errors.parent_shipment_type = \'BulkShipment\''
).where('bulk_shipments.warehouse_id = ?', warehouse_id)
}
private
def send_statsd_metric
Datadog::EmitShipmentErrorEvent.call(shipment_error: self)
end
def fill_in_deprecated_shipment_fields
self.shipment_id = parent_shipment_id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment