marcandre

marcandre

Avoiding compile dependencies in router via plugs

I’m fighting against a mess of compile dependencies.

One issue is that many things have a runtime dependency on the router (ok) and that the router has multiple compile-time dependencies on views and layouts (not ok).

I notice that changing the modules of these views to Module.concat solves the problem:

  # router.ex

  # introduces a compile-time dependency
  pipeline :example do
    plug(:put_layout, {MyApp.LayoutView, :example})
  end
  # no dependency
  pipeline :example do
    plug(:put_layout, {Module.concat(:MyApp, :LayoutView), :example})
  end
  1. Is this expected / am I missing something?

  2. Before I write a macro to do this, is there a better way to resolve this?

This kind of issue does not seem unique.

I was naively thinking that this pattern was pretty natural and would be handled properly, either by put_layout accepting a string for the module name, or by plug being a smart macro that could avoid the compile-time dependency altogether, or by pipelines being in a different files from routes for example.

Most Liked

marcandre

marcandre

Very surprising to me, but moving a plug module from pipe_through and by using an intermediary pipeline removes the compile-time dependency?!

Again, is that expected, where is that documented, and can’t this be improved?

$ mix xref graph --source lib/bobby_web/router.ex --label compile  | wc -l
      113
$ git stash pop && git diff

lib/bobby_web/router.ex
──────────────────────────────────┐
429: defmodule BobbyWeb.Router do │
──────────────────────────────────┘

+ pipeline :active_policies do
+   plug Plugs.ActivePolicies
+ end

  scope "/", BobbyWeb do
-   pipe_through([:browser, Plugs.ActivePolicies])
+   pipe_through([:browser, :active_policies])

$ mix xref graph --source lib/bobby_web/router.ex --label compile  | wc -l
     86

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement