Skip to content

Commit c3fff01

Browse files
committed
Remove Ecto references from AGENTS.md for --no-ecto projects
1 parent 60a009b commit c3fff01

4 files changed

Lines changed: 33 additions & 28 deletions

File tree

installer/test/phx_new_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,12 @@ defmodule Mix.Tasks.Phx.NewTest do
555555
assert file =~ "inputs: [\"*.{heex,ex,exs}\", \"{config,lib,test}/**/*.{heex,ex,exs}\"]"
556556
refute file =~ "subdirectories:"
557557
end)
558+
559+
assert_file("phx_blog/AGENTS.md", fn file ->
560+
refute file =~ "Ecto"
561+
refute file =~ "changeset"
562+
refute file =~ "Ecto.Schema"
563+
end)
558564
end)
559565
end
560566

usage-rules/ecto.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,26 @@
77
- You **must** use `Ecto.Changeset.get_field(changeset, :field)` to access changeset fields
88
- Fields which are set programatically, such as `user_id`, must not be listed in `cast` calls or similar for security purposes. Instead they must be explicitly set when creating the struct
99
- **Always** invoke `mix ecto.gen.migration migration_name_using_underscores` when generating migration files, so the correct timestamp and conventions are applied
10+
11+
### Creating LiveView forms from changesets
12+
13+
When using changesets with LiveView, the underlying data, form params, and errors are retrieved from it. The `:as` option is automatically computed too. E.g. if you have a user schema:
14+
15+
defmodule MyApp.Users.User do
16+
use Ecto.Schema
17+
...
18+
end
19+
20+
And then you create a changeset that you pass to `to_form`:
21+
22+
%MyApp.Users.User{}
23+
|> Ecto.Changeset.change()
24+
|> to_form()
25+
26+
Once the form is submitted, the params will be available under `%{"user" => user_params}`.
27+
28+
In the template, the form assign can be passed to the `<.form>` function component:
29+
30+
<.form for={@form} id="todo-form" phx-change="validate" phx-submit="save">
31+
<.input field={@form[:field]} type="text" />
32+
</.form>

usage-rules/elixir.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
end
3030

3131
- **Never** nest multiple modules in the same file as it can cause cyclic dependencies and compilation errors
32-
- **Never** use map access syntax (`changeset[:field]`) on structs as they do not implement the Access behaviour by default. For regular structs, you **must** access the fields directly, such as `my_struct.field` or use higher level APIs that are available on the struct if they exist, `Ecto.Changeset.get_field/2` for changesets
32+
- **Never** use map access syntax on structs as they do not implement the Access behaviour by default. For structs, you **must** access the fields directly, such as `my_struct.field` or use higher level APIs that are available on the struct if they exist
3333
- Elixir's standard library has everything necessary for date and time manipulation. Familiarize yourself with the common `Time`, `Date`, `DateTime`, and `Calendar` interfaces by accessing their documentation as necessary. **Never** install additional dependencies unless asked or for date/time parsing (which you can use the `date_time_parser` package)
3434
- Don't use `String.to_atom/1` on user input (memory leak risk)
3535
- Predicate function names should not start with `is_` and should end in a question mark. Names like `is_thing` should be reserved for guards

usage-rules/liveview.md

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,6 @@ You can also specify a name to nest the params:
186186
{:noreply, assign(socket, form: to_form(user_params, as: :user))}
187187
end
188188

189-
#### Creating a form from changesets
190-
191-
When using changesets, the underlying data, form params, and errors are retrieved from it. The `:as` option is automatically computed too. E.g. if you have a user schema:
192-
193-
defmodule MyApp.Users.User do
194-
use Ecto.Schema
195-
...
196-
end
197-
198-
And then you create a changeset that you pass to `to_form`:
199-
200-
%MyApp.Users.User{}
201-
|> Ecto.Changeset.change()
202-
|> to_form()
203-
204-
Once the form is submitted, the params will be available under `%{"user" => user_params}`.
205-
206-
In the template, the form form assign can be passed to the `<.form>` function component:
207-
208-
<.form for={@form} id="todo-form" phx-change="validate" phx-submit="save">
209-
<.input field={@form[:field]} type="text" />
210-
</.form>
211-
212189
Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
213190

214191
#### Avoiding form errors
@@ -223,9 +200,8 @@ Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
223200
And **never** do this:
224201

225202
<%!-- NEVER do this (invalid) --%>
226-
<.form for={@changeset} id="my-form">
227-
<.input field={@changeset[:field]} type="text" />
203+
<.form for={@data} id="my-form">
204+
<.input field={@data[:field]} type="text" />
228205
</.form>
229206

230-
- You are FORBIDDEN from accessing the changeset in the template as it will cause errors
231-
- **Never** use `<.form let={f} ...>` in the template, instead **always use `<.form for={@form} ...>`**, then drive all form references from the form assign as in `@form[:field]`. The UI should **always** be driven by a `to_form/2` assigned in the LiveView module that is derived from a changeset
207+
- **Never** use `<.form let={f} ...>` in the template, instead **always use `<.form for={@form} ...>`**, then drive all form references from the form assign as in `@form[:field]`. The UI should **always** be driven by a `to_form/1` or `to_form/2` assigned in the LiveView module

0 commit comments

Comments
 (0)