webhooks_controller.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. module Admin
  3. class WebhooksController < BaseController
  4. before_action :set_webhook, except: [:index, :new, :create]
  5. def index
  6. authorize :webhook, :index?
  7. @webhooks = Webhook.page(params[:page])
  8. end
  9. def show
  10. authorize @webhook, :show?
  11. end
  12. def new
  13. authorize :webhook, :create?
  14. @webhook = Webhook.new
  15. end
  16. def edit
  17. authorize @webhook, :update?
  18. end
  19. def create
  20. authorize :webhook, :create?
  21. @webhook = Webhook.new(resource_params)
  22. @webhook.current_account = current_account
  23. if @webhook.save
  24. redirect_to admin_webhook_path(@webhook)
  25. else
  26. render :new
  27. end
  28. end
  29. def update
  30. authorize @webhook, :update?
  31. @webhook.current_account = current_account
  32. if @webhook.update(resource_params)
  33. redirect_to admin_webhook_path(@webhook)
  34. else
  35. render :edit
  36. end
  37. end
  38. def enable
  39. authorize @webhook, :enable?
  40. @webhook.enable!
  41. redirect_to admin_webhook_path(@webhook)
  42. end
  43. def disable
  44. authorize @webhook, :disable?
  45. @webhook.disable!
  46. redirect_to admin_webhook_path(@webhook)
  47. end
  48. def destroy
  49. authorize @webhook, :destroy?
  50. @webhook.destroy!
  51. redirect_to admin_webhooks_path
  52. end
  53. private
  54. def set_webhook
  55. @webhook = Webhook.find(params[:id])
  56. end
  57. def resource_params
  58. params.require(:webhook).permit(:url, :template, events: [])
  59. end
  60. end
  61. end