Module: Webhooks::Outgoing::IssuingModel

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/webhooks/outgoing/issuing_model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_created_webhookObject



36
37
38
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 36

def generate_created_webhook
  generate_webhook("created")
end

#generate_deleted_webhookObject



44
45
46
47
48
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 44

def generate_deleted_webhook
  return false unless respond_to?(:team)
  return false if team&.being_destroyed?
  generate_webhook("deleted")
end

#generate_updated_webhookObject



40
41
42
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 40

def generate_updated_webhook
  generate_webhook("updated")
end

#generate_webhook(action) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 16

def generate_webhook(action)
  # we can only generate webhooks for objects that return their team.
  return unless respond_to? :team

  # Try to find an event type definition for this action.
  event_type = Webhooks::Outgoing::EventType.find_by(id: "#{self.class.name.underscore}.#{action}")

  # If the event type is defined as one that people can be subscribed to,
  # and this object has a team where an associated outgoing webhooks endpoint could be registered.
  if event_type && team

    # Only generate an event record if an endpoint is actually listening for this event type.
    if team.webhooks_outgoing_endpoints.listening_for_event_type_id(event_type.id).any?
      data = "Api::V1::#{self.class.name}Serializer".constantize.new(self).serializable_hash[:data]
      webhook = team.webhooks_outgoing_events.create(event_type_id: event_type.id, subject: self, data: data)
      webhook.deliver
    end
  end
end