24
votes

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?

1

1 Answers

39
votes

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.