I want to create and delete the user on a 3rd party service based on the below scenarios
create user on 3rd party
active from inactive (i have a column on my User model called is_active)delete user on 3rd party
inactivelooks like I can make use of the after_commit callback, but how do I identify in the after_commit that action is create, update or delete
Any help on this will be helpful.
Don't use a callbacks for this -- you are going to regret it.
The main problem with callbacks are:
I really can't understate this when you seem to be dealing with a third party API as well. Using an implicit mechanism like callbacks when you're touching the application boundary is a really bad idea. The whole idea of piping everything through a single method is also not sound.
Instead you can use patterns such as service objects to handle the "transformations" of the model.
class UserCreationService
def initialize(user)
@user = user
end
def perform
# do something with @user
end
end
class UserInactivationService
def initialize(user)
@user = user
end
def perform
# do something with @user
end
end
These do a single job and are easy to test and will only fire when you explicitly want them to. ActiveJob is actually an example of this pattern.