Repository: phoenixframework/phoenix
Pull request #3745 (2020-04-05 12:48:43)

Add `impl true` to generators


Patch:
diff --git a/installer/templates/phx_live/templates/layout/root.html.leex b/installer/templates/phx_live/templates/layout/root.html.leex
index 3ff73ed27..adad0063e 100644
--- a/installer/templates/phx_live/templates/layout/root.html.leex
+++ b/installer/templates/phx_live/templates/layout/root.html.leex
@@ -14,8 +14,10 @@
       <section class="container">
         <nav role="navigation">
           <ul>
-            <li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li><%= if live and dashboard do %>
-            <li><%%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li><% end %>
+            <li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
+            <%= if dashboard do %><%%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
+              <li><%%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>
+            <%% end %><% end %>
           </ul>
         </nav>
         <a href="https://phoenixframework.org/" class="phx-logo">
diff --git a/installer/templates/phx_single/mix.exs b/installer/templates/phx_single/mix.exs
index 1d4d2565a..4cca83a2b 100644
--- a/installer/templates/phx_single/mix.exs
+++ b/installer/templates/phx_single/mix.exs
@@ -42,10 +42,10 @@ defmodule <%= app_module %>.MixProject do
       {:ecto_sql, "~> 3.4"},
       {<%= inspect adapter_app %>, ">= 0.0.0"},<% end %><%= if html do %><%= if live do %>
       {:phoenix_live_view, "~> 0.11.0"},
-      {:floki, ">= 0.0.0", only: :test},<% end %><%= if dashboard do %>
-      {:phoenix_live_dashboard, github: "phoenixframework/phoenix_live_dashboard"},<% end %>
+      {:floki, ">= 0.0.0", only: :test},<% end %>
       {:phoenix_html, "~> 2.11"},
-      {:phoenix_live_reload, "~> 1.2", only: :dev},<% end %>
+      {:phoenix_live_reload, "~> 1.2", only: :dev},<% end %><%= if dashboard do %>
+      {:phoenix_live_dashboard, github: "phoenixframework/phoenix_live_dashboard"},<% end %>
       {:telemetry_metrics, "~> 0.4"},
       {:telemetry_poller, "~> 0.4"},<%= if gettext do %>
       {:gettext, "~> 0.11"},<% end %>
diff --git a/installer/templates/phx_web/channels/user_socket.ex b/installer/templates/phx_web/channels/user_socket.ex
index 251ef75c2..011734599 100644
--- a/installer/templates/phx_web/channels/user_socket.ex
+++ b/installer/templates/phx_web/channels/user_socket.ex
@@ -15,6 +15,7 @@ defmodule <%= web_namespace %>.UserSocket do
   #
   # See `Phoenix.Token` documentation for examples in
   # performing token verification on connect.
+  @impl true
   def connect(_params, socket, _connect_info) do
     {:ok, socket}
   end
@@ -29,5 +30,6 @@ defmodule <%= web_namespace %>.UserSocket do
   #     <%= endpoint_module %>.broadcast("user_socket:#{user.id}", "disconnect", %{})
   #
   # Returning `nil` makes this socket anonymous.
+  @impl true
   def id(_socket), do: nil
 end
diff --git a/installer/templates/phx_web/router.ex b/installer/templates/phx_web/router.ex
index e4a9605c7..9471cbb67 100644
--- a/installer/templates/phx_web/router.ex
+++ b/installer/templates/phx_web/router.ex
@@ -19,7 +19,16 @@ defmodule <%= web_namespace %>.Router do
     pipe_through :browser
 
     <%= if live do %>live "/", PageLive, :index<% else %>get "/", PageController, :index<% end %>
-  end<%= if dashboard do %>
+  end
+
+  # Other scopes may use custom stacks.
+  # scope "/api", <%= web_namespace %> do
+  #   pipe_through :api
+  # end<% else %>
+
+  scope "/api", <%= web_namespace %> do
+    pipe_through :api
+  end<% end %><%= if dashboard do %>
 
   # Enables LiveDashboard only for development
   #
@@ -28,21 +37,13 @@ defmodule <%= web_namespace %>.Router do
   # If your application does not have an admins-only section yet,
   # you can use Plug.BasicAuth to set up some basic authentication
   # as long as you are also using SSL (which you should anyway).
-  if Mix.env() == :dev do
+  if Mix.env() in [:dev, :test] do
     import Phoenix.LiveDashboard.Router
 
-    scope "/" do
-      pipe_through :browser
+    scope "/" do<%= if html do %>
+      pipe_through :browser<% else %>
+      pipe_through [:fetch_session, :protect_from_forgery]<% end %>
       live_dashboard "/dashboard", metrics: <%= web_namespace %>.Telemetry
     end
   end<% end %>
-
-  # Other scopes may use custom stacks.
-  # scope "/api", <%= web_namespace %> do
-  #   pipe_through :api
-  # end<% else %>
-
-  scope "/api", <%= web_namespace %> do
-    pipe_through :api
-  end<% end %>
 end
diff --git a/installer/templates/phx_web/telemetry.ex b/installer/templates/phx_web/telemetry.ex
index 25e844454..ec20cd3bf 100644
--- a/installer/templates/phx_web/telemetry.ex
+++ b/installer/templates/phx_web/telemetry.ex
@@ -6,6 +6,7 @@ defmodule <%= web_namespace %>.Telemetry do
     Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
   end
 
+  @impl true
   def init(_arg) do
     children = [
       {:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
diff --git a/installer/templates/phx_web/templates/layout/app.html.eex b/installer/templates/phx_web/templates/layout/app.html.eex
index 7cf928e0b..6cddabbaa 100644
--- a/installer/templates/phx_web/templates/layout/app.html.eex
+++ b/installer/templates/phx_web/templates/layout/app.html.eex
@@ -14,6 +14,9 @@
         <nav role="navigation">
           <ul>
             <li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
+            <%= if dashboard do %><%%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
+              <li><%%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>
+            <%% end %><% end %>
           </ul>
         </nav>
         <a href="https://phoenixframework.org/" class="phx-logo">
diff --git a/priv/templates/phx.gen.channel/channel.ex b/priv/templates/phx.gen.channel/channel.ex
index f38be44fd..4c1cf8215 100644
--- a/priv/templates/phx.gen.channel/channel.ex
+++ b/priv/templates/phx.gen.channel/channel.ex
@@ -1,6 +1,7 @@
 defmodule <%= module %>Channel do
   use <%= web_module %>, :channel
 
+  @impl true
   def join("<%= singular %>:lobby", payload, socket) do
     if authorized?(payload) do
       {:ok, socket}
@@ -11,12 +12,14 @@ defmodule <%= module %>Channel do
 
   # Channels can be used in a request/response fashion
   # by sending replies to requests from the client
+  @impl true
   def handle_in("ping", payload, socket) do
     {:reply, {:ok, payload}, socket}
   end
 
   # It is also common to receive messages from the client and
   # broadcast to everyone in the current topic (<%= singular %>:lobby).
+  @impl true
   def handle_in("shout", payload, socket) do
     broadcast socket, "shout", payload
     {:noreply, socket}
diff --git a/priv/templates/phx.gen.live/form.ex b/priv/templates/phx.gen.live/form.ex
index 3ab2b2a67..a9adb0c71 100644
--- a/priv/templates/phx.gen.live/form.ex
+++ b/priv/templates/phx.gen.live/form.ex
@@ -3,6 +3,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
 
   alias <%= inspect context.module %>
 
+  @impl true
   def update(%{<%= schema.singular %>: <%= schema.singular %>} = assigns, socket) do
     changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>)
 
@@ -12,6 +13,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
      |> assign(:changeset, changeset)}
   end
 
+  @impl true
   def handle_event("validate", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do
     changeset =
       socket.assigns.<%= schema.singular %>
diff --git a/priv/templates/phx.gen.live/index.ex b/priv/templates/phx.gen.live/index.ex
index abdae671b..d59915c7e 100644
--- a/priv/templates/phx.gen.live/index.ex
+++ b/priv/templates/phx.gen.live/index.ex
@@ -4,10 +4,12 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
   alias <%= inspect context.module %>
   alias <%= inspect schema.module %>
 
+  @impl true
   def mount(_params, _session, socket) do
     {:ok, assign(socket, :<%= schema.plural %>, fetch_<%= schema.plural %>())}
   end
 
+  @impl true
   def handle_params(params, _url, socket) do
     {:noreply, apply_action(socket, socket.assigns.live_action, params)}
   end
@@ -30,6 +32,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
     |> assign(:<%= schema.singular %>, nil)
   end
 
+  @impl true
   def handle_event("delete", %{"id" => id}, socket) do
     <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
     {:ok, _} = <%= inspect context.alias %>.delete_<%= schema.singular %>(<%= schema.singular %>)
diff --git a/priv/templates/phx.gen.live/modal.ex b/priv/templates/phx.gen.live/modal.ex
index f6a940aba..219739c7e 100644
--- a/priv/templates/phx.gen.live/modal.ex
+++ b/priv/templates/phx.gen.live/modal.ex
@@ -1,6 +1,7 @@
 defmodule <%= inspect context.web_module %>.Modal do
   use <%= inspect context.web_module %>, :live_component
 
+  @impl true
   def render(assigns) do
     ~L"""
     <div id="<%%= @id %>" class="phx-modal"
@@ -18,6 +19,7 @@ defmodule <%= inspect context.web_module %>.Modal do
     """
   end
 
+  @impl true
   def handle_event("close", _, socket) do
     {:noreply, push_patch(socket, to: socket.assigns.return_to)}
   end
diff --git a/priv/templates/phx.gen.live/show.ex b/priv/templates/phx.gen.live/show.ex
index 4fe01a928..b75badea6 100644
--- a/priv/templates/phx.gen.live/show.ex
+++ b/priv/templates/phx.gen.live/show.ex
@@ -3,10 +3,12 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
 
   alias <%= inspect context.module %>
 
+  @impl true
   def mount(_params, _session, socket) do
     {:ok, socket}
   end
 
+  @impl true
   def handle_params(%{"id" => id}, _, socket) do
     {:noreply,
      socket


Test patch:
diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs
index cda0cd336..4783922c3 100644
--- a/installer/test/phx_new_test.exs
+++ b/installer/test/phx_new_test.exs
@@ -305,6 +305,46 @@ defmodule Mix.Tasks.Phx.NewTest do
     end
   end
 
+  test "new with no_dashboard" do
+    in_tmp "new with no_dashboard", fn ->
+      Mix.Tasks.Phx.New.run([@app_name, "--no-dashboard"])
+
+      assert_file "phx_blog/mix.exs", &refute(&1 =~ ~r":phoenix_live_dashboard")
+
+      assert_file "phx_blog/lib/phx_blog_web/templates/layout/app.html.eex", fn file ->
+        refute file =~ ~s|<%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home)|
+      end
+
+      assert_file "phx_blog/lib/phx_blog_web/endpoint.ex", fn file ->
+        assert file =~ ~s|defmodule PhxBlogWeb.Endpoint|
+        refute file =~ ~s|socket "/live"|
+        refute file =~ ~s|plug Phoenix.LiveDashboard.RequestLogger|
+      end
+    end
+  end
+
+  test "new with no_html" do
+    in_tmp "new with no_html", fn ->
+      Mix.Tasks.Phx.New.run([@app_name, "--no-html"])
+
+      assert_file "phx_blog/mix.exs", fn file ->
+        refute file =~ ~s|:phoenix_live_view|
+        assert file =~ ~s|:phoenix_live_dashboard|
+      end
+
+      assert_file "phx_blog/lib/phx_blog_web/endpoint.ex", fn file ->
+        assert file =~ ~s|defmodule PhxBlogWeb.Endpoint|
+        assert file =~ ~s|socket "/live"|
+        assert file =~ ~s|plug Phoenix.LiveDashboard.RequestLogger|
+      end
+
+      assert_file "phx_blog/lib/phx_blog_web/router.ex", fn file ->
+        refute file =~ ~s|pipeline :browser|
+        assert file =~ ~s|pipe_through [:fetch_session, :protect_from_forgery]|
+      end
+    end
+  end
+
   test "new with no_webpack" do
     in_tmp "new with no_webpack", fn ->
       Mix.Tasks.Phx.New.run([@app_name, "--no-webpack"])
@@ -382,16 +422,21 @@ defmodule Mix.Tasks.Phx.NewTest do
     end
   end
 
-  test "new with live without dashboard" do
-    in_tmp "new with live without dashboard", fn ->
+  test "new with live no_dashboard" do
+    in_tmp "new with live no_dashboard", fn ->
       Mix.Tasks.Phx.New.run([@app_name, "--live", "--no-dashboard"])
 
       assert_file "phx_blog/mix.exs", &refute(&1 =~ ~r":phoenix_live_dashboard")
 
-      # No Dashboard
       assert_file "phx_blog/lib/phx_blog_web/templates/layout/root.html.leex", fn file ->
         refute file =~ ~s|<%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home)|
       end
+
+      assert_file "phx_blog/lib/phx_blog_web/endpoint.ex", fn file ->
+        assert file =~ ~s|defmodule PhxBlogWeb.Endpoint|
+        assert file =~ ~s|socket "/live"|
+        refute file =~ ~s|plug Phoenix.LiveDashboard.RequestLogger|
+      end
     end
   end