2. Add model WebhookEndpoint [migration]

This will store the URL for each user that wants a notification.

We probably don't need URL validation (it's not done on Enterprise for example). It could be validated by browser input, and anyway will be validated if the webhook actually works or not.

Inspired by Keygen: https://keygen.sh/blog/how-to-build-a-webhook-system-in-rails-using-sidekiq/
This commit is contained in:
David Cook
2022-09-16 15:16:27 +10:00
parent de9546587a
commit 85c98c6d3e
4 changed files with 32 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# frozen_string_literal: true
# Records a webhook url to send notifications to
class WebhookEndpoint < ApplicationRecord
validates :url, presence: true
end

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
class CreateWebhookEndpoints < ActiveRecord::Migration[6.1]
def change
create_table :webhook_endpoints do |t|
t.string :url, null: false
t.timestamps null: false
end
end
end

View File

@@ -1189,6 +1189,12 @@ ActiveRecord::Schema.define(version: 2023_02_13_160135) do
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end
create_table "webhook_endpoints", force: :cascade do |t|
t.string "url", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "adjustment_metadata", "enterprises", name: "adjustment_metadata_enterprise_id_fk"

View File

@@ -0,0 +1,9 @@
# frozen_string_literal: true
require 'spec_helper'
describe WebhookEndpoint, type: :model do
describe "validations" do
it { is_expected.to validate_presence_of(:url) }
end
end