Right now I have a model Trip
that when saved, runs some callbacks. I want to isolate this behavior so that it happens only when it runs on the controller (create/update/destroy), so I want to remove the callbacks.
Assuming I have a service object DoSomething#call
which accepts trip
and does everything I need, what are my options to run it in create/update/destroy
?
I have a few ideas but they involve horror things like:
def create
super() do |success, failure|
@action_successful = failure.instance_of?(
InheritedResources::BlankSlate
) || failure.class.nil?
if @action_successful
DoSomething.call(Trip.find(params[:id]))
end
end
end
Which comes with a lot of downsides:
- The horrible way to detect if an action is successful
- No way to get the in-memory reference to the object being acted on (reload from db)
- Since I have no reference to the in memory object, it's quite problematic to run something during on destroy (no reference, can't reload)
Additional code as requested
class Trip
end
The custom service (I've multiples)
class SaveLastChangedTrip
def call(user, trip)
return if user.nil?
user.update_attributes!(last_trip: trip)
end
end
and the activeadmin file
ActiveAdmin.register Trip do
controller do
def update
if super() # This is pseudocode, I want to run this branch only if save is successful
SaveLastChangedTrip.call(current_user, resource)
end
end
end
end