Skip to content

Commit e45fd31

Browse files
committed
don't generate broadcast multiple times
1 parent dc8a7e5 commit e45fd31

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/mix/tasks/phx.gen.context.ex

+7
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ defmodule Mix.Tasks.Phx.Gen.Context do
190190
defp inject_schema_access(%Context{file: file} = context, paths, binding) do
191191
ensure_context_file_exists(context, paths, binding)
192192

193+
binding =
194+
if File.exists?(file) and File.read!(file) =~ "defp broadcast" do
195+
[{:add_broadcast, false} | binding]
196+
else
197+
[{:add_broadcast, true} | binding]
198+
end
199+
193200
paths
194201
|> Mix.Phoenix.eval_from(
195202
"priv/templates/phx.gen.context/#{schema_access_template(context)}",

priv/templates/phx.gen.context/schema_access_scope.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
key = scope.<%= Enum.join(scope.access_path, ".") %>
1717

1818
Phoenix.PubSub.subscribe(<%= inspect context.base_module %>.PubSub, "<%= scope.name %>:#{key}:<%= schema.plural %>")
19-
end
19+
end<%= if add_broadcast do %>
2020

2121
defp broadcast(%<%= inspect scope.alias %>{} = scope, message) do
2222
key = scope.<%= Enum.join(scope.access_path, ".") %>
2323

2424
Phoenix.PubSub.broadcast(<%= inspect context.base_module %>.PubSub, "<%= scope.name %>:#{key}:<%= schema.plural %>", message)
25-
end
25+
end<% end %>
2626

2727
@doc """
2828
Returns the list of <%= schema.plural %>.

0 commit comments

Comments
 (0)