Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions elixir/20170420_console.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,20 @@ production server to debug a problem (in which case it's not an `iex -S mix` but

When I do this, I always need to alias a lot of stuff or type a lot of prefixed modules, which is tiresome.

To avoid this, I have made a `Console` helper module in most of my projects.
To avoid this, I have made a file with aliases and imports in most of my projects.

It looks something like this

```elixir
# lib/my_app/console.ex
defmodule MyApp.Console do
@moduledoc """
Makes it easier to work in a console.

Usage:

use MyApp.Console

Aliases common modules and imports Ecto.Query
"""

defmacro __using__([]) do
quote do
alias MyApp.Account
alias MyApp.User
# ... more commonly used modules here

alias MyApp.Repo
import Ecto.Query
import Ecto.Changeset
:ok
end
end
end
# my_project/.iex.exs

alias MyApp.Account
alias MyApp.User
# ... more commonly used modules here

alias MyApp.Repo
import Ecto.Query
import Ecto.Changeset
```

When I then start an interactive shell, I just `use MyApp.Console` and everything is aliased for me.
When I then start an interactive shell, this file is automatically loaded and everything is aliased for me.