How to make this 'busy' action better?

As you can see I have a delete action here.

  def delete(%Plug.Conn{assigns: %{current_user: current_user}} = conn, _) do
    {deleted_sessions_count, _} = Sessions.delete_user_sessions(current_user)

    conn
    |> delete_session(:phauxth_session_id)
    |> Remember.delete_rem_cookie()
    |> put_flash(:info, "User successfully logged out.")
    |> redirect(to: Routes.page_path(conn, :index))
  end

it kind of feels busy, what can I do to make this better?

You can customize the action plug:

  def action(conn, _) do
    apply(__MODULE__, action_name(conn), [conn, conn.params, conn.assigns.current_user])
  end

Then rewrite your delete action:

def delete(conn, _, current_user) do
  # your code here
end

Actually you have to rewrite all other actions in the same module as delete action.

1 Like

I usually do:

  def action(conn, _) do
    apply(__MODULE__, action_name(conn), [conn, conn.params, conn.assigns])
  end

So I can simply pattern match on the actions for everything I need out of the assigns.

3 Likes

Thanks you two good first tip, I will give it a try