Skip to content

Commit

Permalink
Simplify view helpers (phoenixframework#4920)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored Aug 21, 2022
1 parent 4342ab4 commit 5606ad8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
27 changes: 13 additions & 14 deletions installer/templates/phx_single/lib/app_name_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ defmodule <%= @web_namespace %> do
end
end

def channel do
quote do
use Phoenix.Channel<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
end
end

def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
Expand All @@ -96,22 +89,28 @@ defmodule <%= @web_namespace %> do
end
end

def channel do
quote do
use Phoenix.Channel<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
end
end

defp view_helpers do
quote do<%= if @html do %>
# Use all HTML functionality (forms, tags, etc)
# Import basic HTML rendering capabilities (tags, forms, etc)
use Phoenix.HTML

# Import and alias LiveView and .heex helpers (live_render, <.link>, <.form>, etc)
# Import .heex helpers (<.link>, <.form>, etc) and alias JS module
import Phoenix.LiveView.Helpers
alias Phoenix.LiveView.JS
<% end %>
# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View
# Include the router functionality defined above
unquote(verified_routes())

# All imports from the current project should be defined from here under
import <%= @web_namespace %>.ErrorHelpers<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %><%= if @html do %>
alias Phoenix.LiveView.JS<% end %>
unquote(verified_routes())
import <%= @web_namespace %>.Gettext<% end %>
end
end

Expand Down
27 changes: 13 additions & 14 deletions installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ defmodule <%= @web_namespace %> do
end
end

def channel do
quote do
use Phoenix.Channel<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
end
end

def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
Expand All @@ -97,22 +90,28 @@ defmodule <%= @web_namespace %> do
end
end

def channel do
quote do
use Phoenix.Channel<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
end
end

defp view_helpers do
quote do<%= if @html do %>
# Use all HTML functionality (forms, tags, etc)
# Import basic HTML rendering capabilities (tags, forms, etc)
use Phoenix.HTML

# Import and alias LiveView and .heex helpers (live_render, <.link>, <.form>, etc)
# Import .heex helpers (<.link>, <.form>, etc) and alias JS module
import Phoenix.LiveView.Helpers
alias Phoenix.LiveView.JS
<% end %>
# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View
# Include the router functionality defined above
unquote(verified_routes())

# All imports from the current project should be defined from here under
import <%= @web_namespace %>.ErrorHelpers<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %><%= if @html do %>
alias Phoenix.LiveView.JS<% end %>
unquote(verified_routes())
import <%= @web_namespace %>.Gettext<% end %>
end
end

Expand Down
2 changes: 1 addition & 1 deletion installer/templates/phx_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule <%= @web_namespace %>.Router do
live_dashboard "/dashboard", metrics: <%= @web_namespace %>.Telemetry
end
<% end %><%= if @mailer do %>
<% end %><%= if @mailer do %>
scope "/dev" do<%= if @html do %>
pipe_through :browser<% else %>
pipe_through [:fetch_session, :protect_from_forgery]<% end %>
Expand Down
4 changes: 2 additions & 2 deletions installer/test/phx_new_umbrella_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
# assets
assert_file web_path(@app, ".gitignore"), "/priv/static/assets/"
assert_file web_path(@app, ".gitignore"), "#{@app}_web-*.tar"
assert_file web_path(@app, ".gitignore"), ~r/\n$/
assert_file web_path(@app, ".gitignore"), ~r/\n$/
assert_file web_path(@app, "assets/css/app.css")
assert_file web_path(@app, "assets/css/phoenix.css")

Expand Down Expand Up @@ -745,7 +745,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
assert_file "another/lib/another/templates/layout/app.html.heex"

# assets
assert_file "another/.gitignore", ~r/\n$/
assert_file "another/.gitignore", ~r/\n$/
assert_file "another/priv/static/favicon.ico"
assert_file "another/priv/static/images/phoenix.png"
assert_file "another/assets/css/app.css"
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.html/controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web

describe "create <%= schema.singular %>" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, ~p"<%= schema.route_prefix %>", <%= schema.singular %>: @create_attrs)
conn = post(conn, ~p"<%= schema.route_prefix %>", <%= schema.singular %>: @create_attrs)

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == ~p"<%= schema.route_prefix %>/#{id}"
Expand Down

0 comments on commit 5606ad8

Please sign in to comment.