Sha256: 23d36327f48a4869cfe030a9956617836bbbb40057798ee81127f2f5936f0497

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 Bytes

Contents

# frozen_string_literals: true

module Jobshop
  class Order < ApplicationRecord
    self.primary_keys = %i[ organization_id order_id ]

    after_initialize { self.order_id ||= SecureRandom.uuid if new_record? }

    belongs_to :organization, inverse_of: :orders

    belongs_to :created_by, class_name: "Jobshop::Person",
      foreign_key: %i[ organization_id created_by_id ]

    belongs_to :company, inverse_of: :orders,
      foreign_key: %i[ organization_id company_id ]

    has_many :order_lines, inverse_of: :order,
      foreign_key: %i[ organization_id order_id ]

    around_save :notify_on_save

    def add_listener(listener)
      (@listeners ||= []) << listener
    end

    def notify_listeners(event_name, *args)
      @listeners && @listeners.each do |listener|
        if listener.respond_to?(event_name)
          listener.public_send(event_name, self, *args)
        end
      end
    end

    def notify_on_save
      yield
      notify_listeners(:on_save)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jobshop-0.0.163 app/models/jobshop/order.rb