diff --git a/app/controllers/webhooks/activations_controller.rb b/app/controllers/webhooks/activations_controller.rb new file mode 100644 index 000000000..bc72998f2 --- /dev/null +++ b/app/controllers/webhooks/activations_controller.rb @@ -0,0 +1,13 @@ +class Webhooks::ActivationsController < ApplicationController + before_action :set_webhook + + def create + @webhook.activate unless @webhook.active? + redirect_to @webhook, status: :see_other + end + + private + def set_webhook + @webhook = Webhook.find(params[:webhook_id]) + end +end diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index e4cf469c1..65299121b 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -46,6 +46,6 @@ class WebhooksController < ApplicationController end def webhook_params - params.require(:webhook).permit(:name, :url, :active, subscribed_actions: []) + params.require(:webhook).permit(:name, :url, subscribed_actions: []) end end diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 02f13cadf..558124995 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -31,10 +31,12 @@ class Webhook < ApplicationRecord normalizes :subscribed_actions, with: ->(value) { Array.wrap(value).map { |e| e.to_s.strip.presence }.uniq & PERMITTED_ACTIONS } validates :name, presence: true - validates :subscribed_actions, presence: true - validate :validate_url + def activate + update_columns active: true + end + def deactivate update_columns active: false end diff --git a/app/models/webhook/delivery.rb b/app/models/webhook/delivery.rb index 9197d311e..387aef53b 100644 --- a/app/models/webhook/delivery.rb +++ b/app/models/webhook/delivery.rb @@ -19,7 +19,7 @@ class Webhook::Delivery < ApplicationRecord enum :state, %w[ pending in_progress completed errored ].index_by(&:itself), default: :pending - scope :chronologically, -> { order created_at: :asc, id: :asc } + scope :ordered, -> { order created_at: :desc, id: :desc } after_create_commit :deliver_later diff --git a/app/views/webhooks/edit.html.erb b/app/views/webhooks/edit.html.erb index 2aa886c8c..ceba7b793 100644 --- a/app/views/webhooks/edit.html.erb +++ b/app/views/webhooks/edit.html.erb @@ -11,11 +11,6 @@ <%= form.text_field :name, required: true, autofocus: true, class: "input", placeholder: "Name your Webhook…", data: { action: "keydown.esc@document->form#cancel" } %> -
This Webhook doesn't subscribe to any actions. It will never trigger.
+ <% else %> +This Webhook hasn't been triggered yet
<% else %>