Skip to content

Commit

Permalink
Ensure that the html generator uses correct plural collection
Browse files Browse the repository at this point in the history
If the plural and singular term are the same when generating a resource,
the template expects a `_collection` value to be set, however this was
only being set by the live generators and not the html generators.

This commect uses the correct `schema.collection` value when generating
a controller.

This closes #5562
  • Loading branch information
Gazler committed Sep 12, 2023
1 parent 1c1d50e commit 0a7cef3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.html/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web

def index(conn, _params) do
<%= schema.plural %> = <%= inspect context.alias %>.list_<%= schema.plural %>()
render(conn, :index, <%= schema.plural %>: <%= schema.plural %>)
render(conn, :index, <%= schema.collection %>: <%= schema.plural %>)
end

def new(conn, _params) do
Expand Down
9 changes: 9 additions & 0 deletions test/mix/tasks/phx.gen.html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ defmodule Mix.Tasks.Phx.Gen.HtmlTest do
end)
end

test "with a matching plural and singular term", config do
in_tmp_project(config.test, fn ->
Gen.Html.run(~w(Tracker Series series value:integer))
assert_file("lib/phoenix_web/controllers/series_controller.ex", fn file ->
assert file =~ "render(conn, :index, series_collection: series)"
end)
end)
end

test "with --no-context no warning is emitted when context exists", config do
in_tmp_project(config.test, fn ->
Gen.Html.run(~w(Blog Post posts title:string))
Expand Down

0 comments on commit 0a7cef3

Please sign in to comment.