7

I'm new to Elixir/Phoenix and I was wondering if there was a way to essentially require IEx for the purpose of IEx.pry in the same manner that in Ruby/Rails you can add something like

group :test, :development do
  gem 'pry', require: 'pry'
end

which would allow you to binding.pry in essentially any file without needing to include require 'pry'

I'm finding that having to require IEx on any controller, model, view, etc I want to debug to be tedious.

VitaminMarc
  • 193
  • 1
  • 8

1 Answers1

1

You can use web/web.ex file.

A module that keeps using definitions for controllers, views and so on.

This can be used in your application as:

use App.Web, :controller
use App.Web, :view

The definitions below will be executed for every view, controller, etc, so keep them short and clean, focused on imports, uses and aliases.

Just put require IEx here when you need it for controllers, models etc.

defmodule App.Web do
  def model do
    quote do
      ...
      require IEx
    end
  end
  def controller do
    quote do
      ...
      require IEx
    end
  end
end
droptheplot
  • 608
  • 6
  • 22
  • This will be done for `:prod` environment as well, which OP apparently doesn't want. – Dogbert Jan 07 '17 at 14:15
  • ^ Precisely. I had previously done exactly as @droptheplot suggested. It's a work-around, but I was hoping for something more dev/test specific. – VitaminMarc Jan 12 '17 at 17:04
  • Can someone tell me why `if Mix.env == "dev", do: require IEx` does not work? – Joe Eifert Feb 19 '17 at 13:31
  • @Johannes When the compiler tries to expand the macro `IEx.pry` it has not yet evaluated the conditional (`if` being a macro itself). You can use `IEx.pry` in the same condition in which you `require IEx` though. Also note what the docs say for `Mix.html#env/0`: _This function should not be used at runtime in application code"_ – J. Random Coder Jan 24 '18 at 02:22