In a phoenix / elixir project: What is a good place to put helper functions. For example: I use Timex to deal with dates but I have to do some transformations on the returned struct to use it with my DB driver (one uses the key :min, the other :minute, etc). I don't want to repeat this piece of code in every model and would write a simple function that does that. What would be a good place to put this? Just in a module within the /lib folder? Are there any guidelines on how to organize this?
A "helper function" is a function you write because you need that particular functionality in multiple places, and because it makes the code more readable. A good example is an average function. You'd write a function named avg or similar, that takes in a list of numbers, and returns the average value from that list.
I don't think there are guidelines yet. You can create modules grouped by topic, like MyApp.TimexHelpers
and then import those at the places you actually want to use the helpers.
The main difference between lib
and web
for Phoenix before 1.2 is the reloading behaviour. For changes under lib
to take effect you need to restart your server. So I tend to put most stuff under web
to make development easier. Maybe web/helpers
...
Since Phoenix 1.2 "the lib/ directory is now code reloaded by default along with web/ in development". See Changelog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With