Best place for flash logic that will be used in multiple controllers

I’m working on a flash that will display some info to users when they first log in, it’s more complex than a basic greeting so there is a fair amount of work that is done to generate the flash (checking a user’s configuration, time of day, that sort of thing). Due to the nature of how the application works there are multiple controllers where this flash needs to be applied.
I’m wondering where the best place for the flash’s associated functions would be, I initially had them right in the controller but now that I’m realizing I will need at least two controllers it does not seem very DRY to keep all the logic in the controller.

I have a ‘LayoutContext’ module in my server that hosts such global things like that. In every render call in every controller I have a layout_data: LayoutContext.get_layout_data(conn) that does the DB/Cache lookups it needs to do and builds a response map that is passed to the templates as normal (primarily used by the layout template). I have data to build the list of modules they have access to, login data, etc… etc…

Honestly, it would be easier as a plug and just add it to the conn map in private or so, but this is old and I like explicitness, but I’d probably recommend just doing it as a plug… :slight_smile:

1 Like

Hey, thanks for your thoughts on that. I started writing a plug for it but I’m not too familiar with writing plugs, I was unsure of how to use the plug on the controller but not on all actions. I only wanted the flash message on some controller actions, I didn’t want it piped through all actions. Perhaps there is a way of doing that?
I ended up doing something similar to what you had with the LayoutContext, I made a FlashComponent with a function that pattern matches for the user’s info on the conn and then building the alert in there. This was deployed and worked fine, perhaps there was a better way of doing it but for now, I’m happy with the result.

1 Like