Hi Discourse community. I didn’t find a similar topic so I thought I’d start one.
I’m using a plugin to extend Discourse functionality for my organization’s purposes. One of these is rendering a JSON from email/unsubscribe/{KEY}
, as we’d like to control these options from a different server than the one we run our Discourse instance. That endpoint is under the purview of the EmailController, and by default the EmailControllerHelper sets instance variables for the controller before rendering a view. Instead, I’d like to return the EmailController as a JSON.
So my question at base is, is it possible to serialize a controller? i.e., can I define a serializer for the controller like this:
class ::EmailController
# THE FILE THIS MODIFIES: app/controllers/email_controller.rb
# Return a json
module ReturnAsJSON
def unsubscribe
super
render EmailUnsubscribeSerializer.new(self, root: false).to_json
end
end
prepend ReturnAsJSON
end
class EmailUnsubscribeSerializer < ApplicationSerializer
attributes :topic, :type, :unsubscribed_from_all, :user
def user
BasicUserSerializer.new(object.user, root: false).as_json
end
end
or am I better off brute forcing this?
class ::EmailController
# THE FILE THIS MODIFIES: app/controllers/email_controller.rb
# Return a json
module ReturnAsJSON
def unsubscribe
super
render json: {type: @type, unsubscribed_from_all: @unsubscribed_from_all, etc.}
end
end
prepend ReturnAsJSON
end
The 2nd approach looks like it works, so if the 1st approach doesn’t at least I have a decent workaround.