https://avo.cool logo
CRUD, Callbacks and Actions I have a general q...
# resources
w
CRUD, Callbacks and Actions I have a general question about how to handle, say, "side-effects". For example, in our application, a user has absences (e.g. a vacation from day x to day y). There is a lot of calculation going on in the background about how many days are needed for this timeframe, as there might be public holidays, and the employee might not work on fridays, etc. So when changing the start and end date of the absence, I need to recalculate stuff, create some dependent records, etc. When writing the controller myself, I would either do this in the controller, together with the
absence.update
, or just call some kind of "Service" all together, which handles the update and all side effects. However, with Avo, I'm not writing the controller myself - and as far as I can tell from the documentation, I can only set some redirect paths and similar in the controllers. One way to solve this would be callbacks in my model, a
before_update :update_absence_days
. This works, however, I generally try to avoid callbacks, as they are hard to reason about, especially in large applications, and they make testing harder, especially when creating records with FactoryBot Another way I found are actions, where I can mimic the editable fields and then do all of my logic inside the
def handle
method. That works well, and I can disable the regular edit with authorization. However, it is rather verbose to add an action for each of the modifying CRUD operations (to be fair, I currently only need these side effects for very few ressources, but that might change). Are there any other ways to handle these kinds of side effects that I'm missing? Did you maybe think about this topic and came to something like a "suggested solution"? Thanks for any input! 🙂
m
Thread automatically created by Tim in #1141725701549072486
l
Hi
> However, with Avo, I'm not writing the controller myself You can take over the controller for this kind of situations
A few methods are documented but you can hook whichever method, all your resurce controllers inherit from here https://github.com/avo-hq/avo/blob/main/app/controllers/avo/base_controller.rb
Let me know if this works for you or still hitting obstacles
w
You mean I could e.g. overwrite
def save_record_action
, and instead of implementing this with
@record.save!
, I could add my side-effects there? That would indeed help a lot, I think! However, those methods are all private - wouldn't they need to be protected in order to be overwritable? (Not quite sure with Ruby, as this is very lax with these things. I can try it out later 🙂 )
l
You can override those for sure, any method, and if you don't need to fully rewrite you can also add logic before, around or after each one (check rails' [controllers callbacks](https://guides.rubyonrails.org/action_controller_overview.html#controller-callbacks))
w
Well, the callbacks are just for the whole update-action, and my side effects need to happen together with the actual saving, wrapped in a transaction. But overriding works like a charm, e.g.:
Copy code
def save_record_action
  @record.transaction do
    @record.update_absence_days
    @record.save
  end
end
Might be worth a hint in the docs, together with keywords like side-effects, callbacks or similar 😄 Thanks for your help!
l
True! Thanks for the feedback, docs indeed miss important pieces on that page, I'll make sure to update those
Anytime!
2 Views