diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index c65f37532..ddd0f09da 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -2,21 +2,22 @@ class WebhooksController < ApplicationController include FilterScoped before_action :ensure_can_administer + before_action :set_collection before_action :set_webhook, except: %i[ index new create ] def index - set_page_and_extract_portion_from Webhook.all.ordered + set_page_and_extract_portion_from @collection.webhooks.ordered end def show end def new - @webhook = Webhook.new + @webhook = @collection.webhooks.new end def create - @webhook = Webhook.new(webhook_params) + @webhook = @collection.webhooks.new(webhook_params) if @webhook.save redirect_to @webhook @@ -38,15 +39,21 @@ class WebhooksController < ApplicationController def destroy @webhook.destroy! - redirect_to webhooks_path, status: :see_other + redirect_to collection_webhooks_path, status: :see_other end private + def set_collection + @collection = Collection.find(params[:collection_id]) + end + def set_webhook - @webhook = Webhook.find(params[:id]) + @webhook = @collection.webhooks.find(params[:id]) end def webhook_params - params.expect webhook: [ :name, :url, subscribed_actions: [] ] + params + .expect(webhook: [ :name, :url, subscribed_actions: [] ]) + .merge(collection_id: @collection.id) end end diff --git a/app/models/collection.rb b/app/models/collection.rb index 4723ccd64..0d8fdeb37 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -8,6 +8,7 @@ class Collection < ApplicationRecord has_many :cards, dependent: :destroy has_many :tags, -> { distinct }, through: :cards has_many :events + has_many :webhooks, dependent: :destroy has_one :entropy_configuration, class_name: "Entropy::Configuration", as: :container, dependent: :destroy scope :alphabetically, -> { order("lower(name)") } diff --git a/app/models/webhook.rb b/app/models/webhook.rb index defb57186..9fbdada34 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -26,6 +26,8 @@ class Webhook < ApplicationRecord has_many :deliveries, dependent: :delete_all has_one :delinquency_tracker, dependent: :delete + belongs_to :collection + serialize :subscribed_actions, type: Array, coder: JSON scope :ordered, -> { order(name: :asc, id: :desc) } diff --git a/app/models/webhook/triggerable.rb b/app/models/webhook/triggerable.rb index 4e8afeb43..a2f5ea186 100644 --- a/app/models/webhook/triggerable.rb +++ b/app/models/webhook/triggerable.rb @@ -2,7 +2,7 @@ module Webhook::Triggerable extend ActiveSupport::Concern included do - scope :triggered_by, ->(event) { triggered_by_action(event.action) } + scope :triggered_by, ->(event) { where(collection: event.collection).triggered_by_action(event.action) } scope :triggered_by_action, ->(action) { where("subscribed_actions LIKE ?", "%\"#{action}\"%") } end diff --git a/app/views/account/settings/show.html.erb b/app/views/account/settings/show.html.erb index 492ee6cb4..6d41d439d 100644 --- a/app/views/account/settings/show.html.erb +++ b/app/views/account/settings/show.html.erb @@ -4,13 +4,6 @@ <%= render "filters/menu", user_filtering: @user_filtering %>