mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
39 lines
1 KiB
Elixir
39 lines
1 KiB
Elixir
defmodule PhilomenaWeb.MapParameterPlug do
|
|
# A bunch of crappy behaviors all interacting to create a
|
|
# symphony of failure:
|
|
#
|
|
# 1.) Router helpers do not strip nil query parameters.
|
|
# iex> ~p"/galleries?#{[gallery: nil]}"
|
|
# "/galleries?gallery="
|
|
#
|
|
# 2.) Pagination always sets the parameter in the route in order
|
|
# to preserve the query across multiple pages
|
|
#
|
|
# 3.) When received by the router, an empty param is treated as
|
|
# an empty string instead of nil
|
|
#
|
|
# 4.) Phoenix.HTML.Form.form_for/2 raises an error if you try to
|
|
# use it on a conn object which is a string instead of a map
|
|
# (or nil)
|
|
|
|
@spec init(Keyword.t()) :: Keyword.t()
|
|
def init(opts) do
|
|
opts
|
|
end
|
|
|
|
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
|
|
def call(conn, opts) do
|
|
param = Keyword.fetch!(opts, :param)
|
|
value = conn.params[param]
|
|
|
|
cond do
|
|
is_map(value) ->
|
|
conn
|
|
|
|
true ->
|
|
params = Map.delete(conn.params, param)
|
|
|
|
Map.put(conn, :params, params)
|
|
end
|
|
end
|
|
end
|