docbrown/app/controllers/admin/articles_controller.rb
Anna Buianova 4c9a40be41 Dispatch events to the webhook endpoints #3715 (#3872)
* Start with webhooks: table and model

* Start with webhooks api

* Start with webhooks api

* Webhook::Event class and events list

* Remove commented callbacks

* Start with sending events to webhook endpoints

* A couple of tests for Articles::Creator

* Send event to webhook endpoint on article destroy

* Dispatch event on article update

* Dispatch event when an article updated from admin

* Spec for the webhook job

* One more test for the dispatch event job

* Integration-like spec for event dispatching to webhook endpoints when creating an article

* Use Addressable::URI to parse endpoint url

* Add oj as a faster fast_jsonapi backend

* Renamed serializers

* Move article serialization out of a model

* Don't allow to create a webhook event with invalid type

* Add fields for ArticleSerializer

* Fix webhook event job specs

* Specify timeout when dispatching events

* Fix webhook job spec

* Change webhook events queue name

* Change serialized article fields for the dispatchable events

* Include user data when serializing an article for webhook event

* Moved decorating out of Webhook::DispatchEvent, fixed most specs

* Fix route in ArticleSerializer

* Move Article decoration to Webhook::PayloadAdapter

* Refactor image url helpers to avoid including helpers into serializer

* Fix specs

* Default url options for production
2019-09-07 13:17:45 -04:00

44 lines
1.3 KiB
Ruby

module Admin
class ArticlesController < Admin::ApplicationController
def create
resource = resource_class.new(resource_params)
authorize_resource(resource)
user = User.find(resource_params[:user_id])
resource = Articles::Creator.call(user, resource_params)
if resource.persisted?
redirect_to(
[namespace, resource],
notice: translate_with_resource("create.success"),
)
else
render :new, locals: {
page: Administrate::Page::Form.new(dashboard, resource)
}
end
end
def update
if requested_resource.update(resource_params)
Webhook::DispatchEvent.call("article_updated", requested_resource)
redirect_to(
[namespace, requested_resource],
notice: translate_with_resource("update.success"),
)
else
render :edit, locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource)
}
end
end
def destroy
Articles::Destroyer.call(requested_resource)
if requested_resource.destroyed?
flash[:notice] = translate_with_resource("destroy.success")
else
flash[:error] = requested_resource.errors.full_messages.join("<br/>")
end
redirect_to action: :index
end
end
end