Hooking `manage_relationship`

Is there a way to specifically hook into manage_relationship, e.g. for an extension? I'm looking to get notified any time a relationship is added or removed.
4 Replies
ZachDaniel
ZachDaniel2y ago
You can hook into the destination resource if certain actions are called And then customize the actions used in your manage relationship config
\ ឵឵឵
\ ឵឵឵OP2y ago
@Zach Daniel Delay... :thinkies: This one got by me somehow. Would you mind clarifying this a bit? If I understood, you mean setting the action on the relationship and then hooking that action on the destination? The end result I'm hoping to achieve is getting notified something like:
defmodule Relationships do
def add(relationship, from, to)
def remove(relationship, from, to)
end

# e.g.
Changes.add(:client, %Agency{...}, %Client{...})
defmodule Relationships do
def add(relationship, from, to)
def remove(relationship, from, to)
end

# e.g.
Changes.add(:client, %Agency{...}, %Client{...})
ZachDaniel
ZachDaniel2y ago
So if you have a notifier on the destination resource, you could monitor when one is created w/ an agency_id or when the agency_id is updated. That would be one way. Alternatively, you could add a custom change that you add to actions that manage relationships, that looks at the arguments and sends notifications about it. or you could add a special action on client, i.e :relate_to_agency and then in your manage relationship type: :append_and_remove, on_no_match: {:relate, :relate_to_agency} then if that relate_to_agency action is called, you know its being added to an agency
\ ឵឵឵
\ ឵឵឵OP2y ago
Right on, I'd probably go for the first option, as I can readily see it being an inherent requirement that the extension is present on both ends of the relationship (as the case may be, belongs_to being the exception), and this then seems very foolproof, handling situations that happen outside the manage_relationship purview as well. Thanks mate!

Did you find this page helpful?