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
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
Productive. Reliable. Fast. A productive web framework that does not compromise speed or maintainability.
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/phoenix.gen.resource.ex
defmodule Mix.Tasks.Phoenix.Gen.Resource do
use Mix.Task
alias Phoenix.Naming
@shortdoc "Generates resource files"
@moduledoc """
Generates a Phoenix resource.
mix phoenix.gen.resource User users name:string age:integer
The first argument is the module name followed by
its plural name (used for resources and schema).
The generated resource will contain:
* a model in web/models
* a view in web/views
* a controller in web/controllers
* a migration file for the repository
* default CRUD templates in web/templates
## Namespaced resources
Resources can be namespaced, for such, it is just necessary
to namespace the first argument of the generator:
mix phoenix.gen.resource Admin.User users name:string age:integer
"""
def run([singular,plural|attrs]) do
base = Mix.Phoenix.base
scoped = Naming.camelize(singular)
path = Naming.underscore(scoped)
singular = String.split(path, "/") |> List.last
module = Module.concat(base, scoped) |> inspect
alias = Module.split(module) |> List.last
route = String.split(path, "/") |> Enum.drop(-1) |> Kernel.++([plural]) |> Enum.join("/")
attrs = split_attrs(attrs)
migration = String.replace(path, "/", "_")
timestamp = timestamp()
binding = [path: path, singular: singular, module: module, attrs: attrs,
plural: plural, route: route, base: base, alias: alias, scoped: scoped,
types: types(attrs), inputs: inputs(attrs), defaults: defaults(attrs)]
Mix.Phoenix.copy_from source_dir, "", binding, [
{:eex, "migration.exs", "priv/repo/migrations/#{timestamp}_create_#{migration}.exs"},
{:eex, "controller.ex", "web/controllers/#{path}_controller.ex"},
{:eex, "model.ex", "web/models/#{path}.ex"},
{:eex, "edit.html.eex", "web/templates/#{path}/edit.html.eex"},
{:eex, "form.html.eex", "web/templates/#{path}/form.html.eex"},
{:eex, "index.html.eex", "web/templates/#{path}/index.html.eex"},
{:eex, "new.html.eex", "web/templates/#{path}/new.html.eex"},
{:eex, "show.html.eex", "web/templates/#{path}/show.html.eex"},
{:eex, "view.ex", "web/views/#{path}_view.ex"},
]
Mix.shell.info """
Add the resource to the proper scope in web/router.ex:
resources "/#{route}", #{scoped}Controller
and then update your repository by running migrations:
$ mix ecto.migrate
"""
end
def run(_) do
Mix.raise """
mix phoenix.gen.resource expects both singular and plural names
of the generated resource followed by any number of attributes:
mix phoenix.gen.resource User users name:string
"""
end
defp timestamp do
{{y, m, d}, {hh, mm, ss}} = :calendar.universal_time()
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
end
defp pad(i) when i < 10, do: << ?0, ?0 + i >>
defp pad(i), do: to_string(i)
defp split_attrs(attrs) do
Enum.map attrs, fn attr ->
case String.split(attr, ":", parts: 2) do
[key, value] -> {String.to_atom(key), String.to_atom(value)}
[key] -> {String.to_atom(key), :string}
end
end
end
defp types(attrs) do
Enum.into attrs, %{}, fn
{k, :uuid} -> {k, Ecto.UUID}
{k, :date} -> {k, Ecto.Date}
{k, :time} -> {k, Ecto.Time}
{k, :datetime} -> {k, Ecto.DateTime}
{k, v} -> {k, v}
end
end
defp inputs(attrs) do
Enum.into attrs, %{}, fn
{k, :integer} -> {k, :number_input}
{k, :float} -> {k, :number_input}
{k, :decimal} -> {k, :number_input}
{k, :boolean} -> {k, :checkbox}
{k, :date} -> {k, :date_select}
{k, :time} -> {k, :time_select}
{k, :datetime} -> {k, :datetime_select}
{k, _} -> {k, :text_input}
end
end
defp defaults(attrs) do
Enum.into attrs, %{}, fn
{k, :boolean} -> {k, ", default: false"}
{k, _} -> {k, ""}
end
end
defp source_dir do
Application.app_dir(:phoenix, "priv/templates/resource")
end
end