Skip to content

Commit

Permalink
Update phoenix.new and generators to Ecto 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Apr 17, 2016
1 parent 613ff4b commit 8b560cd
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 33 deletions.
14 changes: 7 additions & 7 deletions installer/lib/phoenix_new.ex
Expand Up @@ -426,17 +426,16 @@ defmodule Mix.Tasks.Phoenix.New do
{:sqlite_ecto, Sqlite.Ecto,
dev: [database: "db/#{app}_dev.sqlite"],
test: [database: "db/#{app}_test.sqlite", pool: Ecto.Adapters.SQL.Sandbox],
prod: [database: "db/#{app}_prod.sqlite"],
test_begin: "Ecto.Adapters.SQL.begin_test_transaction(#{module}.Repo)",
test_restart: "Ecto.Adapters.SQL.restart_test_transaction(#{module}.Repo, [])"}
prod: [database: "db/#{app}_prod.sqlite"]}
end
defp get_ecto_adapter("mongodb", app, module) do
{:mongodb_ecto, Mongo.Ecto,
dev: [database: "#{app}_dev"],
test: [database: "#{app}_test", pool_size: 1],
prod: [database: "#{app}_prod"],
test_begin: "",
test_restart: "Mongo.Ecto.truncate(#{module}.Repo, [])",
test_setup_all: "",
test_setup: "",
test_async: "Mongo.Ecto.truncate(#{module}.Repo, [])",
binary_id: true,
migration: false,
sample_binary_id: "111111111111111111111111"}
Expand All @@ -450,8 +449,9 @@ defmodule Mix.Tasks.Phoenix.New do
test: [username: user, password: pass, database: "#{app}_test", hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox],
prod: [username: user, password: pass, database: "#{app}_prod"],
test_begin: "Ecto.Adapters.SQL.begin_test_transaction(#{module}.Repo)",
test_restart: "Ecto.Adapters.SQL.restart_test_transaction(#{module}.Repo, [])"]
test_setup_all: "Ecto.Adapters.SQL.Sandbox.mode(#{module}.Repo, :manual)",
test_setup: ":ok = Ecto.Adapters.SQL.Sandbox.checkout(#{module}.Repo)",
test_async: "Ecto.Adapters.SQL.Sandbox.mode(#{module}.Repo, {:shared, self()})"]
end

defp kw_to_config(kw) do
Expand Down
4 changes: 3 additions & 1 deletion installer/templates/ecto/model_case.ex
Expand Up @@ -26,8 +26,10 @@ defmodule <%= application_module %>.ModelCase do
end

setup tags do
<%= adapter_config[:test_setup] %>

unless tags[:async] do
<%= adapter_config[:test_restart] %>
<%= adapter_config[:test_async] %>
end

:ok
Expand Down
9 changes: 5 additions & 4 deletions installer/templates/new/config/config.exs
Expand Up @@ -4,11 +4,12 @@
# This configuration file is loaded before any dependency and
# is restricted to this project.
use Mix.Config
<%= if namespaced? do %>
# Configures the namespace used by Phoenix generators

# General application configuration
config :<%= application_name %>,
app_namespace: <%= application_module %>
<% end %>
<%= if namespaced? do %> app_namespace: <%= application_module %>,<% end %>
ecto_repos: [<%= application_module %>.Repo]

# Configures the endpoint
config :<%= application_name %>, <%= application_module %>.Endpoint,
url: [host: "localhost"],
Expand Down
9 changes: 5 additions & 4 deletions installer/templates/new/mix.exs
Expand Up @@ -35,13 +35,13 @@ defmodule <%= application_module %>.Mixfile do
# Type `mix help deps` for examples and options.
defp deps do
[<%= phoenix_dep %>,<%= if ecto do %>
{:phoenix_ecto, "~> 2.0"},
{:phoenix_ecto, "~> 3.0-rc"},
{<%= inspect adapter_app %>, ">= 0.0.0"},<% end %><%= if html do %>
{:phoenix_html, "~> 2.3"},
{:phoenix_html, "~> 2.5"},
{:phoenix_live_reload, "~> 1.0", only: :dev},<% end %>
# TODO move to hex release
{:phoenix_pubsub, github: "phoenixframework/phoenix_pubsub"},
{:gettext, "~> 0.9"},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"}]
end<%= if ecto do %>

Expand All @@ -53,6 +53,7 @@ defmodule <%= application_module %>.Mixfile do
# See the documentation for `Mix` for more info on aliases.
defp aliases do
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"]]
"ecto.reset": ["ecto.drop", "ecto.setup"],
"test": ["ecto.create", "ecto.migrate", "test"]]
end<% end %>
end
6 changes: 4 additions & 2 deletions installer/templates/new/test/support/channel_case.ex
Expand Up @@ -32,8 +32,10 @@ defmodule <%= application_module %>.ChannelCase do
end

setup tags do
<%= if ecto do %> unless tags[:async] do
<%= adapter_config[:test_restart] %>
<%= if ecto do %> <%= adapter_config[:test_setup] %>

unless tags[:async] do
<%= adapter_config[:test_async] %>
end
<% end %>
:ok
Expand Down
6 changes: 4 additions & 2 deletions installer/templates/new/test/support/conn_case.ex
Expand Up @@ -33,8 +33,10 @@ defmodule <%= application_module %>.ConnCase do
end

setup tags do
<%= if ecto do %> unless tags[:async] do
<%= adapter_config[:test_restart] %>
<%= if ecto do %> <%= adapter_config[:test_setup] %>

unless tags[:async] do
<%= adapter_config[:test_async] %>
end
<% end %>
{:ok, conn: Phoenix.ConnTest.build_conn()}
Expand Down
4 changes: 1 addition & 3 deletions installer/templates/new/test/test_helper.exs
@@ -1,6 +1,4 @@
ExUnit.start
<%= if ecto do %>
Mix.Task.run "ecto.create", ~w(-r <%= application_module %>.Repo --quiet)
Mix.Task.run "ecto.migrate", ~w(-r <%= application_module %>.Repo --quiet)
<%= adapter_config[:test_begin] %>
<%= adapter_config[:test_setup_all] %>
<% end %>
2 changes: 0 additions & 2 deletions priv/templates/phoenix.gen.html/controller.ex
Expand Up @@ -3,8 +3,6 @@ defmodule <%= module %>Controller do

alias <%= module %>

plug :scrub_params, <%= inspect singular %> when action in [:create, :update]

def index(conn, _params) do
<%= plural %> = Repo.all(<%= alias %>)
render(conn, "index.html", <%= plural %>: <%= plural %>)
Expand Down
2 changes: 0 additions & 2 deletions priv/templates/phoenix.gen.json/controller.ex
Expand Up @@ -3,8 +3,6 @@ defmodule <%= module %>Controller do

alias <%= module %>

plug :scrub_params, <%= inspect singular %> when action in [:create, :update]

def index(conn, _params) do
<%= plural %> = Repo.all(<%= alias %>)
render(conn, "index.json", <%= plural %>: <%= plural %>)
Expand Down
13 changes: 7 additions & 6 deletions priv/templates/phoenix.gen.model/model.ex
Expand Up @@ -8,18 +8,19 @@ defmodule <%= module %> do
timestamps
end

@required_fields ~w(<%= Enum.map_join(attrs, " ", &elem(&1, 0)) %>)
@optional_fields ~w()
@required_fields [<%= Enum.map_join(attrs, ", ", &inspect(elem(&1, 0))) %>]
@optional_fields []

@doc """
Creates a changeset based on the `model` and `params`.
Creates a changeset based on the `struct` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
def changeset(struct, params \\ %{}) do

This comment has been minimized.

Copy link
@philipgiuliani

philipgiuliani May 16, 2016

Contributor

What do you think about another name instead of struct? Credo isn't happy about that, because its a bit unsafe to use struct (defined in Kernel).

[Warning] Parameter `struct` has same name as the `Kernel.struct` function.

This comment has been minimized.

Copy link
@josevalim

josevalim via email May 16, 2016

Member
struct
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
<%= for k <- uniques do %> |> unique_constraint(<%= inspect k %>)
<% end %> end
end

0 comments on commit 8b560cd

Please sign in to comment.