Skip to content

Commit

Permalink
Remove duplication between generators
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 12, 2020
1 parent 8389d70 commit 46013fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 70 deletions.
48 changes: 13 additions & 35 deletions lib/mix/tasks/phx.gen.html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ defmodule Mix.Tasks.Phx.Gen.Html do
> over distinct contexts (such as `Accounts.User` and `Payments.User`).
The schema is responsible for mapping the database fields into an
Elixir struct.
Elixir struct. It is followed by an optional list of attributes,
with their respective names and types. See `mix phx.gen.schema`
for more information on attributes.
Overall, this generator will add the following files to `lib/`:
Expand All @@ -28,6 +30,8 @@ defmodule Mix.Tasks.Phx.Gen.Html do
* a controller in `lib/app_web/controllers/user_controller.ex`
* default CRUD templates in `lib/app_web/templates/user`
## The context app
A migration file for the repository and test files for the context and
controller features will also be generated.
Expand Down Expand Up @@ -58,42 +62,16 @@ defmodule Mix.Tasks.Phx.Gen.Html do
Which would generate a `lib/app_web/controllers/sales/user_controller.ex` and
`lib/app_web/views/sales/user_view.ex`.
## Generating without a schema or context file
In some cases, you may wish to bootstrap HTML templates, controllers, and
controller tests, but leave internal implementation of the context or schema
to yourself. You can use the `--no-context` and `--no-schema` flags for
file generation control.
## table
By default, the table name for the migration and schema will be
the plural name provided for the resource. To customize this value,
a `--table` option may be provided. For example:
mix phx.gen.html Accounts User users --table cms_users
## binary_id
Generated migration can use `binary_id` for schema's primary key
and its references with option `--binary-id`.
## Default options
This generator uses default options provided in the `:generators`
configuration of your application. These are the defaults:
config :your_app, :generators,
migration: true,
binary_id: false,
sample_binary_id: "11111111-1111-1111-1111-111111111111"
## Customising the context, schema, tables and migrations
You can override those options per invocation by providing corresponding
switches, e.g. `--no-binary-id` to use normal ids despite the default
configuration or `--migration` to force generation of the migration.
In some cases, you may wish to bootstrap HTML templates, controllers,
and controller tests, but leave internal implementation of the context
or schema to yourself. You can use the `--no-context` and `--no-schema`
flags for file generation control.
Read the documentation for `phx.gen.schema` for more information on
attributes.
You can also change the table name or configure the migrations to
use binary ids for primary keys, see `mix phx.gen.schema` for more
information.
"""
use Mix.Task

Expand Down
48 changes: 13 additions & 35 deletions lib/mix/tasks/phx.gen.json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ defmodule Mix.Tasks.Phx.Gen.Json do
> over distinct contexts (such as `Accounts.User` and `Payments.User`).
The schema is responsible for mapping the database fields into an
Elixir struct.
Elixir struct. It is followed by an optional list of attributes,
with their respective names and types. See `mix phx.gen.schema`
for more information on attributes.
Overall, this generator will add the following files to `lib/`:
Expand All @@ -30,6 +32,8 @@ defmodule Mix.Tasks.Phx.Gen.Json do
A migration file for the repository and test files for the context and
controller features will also be generated.
## The context app
The location of the web files (controllers, views, templates, etc) in an
umbrella application will vary based on the `:context_app` config located
in your applications `:generators` configuration. When set, the Phoenix
Expand Down Expand Up @@ -57,42 +61,16 @@ defmodule Mix.Tasks.Phx.Gen.Json do
Which would generate a `lib/app_web/controllers/sales/user_controller.ex` and
`lib/app_web/views/sales/user_view.ex`.
## Generating without a schema or context file
In some cases, you may wish to bootstrap JSON views, controllers, and
controller tests, but leave internal implementation of the context or schema
to yourself. You can use the `--no-context` and `--no-schema` flags for
file generation control.
## table
By default, the table name for the migration and schema will be
the plural name provided for the resource. To customize this value,
a `--table` option may be provided. For example:
mix phx.gen.json Accounts User users --table cms_users
## binary_id
Generated migration can use `binary_id` for schema's primary key
and its references with option `--binary-id`.
## Default options
This generator uses default options provided in the `:generators`
configuration of your application. These are the defaults:
config :your_app, :generators,
migration: true,
binary_id: false,
sample_binary_id: "11111111-1111-1111-1111-111111111111"
## Customising the context, schema, tables and migrations
You can override those options per invocation by providing corresponding
switches, e.g. `--no-binary-id` to use normal ids despite the default
configuration or `--migration` to force generation of the migration.
In some cases, you may wish to bootstrap JSON views, controllers,
and controller tests, but leave internal implementation of the context
or schema to yourself. You can use the `--no-context` and `--no-schema`
flags for file generation control.
Read the documentation for `phx.gen.schema` for more information on
attributes.
You can also change the table name or configure the migrations to
use binary ids for primary keys, see `mix phx.gen.schema` for more
information.
"""

use Mix.Task
Expand Down

0 comments on commit 46013fe

Please sign in to comment.