├── assets
├── .babelrc
├── static
│ ├── favicon.ico
│ ├── images
│ │ └── phoenix.png
│ └── robots.txt
├── postcss.config.js
├── package.json
├── webpack.config.js
├── js
│ ├── app.js
│ └── socket.js
└── css
│ ├── app.css
│ └── phoenix.css
├── test
├── test_helper.exs
├── tutorial_web
│ ├── views
│ │ ├── page_view_test.exs
│ │ ├── layout_view_test.exs
│ │ └── error_view_test.exs
│ └── controllers
│ │ ├── page_controller_test.exs
│ │ ├── product_controller_test.exs
│ │ └── secret_controller_test.exs
├── support
│ ├── channel_case.ex
│ ├── conn_case.ex
│ └── data_case.ex
└── tutorial
│ ├── accounts_test.exs
│ ├── taggable_test.exs
│ ├── secrets_test.exs
│ └── products_test.exs
├── lib
├── tutorial_web
│ ├── templates
│ │ ├── layout
│ │ │ ├── app.html.leex
│ │ │ └── root.html.leex
│ │ ├── page
│ │ │ ├── draggable.html.eex
│ │ │ ├── task_async.html.eex
│ │ │ ├── private.html.eex
│ │ │ ├── index.html.eex
│ │ │ ├── search_form.html.leex
│ │ │ └── calendar.html.leex
│ │ ├── secret
│ │ │ ├── new.html.eex
│ │ │ ├── edit.html.eex
│ │ │ ├── show.html.eex
│ │ │ ├── form.html.eex
│ │ │ └── index.html.eex
│ │ ├── product
│ │ │ ├── index.html.eex
│ │ │ ├── new.html.eex
│ │ │ ├── edit.html.eex
│ │ │ ├── products_loading.html.leex
│ │ │ ├── show.html.eex
│ │ │ ├── product_tagging.html.leex
│ │ │ ├── form.html.leex
│ │ │ └── products.html.leex
│ │ └── pow
│ │ │ ├── session
│ │ │ └── new.html.eex
│ │ │ └── registration
│ │ │ ├── edit.html.eex
│ │ │ └── new.html.eex
│ ├── views
│ │ ├── layout_view.ex
│ │ ├── secret_view.ex
│ │ ├── pow
│ │ │ ├── session_view.ex
│ │ │ └── registration_view.ex
│ │ ├── page_view.ex
│ │ ├── product_view.ex
│ │ ├── error_view.ex
│ │ └── error_helpers.ex
│ ├── plugs
│ │ ├── generate_csrf.ex
│ │ ├── assign_session.ex
│ │ └── set_current_account.ex
│ ├── controllers
│ │ ├── page_controller.ex
│ │ ├── product_controller.ex
│ │ └── secret_controller.ex
│ ├── channels
│ │ ├── app_channel.ex
│ │ └── user_socket.ex
│ ├── live
│ │ ├── modals_live.ex
│ │ ├── calendar_day_component.ex
│ │ ├── draggable_live.ex
│ │ ├── task_starter_live.ex
│ │ ├── calendar_live.ex
│ │ ├── product_form_live.ex
│ │ ├── product_list_live.ex
│ │ ├── modal_component.ex
│ │ ├── search_form_live.ex
│ │ ├── modals_live.html.leex
│ │ └── product_tagging_live.ex
│ ├── gettext.ex
│ ├── endpoint.ex
│ └── router.ex
├── tutorial
│ ├── presence.ex
│ ├── repo.ex
│ ├── secrets
│ │ └── secret.ex
│ ├── accounts
│ │ └── account.ex
│ ├── taggable
│ │ ├── tagging.ex
│ │ └── tag.ex
│ ├── worker.ex
│ ├── products
│ │ ├── product.ex
│ │ └── variant.ex
│ ├── users
│ │ └── user.ex
│ ├── taggable.ex
│ ├── application.ex
│ ├── draggable_server.ex
│ ├── accounts.ex
│ ├── secrets.ex
│ └── products.ex
├── tutorial.ex
├── tutorial_web.ex
└── mix
│ └── tasks
│ ├── swagger.gen.json.ex
│ └── tailwind.gen.html.ex
├── priv
├── repo
│ ├── migrations
│ │ ├── .formatter.exs
│ │ ├── 20200210061018_add_properties_to_products.exs
│ │ ├── 20200211160754_create_tags.exs
│ │ ├── 20200124063237_create_products.exs
│ │ ├── 20200313082034_create_accounts.exs
│ │ ├── 20200210062352_add_index_to_products_properties.exs
│ │ ├── 20200316074930_create_secrets.exs
│ │ ├── 20200315124813_create_users.exs
│ │ ├── 20200203173952_create_variants.exs
│ │ └── 20200211160936_create_taggings.exs
│ └── seeds.exs
├── templates
│ ├── tailwind.gen.html
│ │ ├── view.ex
│ │ ├── new.html.eex
│ │ ├── edit.html.eex
│ │ ├── form.html.eex
│ │ ├── show.html.eex
│ │ ├── index.html.eex
│ │ ├── controller.ex
│ │ └── controller_test.exs
│ └── swagger.gen.json
│ │ ├── changeset_view.ex
│ │ ├── fallback_controller.ex
│ │ ├── view.ex
│ │ ├── controller_test.exs
│ │ └── controller.ex
└── gettext
│ ├── en
│ └── LC_MESSAGES
│ │ └── errors.po
│ └── errors.pot
├── .formatter.exs
├── README.md
├── config
├── test.exs
├── prod.secret.exs
├── config.exs
├── prod.exs
└── dev.exs
├── .gitignore
├── mix.exs
└── mix.lock
/assets/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "@babel/preset-env"
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/test/test_helper.exs:
--------------------------------------------------------------------------------
1 | ExUnit.start()
2 | Ecto.Adapters.SQL.Sandbox.mode(Tutorial.Repo, :manual)
3 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/layout/app.html.leex:
--------------------------------------------------------------------------------
1 | <%= render @view_module, @view_template, assigns %>
2 |
--------------------------------------------------------------------------------
/priv/repo/migrations/.formatter.exs:
--------------------------------------------------------------------------------
1 | [
2 | import_deps: [:ecto_sql],
3 | inputs: ["*.exs"]
4 | ]
5 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/page/draggable.html.eex:
--------------------------------------------------------------------------------
1 | <%= live_render @conn, TutorialWeb.DraggableLive %>
2 |
--------------------------------------------------------------------------------
/assets/static/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andreaseriksson/tutorials/HEAD/assets/static/favicon.ico
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/page/task_async.html.eex:
--------------------------------------------------------------------------------
1 | <%= live_render @conn, TutorialWeb.TaskStarterLive %>
2 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/layout_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.LayoutView do
2 | use TutorialWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/secret_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.SecretView do
2 | use TutorialWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/assets/static/images/phoenix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andreaseriksson/tutorials/HEAD/assets/static/images/phoenix.png
--------------------------------------------------------------------------------
/lib/tutorial_web/views/pow/session_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.Pow.SessionView do
2 | use TutorialWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/pow/registration_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.Pow.RegistrationView do
2 | use TutorialWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/test/tutorial_web/views/page_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.PageViewTest do
2 | use TutorialWeb.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/test/tutorial_web/views/layout_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.LayoutViewTest do
2 | use TutorialWeb.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/lib/tutorial/presence.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Presence do
2 | use Phoenix.Presence, otp_app: :tutorial, pubsub_server: Tutorial.PubSub
3 | end
4 |
--------------------------------------------------------------------------------
/lib/tutorial/repo.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo do
2 | use Ecto.Repo,
3 | otp_app: :tutorial,
4 | adapter: Ecto.Adapters.Postgres
5 | use Scrivener, page_size: 10
6 | end
7 |
--------------------------------------------------------------------------------
/.formatter.exs:
--------------------------------------------------------------------------------
1 | [
2 | import_deps: [:ecto, :phoenix],
3 | inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
4 | subdirectories: ["priv/*/migrations"]
5 | ]
6 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/page/private.html.eex:
--------------------------------------------------------------------------------
1 |
2 | This page should be private and requires an account.
3 |
4 |
5 | Welcome <%= @current_user.email %>
6 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/view.ex:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>View do
2 | use <%= inspect context.web_module %>, :view
3 | end
4 |
--------------------------------------------------------------------------------
/assets/static/robots.txt:
--------------------------------------------------------------------------------
1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2 | #
3 | # To ban all spiders from the entire site uncomment the next two lines:
4 | # User-agent: *
5 | # Disallow: /
6 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/page/index.html.eex:
--------------------------------------------------------------------------------
1 |
2 | <%= live_render @conn, TutorialWeb.SearchFormLive %>
3 |
4 |
5 |
6 | <%= live_render @conn, TutorialWeb.CalendarLive %>
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Tutorial app in Phoenix Framework by:
2 |
3 |
4 |
5 | [https://fullstackphoenix.com](https://fullstackphoenix.com)
6 |
--------------------------------------------------------------------------------
/lib/tutorial_web/plugs/generate_csrf.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.GenerateCSRF do
2 | import Plug.Conn, only: [put_session: 3]
3 |
4 | def init(_opts), do: nil
5 |
6 | def call(conn, _opts), do: put_session(conn, :csrf_token, Phoenix.Controller.get_csrf_token())
7 | end
8 |
--------------------------------------------------------------------------------
/test/tutorial_web/controllers/page_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.PageControllerTest do
2 | use TutorialWeb.ConnCase
3 |
4 | test "GET /", %{conn: conn} do
5 | conn = get(conn, "/")
6 | assert html_response(conn, 200) =~ "Fullstack Phoenix"
7 | end
8 | end
9 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200210061018_add_properties_to_products.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.AddPropertiesToProducts do
2 | use Ecto.Migration
3 |
4 | def change do
5 | alter table(:products) do
6 | add :properties, :map, default: %{}
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/tutorial.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial do
2 | @moduledoc """
3 | Tutorial keeps the contexts that define your domain
4 | and business logic.
5 |
6 | Contexts are also responsible for managing your data, regardless
7 | if it comes from the database, an external API or others.
8 | """
9 | end
10 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/secret/new.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | <%= render "form.html", Map.put(assigns, :action, Routes.secret_path(@conn, :create)) %>
7 |
8 |
9 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/secret/edit.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | <%= render "form.html", Map.put(assigns, :action, Routes.secret_path(@conn, :update, @secret)) %>
7 |
8 |
9 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200211160754_create_tags.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateTags do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:tags) do
6 | add :name, :string, null: false
7 |
8 | timestamps()
9 | end
10 |
11 | create unique_index(:tags, [:name])
12 | end
13 | end
14 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/new.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | <%%= render "form.html", Map.put(assigns, :action, Routes.<%= schema.route_helper %>_path(@conn, :create)) %>
7 |
8 |
9 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/page_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.PageView do
2 | use TutorialWeb, :view
3 |
4 | def format_search_result(search_result, search_phrase) do
5 | split_at = String.length(search_phrase)
6 | {selected, rest} = String.split_at(search_result, split_at)
7 |
8 | "#{selected} #{rest}"
9 | end
10 | end
11 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200124063237_create_products.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateProducts do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:products) do
6 | add :name, :string
7 | add :description, :text
8 | add :price, :float
9 |
10 | timestamps()
11 | end
12 |
13 | end
14 | end
15 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200313082034_create_accounts.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateAccounts do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:accounts) do
6 | add :name, :string, null: false
7 |
8 | timestamps()
9 | end
10 |
11 | create unique_index(:accounts, [:name])
12 | end
13 | end
14 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/product_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ProductView do
2 | use TutorialWeb, :view
3 |
4 | def format_search_result(search_result, search_phrase) do
5 | split_at = String.length(search_phrase)
6 | {selected, rest} = String.split_at(search_result, split_at)
7 |
8 | "#{selected} #{rest}"
9 | end
10 | end
11 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200210062352_add_index_to_products_properties.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.AddIndexToProductsProperties do
2 | use Ecto.Migration
3 |
4 | def up do
5 | execute("CREATE INDEX products_properties ON products USING GIN(properties)")
6 | end
7 |
8 | def down do
9 | execute("DROP INDEX products_properties")
10 | end
11 | end
12 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/edit.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | <%%= render "form.html", Map.put(assigns, :action, Routes.<%= schema.route_helper %>_path(@conn, :update, @<%= schema.singular %>)) %>
7 |
8 |
9 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/index.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
6 | <%= live_render @conn, TutorialWeb.ProductListLive %>
7 |
8 |
--------------------------------------------------------------------------------
/lib/tutorial_web/plugs/assign_session.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.AssignSession do
2 | import Plug.Conn, only: [get_session: 2, put_session: 3]
3 |
4 | def init(options), do: options
5 |
6 | def call(conn, _opts) do
7 | case get_session(conn, :session_id) do
8 | nil -> put_session(conn, :session_id, Ecto.UUID.generate())
9 | _ -> conn
10 | end
11 | end
12 | end
13 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/new.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | <%= live_render @conn, TutorialWeb.ProductFormLive,
7 | session: %{"action" => Routes.product_path(@conn, :create), "csrf_token" => Plug.CSRFProtection.get_csrf_token()}
8 | %>
9 |
10 |
11 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/edit.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | <%= live_render @conn, TutorialWeb.ProductFormLive,
7 | session: %{"id" => @product.id, "action" => Routes.product_path(@conn, :update, @product), "csrf_token" => Plug.CSRFProtection.get_csrf_token()}
8 | %>
9 |
10 |
11 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200316074930_create_secrets.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateSecrets do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:secrets) do
6 | add :key, :string
7 | add :value, :string
8 | add :account_id, references(:accounts, on_delete: :delete_all), null: false
9 |
10 | timestamps()
11 | end
12 |
13 | create index(:secrets, [:account_id])
14 | end
15 | end
16 |
--------------------------------------------------------------------------------
/lib/tutorial/secrets/secret.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Secrets.Secret do
2 | use Ecto.Schema
3 | import Ecto.Changeset
4 |
5 | schema "secrets" do
6 | field :key, :string
7 | field :value, :string
8 |
9 | belongs_to :account, Tutorial.Accounts.Account
10 |
11 | timestamps()
12 | end
13 |
14 | @doc false
15 | def changeset(secret, attrs) do
16 | secret
17 | |> cast(attrs, [:key, :value])
18 | |> validate_required([:key, :value])
19 | end
20 | end
21 |
--------------------------------------------------------------------------------
/lib/tutorial_web/controllers/page_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.PageController do
2 | use TutorialWeb, :controller
3 |
4 | def index(conn, _params) do
5 | render(conn, "index.html")
6 | end
7 |
8 | def draggable(conn, _params) do
9 | render(conn, "draggable.html")
10 | end
11 |
12 | def task_async(conn, _params) do
13 | render(conn, "task_async.html")
14 | end
15 |
16 | def private(conn, _params) do
17 | render(conn, "private.html")
18 | end
19 | end
20 |
--------------------------------------------------------------------------------
/lib/tutorial/accounts/account.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Accounts.Account do
2 | use Ecto.Schema
3 | import Ecto.Changeset
4 |
5 | schema "accounts" do
6 | field :name, :string
7 |
8 | has_many :secrets, Tutorial.Secrets.Secret
9 |
10 | timestamps()
11 | end
12 |
13 | @doc false
14 | def changeset(account, attrs) do
15 | account
16 | |> cast(attrs, [:name])
17 | |> validate_required([:name])
18 | |> unique_constraint(:name, name: :accounts_name_index)
19 | end
20 | end
21 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200315124813_create_users.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateUsers do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:users) do
6 | add :account_id, references(:accounts, on_delete: :delete_all), null: false
7 |
8 | add :email, :string, null: false
9 | add :password_hash, :string
10 |
11 | timestamps()
12 | end
13 |
14 | create index(:users, [:account_id])
15 | create unique_index(:users, [:email])
16 | end
17 | end
18 |
--------------------------------------------------------------------------------
/test/tutorial_web/views/error_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ErrorViewTest do
2 | use TutorialWeb.ConnCase, async: true
3 |
4 | # Bring render/3 and render_to_string/3 for testing custom views
5 | import Phoenix.View
6 |
7 | test "renders 404.html" do
8 | assert render_to_string(TutorialWeb.ErrorView, "404.html", []) == "Not Found"
9 | end
10 |
11 | test "renders 500.html" do
12 | assert render_to_string(TutorialWeb.ErrorView, "500.html", []) == "Internal Server Error"
13 | end
14 | end
15 |
--------------------------------------------------------------------------------
/lib/tutorial/taggable/tagging.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Taggable.Tagging do
2 | use Ecto.Schema
3 | import Ecto.Changeset
4 |
5 | schema "taggings" do
6 | belongs_to :tag, Tutorial.Taggable.Tag
7 | belongs_to :product, Tutorial.Products.Product
8 |
9 | timestamps()
10 | end
11 |
12 | @doc false
13 | def changeset(tagging, attrs) do
14 | tagging
15 | |> cast(attrs, [])
16 | |> unique_constraint(:name, name: :taggings_tag_id_product_id_index)
17 | |> cast_assoc(:tag)
18 | end
19 | end
20 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200203173952_create_variants.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateVariants do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:variants) do
6 | add :name, :string, null: false
7 | add :value, :string, null: false
8 | add :product_id, references(:products, on_delete: :delete_all)
9 |
10 | timestamps()
11 | end
12 |
13 | create index(:variants, [:product_id])
14 | create unique_index(:variants, [:name, :value, :product_id])
15 | end
16 | end
17 |
--------------------------------------------------------------------------------
/lib/tutorial/taggable/tag.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Taggable.Tag do
2 | use Ecto.Schema
3 | import Ecto.Changeset
4 |
5 | schema "tags" do
6 | field :name, :string
7 |
8 | has_many :taggings, Tutorial.Taggable.Tagging
9 | has_many :products, through: [:taggings, :post]
10 |
11 | timestamps()
12 | end
13 |
14 | @doc false
15 | def changeset(tag, attrs) do
16 | tag
17 | |> cast(attrs, [:name])
18 | |> validate_required([:name])
19 | |> unique_constraint(:name, name: :tags_name_index)
20 | end
21 | end
22 |
--------------------------------------------------------------------------------
/priv/repo/migrations/20200211160936_create_taggings.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Repo.Migrations.CreateTaggings do
2 | use Ecto.Migration
3 |
4 | def change do
5 | create table(:taggings) do
6 | add :tag_id, references(:tags, on_delete: :delete_all)
7 | add :product_id, references(:products, on_delete: :delete_all)
8 |
9 | timestamps()
10 | end
11 |
12 | create index(:taggings, [:tag_id])
13 | create index(:taggings, [:product_id])
14 | create unique_index(:taggings, [:tag_id, :product_id])
15 | end
16 | end
17 |
--------------------------------------------------------------------------------
/config/test.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # Configure your database
4 | config :tutorial, Tutorial.Repo,
5 | username: "postgres",
6 | password: "postgres",
7 | database: "tutorial_test",
8 | hostname: "localhost",
9 | pool: Ecto.Adapters.SQL.Sandbox
10 |
11 | # We don't run a server during test. If one is required,
12 | # you can enable the server option below.
13 | config :tutorial, TutorialWeb.Endpoint,
14 | http: [port: 4002],
15 | server: false
16 |
17 | # Print only warnings and errors during test
18 | config :logger, level: :warn
19 |
--------------------------------------------------------------------------------
/lib/tutorial_web/plugs/set_current_account.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.Plugs.SetCurrentAccount do
2 | import Plug.Conn, only: [assign: 3]
3 |
4 | alias Tutorial.Repo
5 | alias Tutorial.Users.User
6 |
7 | def init(options), do: options
8 |
9 | def call(conn, _opts) do
10 | case conn.assigns[:current_user] do
11 | %User{} = user ->
12 | %User{account: account} = Repo.preload(user, :account)
13 | assign(conn, :current_account, account)
14 | _ ->
15 | assign(conn, :current_account, nil)
16 | end
17 | end
18 | end
19 |
--------------------------------------------------------------------------------
/lib/tutorial_web/channels/app_channel.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.AppChannel do
2 | use TutorialWeb, :channel
3 |
4 | def join("app:" <> token, _payload, socket) do
5 | {:ok, assign(socket, :channel, "app:#{token}")}
6 | end
7 |
8 | # can be triggered by from the frontend js by:
9 | # channel.push('paginate', message)
10 | def handle_in("paginate", payload, socket) do
11 | Phoenix.PubSub.broadcast(Tutorial.PubSub, socket.assigns.channel, {"paginate", payload})
12 | {:noreply, socket}
13 | end
14 |
15 | def handle_info(_, socket), do: {:noreply, socket}
16 | end
17 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/error_view.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ErrorView do
2 | use TutorialWeb, :view
3 |
4 | # If you want to customize a particular status code
5 | # for a certain format, you may uncomment below.
6 | # def render("500.html", _assigns) do
7 | # "Internal Server Error"
8 | # end
9 |
10 | # By default, Phoenix returns the status message from
11 | # the template name. For example, "404.html" becomes
12 | # "Not Found".
13 | def template_not_found(template, _assigns) do
14 | Phoenix.Controller.status_message_from_template(template)
15 | end
16 | end
17 |
--------------------------------------------------------------------------------
/assets/postcss.config.js:
--------------------------------------------------------------------------------
1 | const purgecss = require('@fullhuman/postcss-purgecss')({
2 |
3 | // Specify the paths to all of the template files in your project
4 | content: [
5 | '../lib/**/*.eex',
6 | '../lib/**/*.leex',
7 | ],
8 |
9 | // Include any special characters you're using in this regular expression
10 | defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
11 | })
12 |
13 | module.exports = {
14 | plugins: [
15 | require('tailwindcss'),
16 | require('autoprefixer'),
17 | ...process.env.NODE_ENV === 'production'
18 | ? [purgecss]
19 | : []
20 | ]
21 | }
22 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/form.html.eex:
--------------------------------------------------------------------------------
1 | <%%= form_for @changeset, @action, [class: "block"], fn f -> %>
2 | <%%= if @changeset.action do %>
3 |
4 |
The form couln't be saved! Please check the errors below.
5 |
6 | <%% end %>
7 | <%= for {label, input, error} <- inputs, input do %>
8 |
9 | <%= label %>
10 | <%= input %>
11 | <%= error %>
12 |
13 | <% end %>
14 |
15 | <%%= submit "Save", class: "btn btn-primary mr-2" %>
16 | <%%= link "Back", to: Routes.<%= schema.route_helper %>_path(@conn, :index), class: "btn btn-secondary" %>
17 |
18 | <%% end %>
19 |
--------------------------------------------------------------------------------
/priv/repo/seeds.exs:
--------------------------------------------------------------------------------
1 | # Script for populating the database. You can run it as:
2 | #
3 | # mix run priv/repo/seeds.exs
4 | #
5 | # Inside the script, you can read and write to any of your
6 | # repositories directly:
7 | #
8 | # Tutorial.Repo.insert!(%Tutorial.SomeSchema{})
9 | #
10 | # We recommend using the bang functions (`insert!`, `update!`
11 | # and so on) as they will fail if something goes wrong.
12 |
13 | alias Tutorial.Products
14 |
15 | Enum.each(1..100, fn _ ->
16 | Products.create_product(%{
17 | name: Faker.Beer.En.name,
18 | description: "#{Faker.Beer.En.style} - #{Faker.Beer.En.brand}",
19 | price: (Faker.random_between(200, 1000) / 100.0)
20 | })
21 | end)
22 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/modals_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ModalsLive do
2 | use Phoenix.LiveView
3 |
4 | # MODAL
5 | def handle_event("submit", %{"id" => id}, socket) do
6 | send_update TutorialWeb.ModalComponent, id: id, action: "CLOSE"
7 | {:noreply, socket}
8 | end
9 |
10 | def handle_event("open-modal", %{"id" => id}, socket) do
11 | send_update TutorialWeb.ModalComponent, id: id, state: "OPEN"
12 | {:noreply, socket}
13 | end
14 |
15 | def handle_event("close-modal", %{"id" => id}, socket) do
16 | :timer.sleep(300) # SO THE CSS ANIMATIONS HAVE TIME TO RUN
17 | send_update TutorialWeb.ModalComponent, id: id, state: "CLOSED", action: nil
18 | {:noreply, socket}
19 | end
20 | end
21 |
--------------------------------------------------------------------------------
/lib/tutorial/worker.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Worker do
2 | use Task, restart: :transient
3 |
4 | alias Tutorial.PubSub
5 | @topic inspect(__MODULE__)
6 |
7 | def start_link(arg) do
8 | Task.start_link(__MODULE__, :run, [arg])
9 | end
10 |
11 | def run(_arg) do
12 | :timer.sleep(2000)
13 | number = Enum.random(0..3)
14 |
15 | if number == 1 do
16 | IO.puts "CRASH"
17 | raise inspect(number)
18 | end
19 |
20 | number
21 | |> notify_subscribers()
22 | end
23 |
24 | defp notify_subscribers(number) do
25 | Phoenix.PubSub.broadcast(PubSub, @topic, {:message, number})
26 | end
27 |
28 | def subscribe do
29 | Phoenix.PubSub.subscribe(PubSub, @topic)
30 | end
31 | end
32 |
--------------------------------------------------------------------------------
/priv/templates/swagger.gen.json/changeset_view.ex:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.ChangesetView do
2 | use <%= inspect context.web_module %>, :view
3 |
4 | @doc """
5 | Traverses and translates changeset errors.
6 |
7 | See `Ecto.Changeset.traverse_errors/2` and
8 | `<%= inspect context.web_module %>.ErrorHelpers.translate_error/1` for more details.
9 | """
10 | def translate_errors(changeset) do
11 | Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
12 | end
13 |
14 | def render("error.json", %{changeset: changeset}) do
15 | # When encoded, the changeset returns its errors
16 | # as a JSON object. So we just pass it forward.
17 | %{errors: translate_errors(changeset)}
18 | end
19 | end
20 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/secret/show.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 | Key:
12 | <%= @secret.key %>
13 |
14 |
15 |
16 | Value:
17 | <%= @secret.value %>
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/pow/session/new.html.eex:
--------------------------------------------------------------------------------
1 | Sign in
2 |
3 | <%= form_for @changeset, @action, [as: :user], fn f -> %>
4 | <%= if @changeset.action do %>
5 |
6 |
Oops, something went wrong! Please check the errors below.
7 |
8 | <% end %>
9 |
10 | <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %>
11 | <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %>
12 | <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
13 |
14 | <%= label f, :password %>
15 | <%= password_input f, :password %>
16 | <%= error_tag f, :password %>
17 |
18 |
19 | <%= submit "Sign in" %>
20 |
21 | <% end %>
22 |
23 |
24 | <%= link "Register", to: Routes.pow_registration_path(@conn, :new) %>
25 |
--------------------------------------------------------------------------------
/lib/tutorial/products/product.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Products.Product do
2 | use Ecto.Schema
3 | import Ecto.Changeset
4 |
5 | schema "products" do
6 | field :description, :string
7 | field :name, :string
8 | field :price, :float
9 | field :properties, :map
10 | has_many :variants, Tutorial.Products.Variant
11 | has_many :taggings, Tutorial.Taggable.Tagging
12 | has_many :tags, through: [:taggings, :tag]
13 |
14 | timestamps()
15 | end
16 |
17 | @doc false
18 | def changeset(product, attrs) do
19 | product
20 | |> cast(attrs, [:name, :description, :price, :properties])
21 | |> cast_assoc(:variants)
22 | |> validate_required([:name, :description, :price])
23 | |> validate_length(:name, min: 2)
24 | |> validate_number(:price, greater_than: 0)
25 | end
26 | end
27 |
--------------------------------------------------------------------------------
/lib/tutorial_web/gettext.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.Gettext do
2 | @moduledoc """
3 | A module providing Internationalization with a gettext-based API.
4 |
5 | By using [Gettext](https://hexdocs.pm/gettext),
6 | your module gains a set of macros for translations, for example:
7 |
8 | import TutorialWeb.Gettext
9 |
10 | # Simple translation
11 | gettext("Here is the string to translate")
12 |
13 | # Plural translation
14 | ngettext("Here is the string to translate",
15 | "Here are the strings to translate",
16 | 3)
17 |
18 | # Domain-based translation
19 | dgettext("errors", "Here is the error message to translate")
20 |
21 | See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
22 | """
23 | use Gettext, otp_app: :tutorial
24 | end
25 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/secret/form.html.eex:
--------------------------------------------------------------------------------
1 | <%= form_for @changeset, @action, [class: "block"], fn f -> %>
2 | <%= if @changeset.action do %>
3 |
4 |
The form couln't be saved! Please check the errors below.
5 |
6 | <% end %>
7 |
8 |
9 | <%= label f, :key %>
10 | <%= text_input f, :key, class: "form-control" %>
11 | <%= error_tag f, :key %>
12 |
13 |
14 |
15 | <%= label f, :value %>
16 | <%= text_input f, :value, class: "form-control" %>
17 | <%= error_tag f, :value %>
18 |
19 |
20 |
21 | <%= submit "Save", class: "btn btn-primary mr-2" %>
22 | <%= link "Back", to: Routes.secret_path(@conn, :index), class: "btn btn-secondary" %>
23 |
24 | <% end %>
25 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/show.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 | <%= for {k, _} <- schema.attrs do %>
10 |
11 | <%= Phoenix.Naming.humanize(Atom.to_string(k)) %>:
12 | <%%= @<%= schema.singular %>.<%= k %> %>
13 |
14 | <% end %>
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/priv/templates/swagger.gen.json/fallback_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.FallbackController do
2 | @moduledoc """
3 | Translates controller action results into valid `Plug.Conn` responses.
4 |
5 | See `Phoenix.Controller.action_fallback/1` for more details.
6 | """
7 | use <%= inspect context.web_module %>, :controller
8 |
9 | <%= if schema.generate? do %>def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
10 | conn
11 | |> put_status(:unprocessable_entity)
12 | |> put_view(<%= inspect context.web_module %>.ChangesetView)
13 | |> render("error.json", changeset: changeset)
14 | end
15 |
16 | <% end %>def call(conn, {:error, :not_found}) do
17 | conn
18 | |> put_status(:not_found)
19 | |> put_view(<%= inspect context.web_module %>.ErrorView)
20 | |> render(:"404")
21 | end
22 | end
23 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/products_loading.html.leex:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 | Name
10 | Description
11 | Price
12 |
13 |
14 |
15 |
16 |
17 | <%= for _ <- @products do %>
18 |
19 |
20 |
21 |
22 |
23 |
24 | <% end %>
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/page/search_form.html.leex:
--------------------------------------------------------------------------------
1 | <%= form_tag "#", [phx_change: :search, phx_submit: :submit] do %>
2 |
3 |
4 | <%= if @search_results != [] do %>
5 |
6 |
7 | <%= for {search_result, idx} <- Enum.with_index(@search_results) do %>
8 |
" phx-click="pick" phx-value-name="<%= search_result %>">
9 | <%= raw format_search_result(search_result, @search_phrase) %>
10 |
11 | <% end %>
12 |
13 |
14 | <% end %>
15 |
16 | Search for states
17 | <% end %>
18 |
--------------------------------------------------------------------------------
/priv/templates/swagger.gen.json/view.ex:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>View do
2 | use <%= inspect context.web_module %>, :view
3 | alias <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>View
4 |
5 | def render("index.json", %{<%= schema.plural %>: <%= schema.plural %>}) do
6 | %{data: render_many(<%= schema.plural %>, <%= inspect schema.alias %>View, "<%= schema.singular %>.json")}
7 | end
8 |
9 | def render("show.json", %{<%= schema.singular %>: <%= schema.singular %>}) do
10 | %{data: render_one(<%= schema.singular %>, <%= inspect schema.alias %>View, "<%= schema.singular %>.json")}
11 | end
12 |
13 | def render("<%= schema.singular %>.json", %{<%= schema.singular %>: <%= schema.singular %>}) do
14 | %{id: <%= schema.singular %>.id<%= for {k, _} <- schema.attrs do %>,
15 | <%= k %>: <%= schema.singular %>.<%= k %><% end %>}
16 | end
17 | end
18 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/pow/registration/edit.html.eex:
--------------------------------------------------------------------------------
1 | Edit profile
2 |
3 | <%= form_for @changeset, @action, [as: :user], fn f -> %>
4 | <%= if @changeset.action do %>
5 |
6 |
Oops, something went wrong! Please check the errors below.
7 |
8 | <% end %>
9 |
10 | <%= label f, :current_password %>
11 | <%= password_input f, :current_password %>
12 | <%= error_tag f, :current_password %>
13 |
14 | <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %>
15 | <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %>
16 | <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
17 |
18 | <%= label f, :password %>
19 | <%= password_input f, :password %>
20 | <%= error_tag f, :password %>
21 |
22 | <%= label f, :password_confirmation %>
23 | <%= password_input f, :password_confirmation %>
24 | <%= error_tag f, :password_confirmation %>
25 |
26 |
27 | <%= submit "Update" %>
28 |
29 | <% end %>
30 |
31 |
--------------------------------------------------------------------------------
/assets/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "repository": {},
3 | "license": "MIT",
4 | "scripts": {
5 | "deploy": "webpack --mode production",
6 | "watch": "webpack --mode development --watch"
7 | },
8 | "dependencies": {
9 | "@tailwindcss/ui": "^0.1.3",
10 | "alpinejs": "^2.2.3",
11 | "interactjs": "^1.8.5",
12 | "phoenix": "file:../deps/phoenix",
13 | "phoenix_html": "file:../deps/phoenix_html",
14 | "phoenix_live_view": "file:../deps/phoenix_live_view",
15 | "tailwindcss": "^1.2.0"
16 | },
17 | "devDependencies": {
18 | "@babel/core": "^7.0.0",
19 | "@babel/preset-env": "^7.0.0",
20 | "@fullhuman/postcss-purgecss": "^2.0.5",
21 | "babel-loader": "^8.0.0",
22 | "copy-webpack-plugin": "^4.5.0",
23 | "css-loader": "^2.1.1",
24 | "mini-css-extract-plugin": "^0.4.0",
25 | "optimize-css-assets-webpack-plugin": "^5.0.1",
26 | "postcss-loader": "^3.0.0",
27 | "terser-webpack-plugin": "^1.1.0",
28 | "webpack": "4.4.0",
29 | "webpack-cli": "^3.3.2"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # The directory Mix will write compiled artifacts to.
2 | /_build/
3 |
4 | # If you run "mix test --cover", coverage assets end up here.
5 | /cover/
6 |
7 | # The directory Mix downloads your dependencies sources to.
8 | /deps/
9 |
10 | # Where 3rd-party dependencies like ExDoc output generated docs.
11 | /doc/
12 |
13 | # Ignore .fetch files in case you like to edit your project deps locally.
14 | /.fetch
15 |
16 | # If the VM crashes, it generates a dump, let's ignore it too.
17 | erl_crash.dump
18 |
19 | # Also ignore archive artifacts (built via "mix archive.build").
20 | *.ez
21 |
22 | # Ignore package tarball (built via "mix hex.build").
23 | tutorial-*.tar
24 |
25 | # If NPM crashes, it generates a log, let's ignore it too.
26 | npm-debug.log
27 |
28 | # The directory NPM downloads your dependencies sources to.
29 | /assets/node_modules/
30 |
31 | # Since we are building assets from assets/,
32 | # we ignore priv/static. You may want to comment
33 | # this depending on your deployment strategy.
34 | /priv/static/
35 |
--------------------------------------------------------------------------------
/lib/tutorial/products/variant.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Products.Variant do
2 | use Ecto.Schema
3 | import Ecto.Changeset
4 |
5 | schema "variants" do
6 | field :name, :string
7 | field :value, :string
8 | field :temp_id, :string, virtual: true
9 | field :delete, :boolean, virtual: true
10 |
11 | belongs_to :product, Tutorial.Products.Product
12 |
13 | timestamps()
14 | end
15 |
16 | @doc false
17 | def changeset(variant, attrs) do
18 | variant
19 | |> Map.put(:temp_id, (variant.temp_id || attrs["temp_id"]))
20 | |> cast(attrs, [:name, :value, :delete])
21 | |> validate_required([:name, :value])
22 | |> unique_constraint(:name, name: :variants_name_value_product_id_index)
23 | |> maybe_mark_for_deletion
24 | end
25 |
26 | defp maybe_mark_for_deletion(%{data: %{id: nil}} = changeset), do: changeset
27 | defp maybe_mark_for_deletion(changeset) do
28 | if get_change(changeset, :delete) do
29 | %{changeset | action: :delete}
30 | else
31 | changeset
32 | end
33 | end
34 | end
35 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/show.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 | Name:
12 | <%= @product.name %>
13 |
14 |
15 |
16 | Description:
17 | <%= @product.description %>
18 |
19 |
20 |
21 | Price:
22 | <%= @product.price %>
23 |
24 |
25 |
26 |
27 |
28 | <%= live_render @conn, TutorialWeb.ProductTaggingLive, session: %{"id" => @product.id} %>
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/lib/tutorial/users/user.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Users.User do
2 | use Ecto.Schema
3 | use Pow.Ecto.Schema
4 |
5 | import Ecto.Changeset
6 | alias Tutorial.Accounts
7 |
8 | schema "users" do
9 | pow_user_fields()
10 |
11 | field :account_name, :string, virtual: true
12 |
13 | belongs_to :account, Tutorial.Accounts.Account
14 |
15 | timestamps()
16 | end
17 |
18 | def changeset(user, attrs) do
19 | user
20 | |> pow_changeset(attrs)
21 | |> cast(attrs, [:account_name])
22 | |> validate_required([:account_name])
23 | |> create_account_for_new_user(user)
24 | |> assoc_constraint(:account)
25 | end
26 |
27 | defp create_account_for_new_user(%{valid?: true, changes: %{account_name: account_name}} = changeset, %{account_id: nil} = _user) do
28 | with {:ok, account} <- Accounts.create_account(%{name: account_name}) do
29 | put_assoc(changeset, :account, account)
30 | else
31 | _ -> changeset
32 | end
33 | end
34 | defp create_account_for_new_user(changeset, _), do: changeset
35 | end
36 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/pow/registration/new.html.eex:
--------------------------------------------------------------------------------
1 | Register
2 |
3 | <%= form_for @changeset, @action, [as: :user], fn f -> %>
4 | <%= if @changeset.action do %>
5 |
6 |
Oops, something went wrong! Please check the errors below.
7 |
8 | <% end %>
9 |
10 | <%= label f, :account_name %>
11 | <%= text_input f, :account_name %>
12 | <%= error_tag f, :account_name %>
13 |
14 | <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %>
15 | <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %>
16 | <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
17 |
18 | <%= label f, :password %>
19 | <%= password_input f, :password %>
20 | <%= error_tag f, :password %>
21 |
22 | <%= label f, :password_confirmation %>
23 | <%= password_input f, :password_confirmation %>
24 | <%= error_tag f, :password_confirmation %>
25 |
26 |
27 | <%= submit "Register" %>
28 |
29 | <% end %>
30 |
31 |
32 | <%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %>
33 |
--------------------------------------------------------------------------------
/test/support/channel_case.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ChannelCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | channel tests.
5 |
6 | Such tests rely on `Phoenix.ChannelTest` and also
7 | import other functionality to make it easier
8 | to build common data structures and query the data layer.
9 |
10 | Finally, if the test case interacts with the database,
11 | it cannot be async. For this reason, every test runs
12 | inside a transaction which is reset at the beginning
13 | of the test unless the test case is marked as async.
14 | """
15 |
16 | use ExUnit.CaseTemplate
17 |
18 | using do
19 | quote do
20 | # Import conveniences for testing with channels
21 | use Phoenix.ChannelTest
22 |
23 | # The default endpoint for testing
24 | @endpoint TutorialWeb.Endpoint
25 | end
26 | end
27 |
28 | setup tags do
29 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(Tutorial.Repo)
30 |
31 | unless tags[:async] do
32 | Ecto.Adapters.SQL.Sandbox.mode(Tutorial.Repo, {:shared, self()})
33 | end
34 |
35 | :ok
36 | end
37 | end
38 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/page/calendar.html.leex:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | <%= Timex.format!(@current_date, "%B %Y", :strftime) %>
5 |
6 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | <%= for day_name <- @day_names do %>
17 |
18 | <%= day_name %>
19 |
20 | <% end %>
21 |
22 |
23 |
24 | <%= for week <- @week_rows do %>
25 |
26 | <%= for day <- week do %>
27 | <%= live_component @conn, TutorialWeb.CalendarDayComponent, day: day, current_date: @current_date %>
28 | <% end %>
29 |
30 | <% end %>
31 |
32 |
33 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/secret/index.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 | Key
10 | Value
11 |
12 |
13 |
14 |
15 |
16 | <%= for secret <- @secrets do %>
17 |
18 | <%= secret.key %>
19 | <%= secret.value %>
20 |
21 |
22 | <%= link "Show", to: Routes.secret_path(@conn, :show, secret) %>
23 | <%= link "Edit", to: Routes.secret_path(@conn, :edit, secret) %>
24 | <%= link "Delete", to: Routes.secret_path(@conn, :delete, secret), method: :delete, data: [confirm: "Are you sure?"] %>
25 |
26 |
27 | <% end %>
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/lib/tutorial/taggable.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Taggable do
2 | @moduledoc """
3 | The Taggable context.
4 | """
5 |
6 | import Ecto.Query, warn: false
7 | alias Tutorial.Repo
8 |
9 | alias Tutorial.Taggable.Tag
10 | alias Tutorial.Taggable.Tagging
11 |
12 | def list_tags do
13 | Repo.all(Tag)
14 | end
15 |
16 | def tag_product(product, %{tag: tag_attrs} = attrs) do
17 | tag = create_or_find_tag(tag_attrs)
18 |
19 | product
20 | |> Ecto.build_assoc(:taggings)
21 | |> Tagging.changeset(attrs)
22 | |> Ecto.Changeset.put_assoc(:tag, tag)
23 | |> Repo.insert()
24 | end
25 |
26 | defp create_or_find_tag(%{name: "" <> name} = attrs) do
27 | %Tag{}
28 | |> Tag.changeset(attrs)
29 | |> Repo.insert()
30 | |> case do
31 | {:ok, tag} -> tag
32 | _ -> Repo.get_by(Tag, name: name)
33 | end
34 | end
35 | defp create_or_find_tag(_), do: nil
36 |
37 | def delete_tag_from_product(product, tag) do
38 | Repo.get_by(Tagging, product_id: product.id, tag_id: tag.id)
39 | |> case do
40 | %Tagging{} = tagging -> Repo.delete(tagging)
41 | nil -> {:ok, %Tagging{}}
42 | end
43 | end
44 | end
45 |
--------------------------------------------------------------------------------
/lib/tutorial/application.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Application do
2 | # See https://hexdocs.pm/elixir/Application.html
3 | # for more information on OTP Applications
4 | @moduledoc false
5 |
6 | use Application
7 |
8 | def start(_type, _args) do
9 | # List all child processes to be supervised
10 | children = [
11 | # Start the Ecto repository
12 | Tutorial.Repo,
13 | # Start the endpoint when the application starts
14 | TutorialWeb.Endpoint,
15 | # Starts Presence Process
16 | Tutorial.Presence,
17 | # Starts a worker by calling: Tutorial.Worker.start_link(arg)
18 | # {Tutorial.Worker, arg},
19 | Tutorial.DraggableServer
20 | ]
21 |
22 | # See https://hexdocs.pm/elixir/Supervisor.html
23 | # for other strategies and supported options
24 | opts = [strategy: :one_for_one, name: Tutorial.Supervisor]
25 | Supervisor.start_link(children, opts)
26 | end
27 |
28 | # Tell Phoenix to update the endpoint configuration
29 | # whenever the application is updated.
30 | def config_change(changed, _new, removed) do
31 | TutorialWeb.Endpoint.config_change(changed, removed)
32 | :ok
33 | end
34 | end
35 |
--------------------------------------------------------------------------------
/lib/tutorial_web/channels/user_socket.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.UserSocket do
2 | use Phoenix.Socket
3 |
4 | ## Channels
5 | # channel "room:*", TutorialWeb.RoomChannel
6 | channel "app:*", TutorialWeb.AppChannel
7 |
8 | # Socket params are passed from the client and can
9 | # be used to verify and authenticate a user. After
10 | # verification, you can put default assigns into
11 | # the socket that will be set for all channels, ie
12 | #
13 | # {:ok, assign(socket, :user_id, verified_user_id)}
14 | #
15 | # To deny connection, return `:error`.
16 | #
17 | # See `Phoenix.Token` documentation for examples in
18 | # performing token verification on connect.
19 | def connect(_params, socket, _connect_info) do
20 | {:ok, socket}
21 | end
22 |
23 | # Socket id's are topics that allow you to identify all sockets for a given user:
24 | #
25 | # def id(socket), do: "user_socket:#{socket.assigns.user_id}"
26 | #
27 | # Would allow you to broadcast a "disconnect" event and terminate
28 | # all active sockets and channels for a given user:
29 | #
30 | # TutorialWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
31 | #
32 | # Returning `nil` makes this socket anonymous.
33 | def id(_socket), do: nil
34 | end
35 |
--------------------------------------------------------------------------------
/assets/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const glob = require('glob');
3 | const MiniCssExtractPlugin = require('mini-css-extract-plugin');
4 | const TerserPlugin = require('terser-webpack-plugin');
5 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
6 | const CopyWebpackPlugin = require('copy-webpack-plugin');
7 |
8 | module.exports = (env, options) => ({
9 | optimization: {
10 | minimizer: [
11 | new TerserPlugin({ cache: true, parallel: true, sourceMap: false }),
12 | new OptimizeCSSAssetsPlugin({})
13 | ]
14 | },
15 | entry: {
16 | './js/app.js': glob.sync('./vendor/**/*.js').concat(['./js/app.js'])
17 | },
18 | output: {
19 | filename: 'app.js',
20 | path: path.resolve(__dirname, '../priv/static/js')
21 | },
22 | module: {
23 | rules: [
24 | {
25 | test: /\.js$/,
26 | exclude: /node_modules/,
27 | use: {
28 | loader: 'babel-loader'
29 | }
30 | },
31 | {
32 | test: /\.css$/,
33 | use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
34 | }
35 | ]
36 | },
37 | plugins: [
38 | new MiniCssExtractPlugin({ filename: '../css/app.css' }),
39 | new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
40 | ]
41 | });
42 |
--------------------------------------------------------------------------------
/config/prod.secret.exs:
--------------------------------------------------------------------------------
1 | # In this file, we load production configuration and secrets
2 | # from environment variables. You can also hardcode secrets,
3 | # although such is generally not recommended and you have to
4 | # remember to add this file to your .gitignore.
5 | use Mix.Config
6 |
7 | database_url =
8 | System.get_env("DATABASE_URL") ||
9 | raise """
10 | environment variable DATABASE_URL is missing.
11 | For example: ecto://USER:PASS@HOST/DATABASE
12 | """
13 |
14 | config :tutorial, Tutorial.Repo,
15 | # ssl: true,
16 | url: database_url,
17 | pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
18 |
19 | secret_key_base =
20 | System.get_env("SECRET_KEY_BASE") ||
21 | raise """
22 | environment variable SECRET_KEY_BASE is missing.
23 | You can generate one by calling: mix phx.gen.secret
24 | """
25 |
26 | config :tutorial, TutorialWeb.Endpoint,
27 | http: [:inet6, port: String.to_integer(System.get_env("PORT") || "4000")],
28 | secret_key_base: secret_key_base
29 |
30 | # ## Using releases (Elixir v1.9+)
31 | #
32 | # If you are doing OTP releases, you need to instruct Phoenix
33 | # to start each relevant endpoint:
34 | #
35 | # config :tutorial, TutorialWeb.Endpoint, server: true
36 | #
37 | # Then you can assemble a release by calling `mix release`.
38 | # See `mix help release` for more information.
39 |
--------------------------------------------------------------------------------
/test/support/conn_case.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ConnCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | tests that require setting up a connection.
5 |
6 | Such tests rely on `Phoenix.ConnTest` and also
7 | import other functionality to make it easier
8 | to build common data structures and query the data layer.
9 |
10 | Finally, if the test case interacts with the database,
11 | we enable the SQL sandbox, so changes done to the database
12 | are reverted at the end of every test. If you are using
13 | PostgreSQL, you can even run database tests asynchronously
14 | by setting `use TutorialWeb.ConnCase, async: true`, although
15 | this option is not recommendded for other databases.
16 | """
17 |
18 | use ExUnit.CaseTemplate
19 |
20 | using do
21 | quote do
22 | # Import conveniences for testing with connections
23 | use Phoenix.ConnTest
24 | alias TutorialWeb.Router.Helpers, as: Routes
25 |
26 | # The default endpoint for testing
27 | @endpoint TutorialWeb.Endpoint
28 | end
29 | end
30 |
31 | setup tags do
32 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(Tutorial.Repo)
33 |
34 | unless tags[:async] do
35 | Ecto.Adapters.SQL.Sandbox.mode(Tutorial.Repo, {:shared, self()})
36 | end
37 |
38 | {:ok, conn: Phoenix.ConnTest.build_conn()}
39 | end
40 | end
41 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/index.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 | <%= for {k, _} <- schema.attrs do %> <%= Phoenix.Naming.humanize(Atom.to_string(k)) %>
10 | <% end %>
11 |
12 |
13 |
14 |
15 | <%%= for <%= schema.singular %> <- @<%= schema.plural %> do %>
16 |
17 | <%= for {k, _} <- schema.attrs do %> <%%= <%= schema.singular %>.<%= k %> %>
18 | <% end %>
19 |
20 | <%%= link "Show", to: Routes.<%= schema.route_helper %>_path(@conn, :show, <%= schema.singular %>) %>
21 | <%%= link "Edit", to: Routes.<%= schema.route_helper %>_path(@conn, :edit, <%= schema.singular %>) %>
22 | <%%= link "Delete", to: Routes.<%= schema.route_helper %>_path(@conn, :delete, <%= schema.singular %>), method: :delete, data: [confirm: "Are you sure?"] %>
23 |
24 |
25 | <%% end %>
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/product_tagging.html.leex:
--------------------------------------------------------------------------------
1 | <%= form_tag "#", [phx_change: :search, phx_submit: :submit] do %>
2 |
3 | <%= for tagging <- @taggings do %>
4 |
5 | <%= tagging.tag.name %>
6 | ×
7 |
8 | <% end %>
9 |
18 |
19 |
20 | <%= if @search_results != [] do %>
21 |
22 |
23 | <%= for {search_result, idx} <- Enum.with_index(@search_results) do %>
24 |
" phx-click="pick" phx-value-name="<%= search_result %>">
25 | <%= raw format_search_result(search_result, @search_phrase) %>
26 |
27 | <% end %>
28 |
29 |
30 | <% end %>
31 | <% end %>
32 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/calendar_day_component.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.CalendarDayComponent do
2 | use Phoenix.LiveComponent
3 | use Timex
4 |
5 | def render(assigns) do
6 | assigns = Map.put(assigns, :day_class, day_class(assigns))
7 |
8 | ~L"""
9 | " class="<%= @day_class %>">
10 | <%= Timex.format!(@day, "%d", :strftime) %>
11 |
12 | """
13 | end
14 |
15 | defp day_class(assigns) do
16 | cond do
17 | today?(assigns) ->
18 | "text-xs p-2 text-gray-600 border border-gray-200 bg-green-200 hover:bg-green-300 cursor-pointer"
19 | current_date?(assigns) ->
20 | "text-xs p-2 text-gray-600 border border-gray-200 bg-blue-100 cursor-pointer"
21 | other_month?(assigns) ->
22 | "text-xs p-2 text-gray-400 border border-gray-200 bg-gray-200 cursor-not-allowed"
23 | true ->
24 | "text-xs p-2 text-gray-600 border border-gray-200 bg-white hover:bg-blue-100 cursor-pointer"
25 | end
26 | end
27 |
28 | defp current_date?(assigns) do
29 | Map.take(assigns.day, [:year, :month, :day]) == Map.take(assigns.current_date, [:year, :month, :day])
30 | end
31 |
32 | defp today?(assigns) do
33 | Map.take(assigns.day, [:year, :month, :day]) == Map.take(Timex.now, [:year, :month, :day])
34 | end
35 |
36 | defp other_month?(assigns) do
37 | Map.take(assigns.day, [:year, :month]) != Map.take(assigns.current_date, [:year, :month])
38 | end
39 | end
40 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/draggable_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.DraggableLive do
2 | use Phoenix.LiveView
3 |
4 | alias Tutorial.DraggableServer
5 | alias Tutorial.Presence
6 |
7 | def mount(_params, %{"session_id" => session_id}, socket) do
8 | if connected?(socket) do
9 | # As soon as a new instance if the LiveView is mounted, track the session id
10 | {:ok, _} = Presence.track(self(), "tutorial:presence", session_id, %{
11 | session_id: session_id,
12 | joined_at: :os.system_time(:seconds)
13 | })
14 |
15 | DraggableServer.subscribe(session_id)
16 | end
17 |
18 | {x, y} = DraggableServer.get_coordinates(session_id)
19 |
20 | assigns = [
21 | x: x,
22 | y: y,
23 | session_id: session_id
24 | ]
25 |
26 | {:ok, assign(socket, assigns)}
27 | end
28 |
29 | def render(assigns) do
30 | ~L"""
31 |
33 | """
34 | end
35 |
36 | def handle_event("moving", %{"x" => x, "y" => y}, socket) do
37 | coordinates = {socket.assigns.x + x, socket.assigns.y + y}
38 | DraggableServer.set_coordinates(socket.assigns.session_id, coordinates)
39 | {:noreply, socket}
40 | end
41 |
42 | def handle_info({:updated_coordinates, {x, y}}, socket) do
43 | assigns = [
44 | x: x,
45 | y: y
46 | ]
47 |
48 | {:noreply, assign(socket, assigns)}
49 | end
50 | end
51 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/task_starter_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.TaskStarterLive do
2 | use Phoenix.LiveView
3 |
4 | alias Tutorial.Worker
5 |
6 | def mount(_params, _session, socket) do
7 | if connected?(socket) do
8 | Worker.subscribe()
9 | end
10 |
11 | assigns = [
12 | loading: false,
13 | result: nil
14 | ]
15 |
16 | {:ok, assign(socket, assigns)}
17 | end
18 |
19 | def render(assigns) do
20 | ~L"""
21 |
22 | Long running task that might crash
23 |
24 |
25 | <%= if @result || @loading do %>
26 | <%= if @loading do %>
27 |
28 |
29 |
30 | <% else %>
31 | Result: <%= @result %>
32 | <% end %>
33 | <% end %>
34 |
35 | """
36 | end
37 |
38 | def handle_event("start-task", _, socket) do
39 | Supervisor.start_link([{Worker, nil}], strategy: :one_for_one)
40 |
41 | assigns = [
42 | loading: true
43 | ]
44 |
45 | {:noreply, assign(socket, assigns)}
46 | end
47 |
48 | def handle_info({:message, number}, socket) do
49 | assigns = [
50 | loading: false,
51 | result: number
52 | ]
53 |
54 | {:noreply, assign(socket, assigns)}
55 | end
56 | end
57 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/layout/root.html.leex:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Tutorial · Fullstack Phoenix
8 | <%= csrf_meta_tag() %>
9 | "/>
10 |
11 |
12 |
19 |
20 | <%= for {type, class} <- [{:info, "info"}, {:error, "danger"}] do %>
21 | <%= if get_flash(@conn, type) do %>
22 | <%= get_flash(@conn, type) %>
23 | <% end %>
24 | <% end %>
25 | <%= @inner_content %>
26 |
27 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/lib/tutorial_web/endpoint.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.Endpoint do
2 | use Phoenix.Endpoint, otp_app: :tutorial
3 |
4 | @session_options [
5 | store: :cookie,
6 | key: "_tutorial_key",
7 | signing_salt: "4qBWyMiY"
8 | ]
9 |
10 | socket "/live", Phoenix.LiveView.Socket,
11 | websocket: [connect_info: [session: @session_options]]
12 |
13 | socket "/socket", TutorialWeb.UserSocket,
14 | websocket: true,
15 | longpoll: false
16 |
17 | # Serve at "/" the static files from "priv/static" directory.
18 | #
19 | # You should set gzip to true if you are running phx.digest
20 | # when deploying your static files in production.
21 | plug Plug.Static,
22 | at: "/",
23 | from: :tutorial,
24 | gzip: false,
25 | only: ~w(css fonts images js favicon.ico robots.txt)
26 |
27 | # Code reloading can be explicitly enabled under the
28 | # :code_reloader configuration of your endpoint.
29 | if code_reloading? do
30 | socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
31 | plug Phoenix.LiveReloader
32 | plug Phoenix.CodeReloader
33 | end
34 |
35 | plug Plug.RequestId
36 | plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
37 |
38 | plug Plug.Parsers,
39 | parsers: [:urlencoded, :multipart, :json],
40 | pass: ["*/*"],
41 | json_decoder: Phoenix.json_library()
42 |
43 | plug Plug.MethodOverride
44 | plug Plug.Head
45 |
46 | # The session will be stored in the cookie and signed,
47 | # this means its contents can be read but not tampered with.
48 | # Set :encryption_salt if you would also like to encrypt it.
49 | plug Plug.Session, @session_options
50 | plug Pow.Plug.Session, otp_app: :tutorial
51 |
52 | plug TutorialWeb.Router
53 | end
54 |
--------------------------------------------------------------------------------
/config/config.exs:
--------------------------------------------------------------------------------
1 | # This file is responsible for configuring your application
2 | # and its dependencies with the aid of the Mix.Config module.
3 | #
4 | # This configuration file is loaded before any dependency and
5 | # is restricted to this project.
6 |
7 | # General application configuration
8 | use Mix.Config
9 |
10 | config :tutorial,
11 | ecto_repos: [Tutorial.Repo]
12 |
13 | # Configures the endpoint
14 | config :tutorial, TutorialWeb.Endpoint,
15 | url: [host: "localhost"],
16 | secret_key_base: "IgK/07WPhpZdY3pPfPkF1FhjTlAt1hCWCAYgG3nKsNbD89i6Z/lh/Hb1e0XX4W6U",
17 | render_errors: [view: TutorialWeb.ErrorView, accepts: ~w(html json)],
18 | pubsub: [name: Tutorial.PubSub, adapter: Phoenix.PubSub.PG2],
19 | live_view: [
20 | signing_salt: "ne2OraWqFq6nPInMelyiZPZDgIIQmtRN"
21 | ]
22 |
23 | # Configures Elixir's Logger
24 | config :logger, :console,
25 | format: "$time $metadata[$level] $message\n",
26 | metadata: [:request_id]
27 |
28 | # Use Jason for JSON parsing in Phoenix
29 | config :phoenix, :json_library, Jason
30 |
31 | config :tutorial, :phoenix_swagger,
32 | swagger_files: %{
33 | "priv/static/swagger.json" => [
34 | router: TutorialWeb.Router, # phoenix routes will be converted to swagger paths
35 | endpoint: TutorialWeb.Endpoint # (optional) endpoint config used to set host, port and https schemes.
36 | ]
37 | }
38 |
39 | config :phoenix_swagger, json_library: Jason
40 |
41 | config :tutorial, :pow,
42 | user: Tutorial.Users.User,
43 | repo: Tutorial.Repo,
44 | web_module: TutorialWeb
45 |
46 | # Import environment specific config. This must remain at the bottom
47 | # of this file so it overrides the configuration defined above.
48 | import_config "#{Mix.env()}.exs"
49 |
--------------------------------------------------------------------------------
/test/support/data_case.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.DataCase do
2 | @moduledoc """
3 | This module defines the setup for tests requiring
4 | access to the application's data layer.
5 |
6 | You may define functions here to be used as helpers in
7 | your tests.
8 |
9 | Finally, if the test case interacts with the database,
10 | we enable the SQL sandbox, so changes done to the database
11 | are reverted at the end of every test. If you are using
12 | PostgreSQL, you can even run database tests asynchronously
13 | by setting `use TutorialWeb.DataCase, async: true`, although
14 | this option is not recommendded for other databases.
15 | """
16 |
17 | use ExUnit.CaseTemplate
18 |
19 | using do
20 | quote do
21 | alias Tutorial.Repo
22 |
23 | import Ecto
24 | import Ecto.Changeset
25 | import Ecto.Query
26 | import Tutorial.DataCase
27 | end
28 | end
29 |
30 | setup tags do
31 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(Tutorial.Repo)
32 |
33 | unless tags[:async] do
34 | Ecto.Adapters.SQL.Sandbox.mode(Tutorial.Repo, {:shared, self()})
35 | end
36 |
37 | :ok
38 | end
39 |
40 | @doc """
41 | A helper that transforms changeset errors into a map of messages.
42 |
43 | assert {:error, changeset} = Accounts.create_user(%{password: "short"})
44 | assert "password is too short" in errors_on(changeset).password
45 | assert %{password: ["password is too short"]} = errors_on(changeset)
46 |
47 | """
48 | def errors_on(changeset) do
49 | Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
50 | Regex.replace(~r"%{(\w+)}", message, fn _, key ->
51 | opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
52 | end)
53 | end)
54 | end
55 | end
56 |
--------------------------------------------------------------------------------
/lib/tutorial/draggable_server.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.DraggableServer do
2 | use GenServer
3 |
4 | alias Tutorial.PubSub
5 |
6 | @default_coordinates {10, 10}
7 |
8 | def init(_opts) do
9 | Phoenix.PubSub.subscribe(PubSub, "tutorial:presence")
10 |
11 | state = %{}
12 | {:ok, state}
13 | end
14 |
15 | def start_link(opts) do
16 | GenServer.start_link(__MODULE__, opts, name: __MODULE__)
17 | end
18 |
19 | def subscribe(session_id), do: Phoenix.PubSub.subscribe(PubSub, "draggable:#{session_id}")
20 |
21 | def set_coordinates(session_id, coordinates) do
22 | GenServer.cast(__MODULE__, {:set_coordinates, session_id, coordinates})
23 | end
24 |
25 | def get_coordinates(session_id) do
26 | GenServer.call(__MODULE__, {:get_cordinates, session_id})
27 | end
28 |
29 | # Internal interface
30 |
31 | def handle_cast({:set_coordinates, session_id, coordinates}, state) do
32 | Phoenix.PubSub.broadcast(PubSub, "draggable:#{session_id}", {:updated_coordinates, coordinates})
33 | {:noreply, Map.put(state, session_id, coordinates)}
34 | end
35 |
36 | def handle_call({:get_cordinates, session_id}, _from, state) do
37 | {:reply, Map.get(state, session_id, @default_coordinates), state}
38 | end
39 |
40 | def handle_info(%Phoenix.Socket.Broadcast{event: "presence_diff", payload: diff}, state) do
41 | state =
42 | state
43 | |> handle_leaves(diff.leaves)
44 |
45 | {:noreply, state}
46 | end
47 |
48 | defp handle_leaves(state, leaves) do
49 | Enum.reduce(leaves, state, fn {session_id, _}, state ->
50 | Tutorial.Presence.list("tutorial:presence")
51 | |> Map.get(session_id)
52 | |> case do
53 | nil -> Map.delete(state, session_id)
54 | _ -> state
55 | end
56 | end)
57 | end
58 | end
59 |
--------------------------------------------------------------------------------
/lib/tutorial_web/views/error_helpers.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ErrorHelpers do
2 | @moduledoc """
3 | Conveniences for translating and building error messages.
4 | """
5 |
6 | use Phoenix.HTML
7 |
8 | @doc """
9 | Generates tag for inlined form input errors.
10 | """
11 | def error_tag(form, field) do
12 | Enum.map(Keyword.get_values(form.errors, field), fn error ->
13 | field_name = field |> Atom.to_string() |> String.capitalize()
14 | content_tag(:span, "#{field_name} #{translate_error(error)}", class: "block mt-1 text-sm text-red-700", data: [phx_error_for: input_id(form, field)])
15 | end)
16 | end
17 |
18 | @doc """
19 | Translates an error message using gettext.
20 | """
21 | def translate_error({msg, opts}) do
22 | # When using gettext, we typically pass the strings we want
23 | # to translate as a static argument:
24 | #
25 | # # Translate "is invalid" in the "errors" domain
26 | # dgettext("errors", "is invalid")
27 | #
28 | # # Translate the number of files with plural rules
29 | # dngettext("errors", "1 file", "%{count} files", count)
30 | #
31 | # Because the error messages we show in our forms and APIs
32 | # are defined inside Ecto, we need to translate them dynamically.
33 | # This requires us to call the Gettext module passing our gettext
34 | # backend as first argument.
35 | #
36 | # Note we use the "errors" domain, which means translations
37 | # should be written to the errors.po file. The :count option is
38 | # set by Ecto and indicates we should also apply plural rules.
39 | if count = opts[:count] do
40 | Gettext.dngettext(TutorialWeb.Gettext, "errors", msg, msg, count, opts)
41 | else
42 | Gettext.dgettext(TutorialWeb.Gettext, "errors", msg, opts)
43 | end
44 | end
45 | end
46 |
--------------------------------------------------------------------------------
/lib/tutorial_web/router.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.Router do
2 | use TutorialWeb, :router
3 | use Pow.Phoenix.Router
4 |
5 | pipeline :browser do
6 | plug :accepts, ["html"]
7 | plug :fetch_session
8 | plug :fetch_live_flash
9 | plug :put_root_layout, {TutorialWeb.LayoutView, :root}
10 | plug :protect_from_forgery
11 | plug :put_secure_browser_headers
12 | plug TutorialWeb.GenerateCSRF
13 | plug TutorialWeb.AssignSession
14 | end
15 |
16 | pipeline :api do
17 | plug :accepts, ["json"]
18 | end
19 |
20 | pipeline :protected do
21 | plug Pow.Plug.RequireAuthenticated, error_handler: Pow.Phoenix.PlugErrorHandler
22 | plug TutorialWeb.Plugs.SetCurrentAccount
23 | end
24 |
25 | scope "/" do
26 | pipe_through :browser
27 |
28 | pow_routes()
29 | end
30 |
31 | scope "/", TutorialWeb do
32 | pipe_through :browser
33 |
34 | get "/", PageController, :index
35 | get "/draggable", PageController, :draggable
36 | get "/task_async", PageController, :task_async
37 | live "/modals", ModalsLive, :index
38 |
39 | live "/products", ProductListLive # NEEDS TO BE ABOVE
40 | resources "/products", ProductController
41 | end
42 |
43 | scope "/", TutorialWeb do
44 | pipe_through [:protected, :browser]
45 |
46 | get "/private", PageController, :private
47 | resources "/secrets", SecretController
48 | end
49 |
50 | # Other scopes may use custom stacks.
51 | # scope "/api", TutorialWeb do
52 | # pipe_through :api
53 | # end
54 |
55 | scope "/api/swagger" do
56 | forward "/", PhoenixSwagger.Plug.SwaggerUI, otp_app: :tutorial, swagger_file: "swagger.json"
57 | end
58 |
59 | def swagger_info do
60 | %{
61 | info: %{
62 | version: "1.0",
63 | title: "Tutorial App - Fullstack Phoenix"
64 | }
65 | }
66 | end
67 | end
68 |
--------------------------------------------------------------------------------
/assets/js/app.js:
--------------------------------------------------------------------------------
1 | // We need to import the CSS so that webpack will load it.
2 | // The MiniCssExtractPlugin is used to separate it out into
3 | // its own CSS file.
4 | import css from "../css/app.css"
5 |
6 | // webpack automatically bundles all modules in your
7 | // entry points. Those entry points can be configured
8 | // in "webpack.config.js".
9 | //
10 | // Import dependencies
11 | //
12 | import "phoenix_html"
13 |
14 | import "alpinejs"
15 |
16 | import interact from "interactjs"
17 |
18 | // Import local files
19 | //
20 | // Local files can be imported directly using relative paths, for example:
21 | import socket from "./socket"
22 |
23 | import {Socket} from "phoenix"
24 | import LiveSocket from "phoenix_live_view"
25 |
26 | let Hooks = {}
27 | Hooks.Draggable = {
28 | mounted() {
29 | let instance = this
30 |
31 | interact(this.el).draggable({
32 | onmove(event) {
33 | instance.pushEvent("moving", {x: event.dx, y: event.dy})
34 | }
35 | })
36 | }
37 | }
38 |
39 | Hooks.initModal = {
40 | mounted() {
41 | const handleOpenCloseEvent = event => {
42 | if (event.detail.open === false) {
43 | this.el.removeEventListener("modal-change", handleOpenCloseEvent)
44 | this.pushEvent("close-modal", {id: this.el.id})
45 | }
46 | }
47 | this.el.addEventListener("modal-change", handleOpenCloseEvent)
48 | }
49 | }
50 |
51 | Hooks.closeModal = {
52 | mounted() {
53 | const modalId = this.el.dataset.modalId
54 | const el = document.getElementById(modalId)
55 | const event = new CustomEvent('close-modal');
56 | el.dispatchEvent(event)
57 | }
58 | }
59 |
60 | let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
61 | let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks});
62 | liveSocket.connect()
63 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/form.html.leex:
--------------------------------------------------------------------------------
1 | <%= form_for @changeset, @action, [phx_change: :validate, class: "block", csrf_token: @csrf_token], fn f -> %>
2 |
3 | <%= label f, :name %>
4 | <%= text_input f, :name, class: "form-control" %>
5 | <%= error_tag f, :name %>
6 |
7 |
8 |
9 | <%= label f, :description %>
10 | <%= textarea f, :description, class: "form-control" %>
11 | <%= error_tag f, :description %>
12 |
13 |
14 |
15 | <%= label f, :price %>
16 | <%= number_input f, :price, step: "any", class: "form-control" %>
17 | <%= error_tag f, :price %>
18 |
19 |
20 | Variants
21 |
22 | <%= inputs_for f, :variants, fn v -> %>
23 |
24 |
25 | <%= label v, :name %>
26 | <%= text_input v, :name, class: "form-control" %>
27 | <%= error_tag v, :name %>
28 |
29 |
30 |
31 | <%= label v, :value %>
32 | <%= text_input v, :value, class: "form-control" %>
33 | <%= error_tag v, :value %>
34 |
35 |
36 |
45 |
46 | <% end %>
47 |
48 | Add a variant
49 |
50 |
51 | <%= submit "Save", class: "btn btn-primary mr-2", disabled: !@changeset.valid? %>
52 | <%= link "Back", to: Routes.product_path(@conn, :index), class: "btn btn-secondary" %>
53 |
54 | <% end %>
55 |
--------------------------------------------------------------------------------
/lib/tutorial_web.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb do
2 | @moduledoc """
3 | The entrypoint for defining your web interface, such
4 | as controllers, views, channels and so on.
5 |
6 | This can be used in your application as:
7 |
8 | use TutorialWeb, :controller
9 | use TutorialWeb, :view
10 |
11 | The definitions below will be executed for every view,
12 | controller, etc, so keep them short and clean, focused
13 | on imports, uses and aliases.
14 |
15 | Do NOT define functions inside the quoted expressions
16 | below. Instead, define any helper function in modules
17 | and import those modules here.
18 | """
19 |
20 | def controller do
21 | quote do
22 | use Phoenix.Controller, namespace: TutorialWeb
23 |
24 | import Plug.Conn
25 | import TutorialWeb.Gettext
26 | alias TutorialWeb.Router.Helpers, as: Routes
27 | import Phoenix.LiveView.Controller
28 | end
29 | end
30 |
31 | def view do
32 | quote do
33 | use Phoenix.View,
34 | root: "lib/tutorial_web/templates",
35 | namespace: TutorialWeb
36 |
37 | # Import convenience functions from controllers
38 | import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]
39 |
40 | # Use all HTML functionality (forms, tags, etc)
41 | use Phoenix.HTML
42 |
43 | import TutorialWeb.ErrorHelpers
44 | import TutorialWeb.Gettext
45 | alias TutorialWeb.Router.Helpers, as: Routes
46 | import Phoenix.LiveView.Helpers
47 | end
48 | end
49 |
50 | def router do
51 | quote do
52 | use Phoenix.Router
53 | import Plug.Conn
54 | import Phoenix.Controller
55 | import Phoenix.LiveView.Router
56 | end
57 | end
58 |
59 | def channel do
60 | quote do
61 | use Phoenix.Channel
62 | import TutorialWeb.Gettext
63 | end
64 | end
65 |
66 | @doc """
67 | When used, dispatch to the appropriate controller/view/etc.
68 | """
69 | defmacro __using__(which) when is_atom(which) do
70 | apply(__MODULE__, which, [])
71 | end
72 | end
73 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/calendar_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.CalendarLive do
2 | use Phoenix.LiveView
3 | use Timex
4 |
5 | @week_start_at :mon
6 |
7 | def mount(_params, _session, socket) do
8 | current_date = Timex.now
9 |
10 | assigns = [
11 | conn: socket,
12 | current_date: current_date,
13 | day_names: day_names(@week_start_at),
14 | week_rows: week_rows(current_date)
15 | ]
16 |
17 | {:ok, assign(socket, assigns)}
18 | end
19 |
20 | def render(assigns) do
21 | TutorialWeb.PageView.render("calendar.html", assigns)
22 | end
23 |
24 | defp day_names(:sun), do: [7, 1, 2, 3, 4, 5, 6] |> Enum.map(&Timex.day_shortname/1)
25 | defp day_names(_), do: [1, 2, 3, 4, 5, 6, 7] |> Enum.map(&Timex.day_shortname/1)
26 |
27 | defp week_rows(current_date) do
28 | first =
29 | current_date
30 | |> Timex.beginning_of_month()
31 | |> Timex.beginning_of_week(@week_start_at)
32 |
33 | last =
34 | current_date
35 | |> Timex.end_of_month()
36 | |> Timex.end_of_week(@week_start_at)
37 |
38 | Interval.new(from: first, until: last)
39 | |> Enum.map(& &1)
40 | |> Enum.chunk_every(7)
41 | end
42 |
43 | def handle_event("prev-month", _, socket) do
44 | current_date = Timex.shift(socket.assigns.current_date, months: -1)
45 |
46 | assigns = [
47 | current_date: current_date,
48 | week_rows: week_rows(current_date)
49 | ]
50 |
51 | {:noreply, assign(socket, assigns)}
52 | end
53 |
54 | def handle_event("next-month", _, socket) do
55 | current_date = Timex.shift(socket.assigns.current_date, months: 1)
56 |
57 | assigns = [
58 | current_date: current_date,
59 | week_rows: week_rows(current_date)
60 | ]
61 |
62 | {:noreply, assign(socket, assigns)}
63 | end
64 |
65 | def handle_event("pick-date", %{"date" => date}, socket) do
66 | current_date = Timex.parse!(date, "{YYYY}-{0M}-{D}")
67 |
68 | assigns = [
69 | current_date: current_date
70 | ]
71 |
72 | {:noreply, assign(socket, assigns)}
73 | end
74 | end
75 |
--------------------------------------------------------------------------------
/lib/tutorial_web/templates/product/products.html.leex:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 | Name
10 | Description
11 | Price
12 |
13 |
14 |
15 |
16 |
17 | <%= for product <- @products do %>
18 |
19 | <%= product.name %>
20 | <%= product.description %>
21 | <%= product.price %>
22 |
23 |
24 | <%= link "Show", to: Routes.product_path(@conn, :show, product) %>
25 | <%= link "Edit", to: Routes.product_path(@conn, :edit, product) %>
26 | <%= link "Delete", to: Routes.product_path(@conn, :delete, product), csrf_token: @csrf_token, method: :delete, data: [confirm: "Are you sure?"] %>
27 |
28 |
29 | <% end %>
30 |
31 |
32 |
33 |
34 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/lib/tutorial_web/controllers/product_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ProductController do
2 | use TutorialWeb, :controller
3 |
4 | alias Tutorial.Products
5 | alias Tutorial.Products.Product
6 |
7 | def index(conn, _params) do
8 | products = Products.list_products()
9 | render(conn, "index.html", products: products)
10 | end
11 |
12 | def new(conn, _params) do
13 | changeset = Products.change_product(%Product{})
14 | render(conn, "new.html", changeset: changeset)
15 | end
16 |
17 | def create(conn, %{"product" => product_params}) do
18 | case Products.create_product(product_params) do
19 | {:ok, product} ->
20 | conn
21 | |> put_flash(:info, "Product created successfully.")
22 | |> redirect(to: Routes.product_path(conn, :show, product))
23 |
24 | {:error, %Ecto.Changeset{} = changeset} ->
25 | render(conn, "new.html", changeset: changeset)
26 | end
27 | end
28 |
29 | def show(conn, %{"id" => id}) do
30 | product = Products.get_product!(id)
31 | render(conn, "show.html", product: product)
32 | end
33 |
34 | def edit(conn, %{"id" => id}) do
35 | product = Products.get_product!(id)
36 | changeset = Products.change_product(product)
37 | render(conn, "edit.html", product: product, changeset: changeset)
38 | end
39 |
40 | def update(conn, %{"id" => id, "product" => product_params}) do
41 | product = Products.get_product!(id)
42 |
43 | case Products.update_product(product, product_params) do
44 | {:ok, product} ->
45 | conn
46 | |> put_flash(:info, "Product updated successfully.")
47 | |> redirect(to: Routes.product_path(conn, :show, product))
48 |
49 | {:error, %Ecto.Changeset{} = changeset} ->
50 | render(conn, "edit.html", product: product, changeset: changeset)
51 | end
52 | end
53 |
54 | def delete(conn, %{"id" => id}) do
55 | product = Products.get_product!(id)
56 | {:ok, _product} = Products.delete_product(product)
57 |
58 | conn
59 | |> put_flash(:info, "Product deleted successfully.")
60 | |> redirect(to: Routes.product_path(conn, :index))
61 | end
62 | end
63 |
--------------------------------------------------------------------------------
/config/prod.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # For production, don't forget to configure the url host
4 | # to something meaningful, Phoenix uses this information
5 | # when generating URLs.
6 | #
7 | # Note we also include the path to a cache manifest
8 | # containing the digested version of static files. This
9 | # manifest is generated by the `mix phx.digest` task,
10 | # which you should run after static files are built and
11 | # before starting your production server.
12 | config :tutorial, TutorialWeb.Endpoint,
13 | url: [host: "example.com", port: 80],
14 | cache_static_manifest: "priv/static/cache_manifest.json"
15 |
16 | # Do not print debug messages in production
17 | config :logger, level: :info
18 |
19 | # ## SSL Support
20 | #
21 | # To get SSL working, you will need to add the `https` key
22 | # to the previous section and set your `:url` port to 443:
23 | #
24 | # config :tutorial, TutorialWeb.Endpoint,
25 | # ...
26 | # url: [host: "example.com", port: 443],
27 | # https: [
28 | # :inet6,
29 | # port: 443,
30 | # cipher_suite: :strong,
31 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
32 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH")
33 | # ]
34 | #
35 | # The `cipher_suite` is set to `:strong` to support only the
36 | # latest and more secure SSL ciphers. This means old browsers
37 | # and clients may not be supported. You can set it to
38 | # `:compatible` for wider support.
39 | #
40 | # `:keyfile` and `:certfile` expect an absolute path to the key
41 | # and cert in disk or a relative path inside priv, for example
42 | # "priv/ssl/server.key". For all supported SSL configuration
43 | # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
44 | #
45 | # We also recommend setting `force_ssl` in your endpoint, ensuring
46 | # no data is ever sent via http, always redirecting to https:
47 | #
48 | # config :tutorial, TutorialWeb.Endpoint,
49 | # force_ssl: [hsts: true]
50 | #
51 | # Check `Plug.SSL` for all available options in `force_ssl`.
52 |
53 | # Finally import the config/prod.secret.exs which loads secrets
54 | # and configuration from environment variables.
55 | import_config "prod.secret.exs"
56 |
--------------------------------------------------------------------------------
/mix.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.MixProject do
2 | use Mix.Project
3 |
4 | def project do
5 | [
6 | app: :tutorial,
7 | version: "0.1.0",
8 | elixir: "~> 1.5",
9 | elixirc_paths: elixirc_paths(Mix.env()),
10 | compilers: [:phoenix, :gettext] ++ Mix.compilers(),
11 | start_permanent: Mix.env() == :prod,
12 | aliases: aliases(),
13 | deps: deps()
14 | ]
15 | end
16 |
17 | # Configuration for the OTP application.
18 | #
19 | # Type `mix help compile.app` for more information.
20 | def application do
21 | [
22 | mod: {Tutorial.Application, []},
23 | extra_applications: [:logger, :runtime_tools]
24 | ]
25 | end
26 |
27 | # Specifies which paths to compile per environment.
28 | defp elixirc_paths(:test), do: ["lib", "test/support"]
29 | defp elixirc_paths(_), do: ["lib"]
30 |
31 | # Specifies your project dependencies.
32 | #
33 | # Type `mix help deps` for examples and options.
34 | defp deps do
35 | [
36 | {:phoenix, "~> 1.4.16"},
37 | {:phoenix_pubsub, "~> 1.1"},
38 | {:phoenix_ecto, "~> 4.0"},
39 | {:ecto_sql, "~> 3.1"},
40 | {:postgrex, ">= 0.0.0"},
41 | {:phoenix_html, "~> 2.11"},
42 | {:phoenix_live_reload, "~> 1.2", only: :dev},
43 | {:gettext, "~> 0.11"},
44 | {:jason, "~> 1.0"},
45 | {:plug_cowboy, "~> 2.0"},
46 | # Additional
47 | {:phoenix_live_view, "~> 0.10.0"},
48 | {:floki, ">= 0.0.0", only: :test},
49 | {:phoenix_swagger, "~> 0.8"},
50 | {:ex_json_schema, "~> 0.5"},
51 | {:faker, "~> 0.13"},
52 | {:scrivener_ecto, "~> 2.0"},
53 | {:timex, "~> 3.6"},
54 | {:pow, "~> 1.0.18"}
55 | ]
56 | end
57 |
58 | # Aliases are shortcuts or tasks specific to the current project.
59 | # For example, to create, migrate and run the seeds file at once:
60 | #
61 | # $ mix ecto.setup
62 | #
63 | # See the documentation for `Mix` for more info on aliases.
64 | defp aliases do
65 | [
66 | "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
67 | "ecto.reset": ["ecto.drop", "ecto.setup"],
68 | test: ["ecto.create --quiet", "ecto.migrate", "test"]
69 | ]
70 | end
71 | end
72 |
--------------------------------------------------------------------------------
/test/tutorial/accounts_test.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.AccountsTest do
2 | use Tutorial.DataCase
3 |
4 | alias Tutorial.Accounts
5 |
6 | describe "accounts" do
7 | alias Tutorial.Accounts.Account
8 |
9 | @valid_attrs %{name: "some name"}
10 | @update_attrs %{name: "some updated name"}
11 | @invalid_attrs %{name: nil}
12 |
13 | def account_fixture(attrs \\ %{}) do
14 | {:ok, account} =
15 | attrs
16 | |> Enum.into(@valid_attrs)
17 | |> Accounts.create_account()
18 |
19 | account
20 | end
21 |
22 | test "list_accounts/0 returns all accounts" do
23 | account = account_fixture()
24 | assert Accounts.list_accounts() == [account]
25 | end
26 |
27 | test "get_account!/1 returns the account with given id" do
28 | account = account_fixture()
29 | assert Accounts.get_account!(account.id) == account
30 | end
31 |
32 | test "create_account/1 with valid data creates a account" do
33 | assert {:ok, %Account{} = account} = Accounts.create_account(@valid_attrs)
34 | assert account.name == "some name"
35 | end
36 |
37 | test "create_account/1 with invalid data returns error changeset" do
38 | assert {:error, %Ecto.Changeset{}} = Accounts.create_account(@invalid_attrs)
39 | end
40 |
41 | test "update_account/2 with valid data updates the account" do
42 | account = account_fixture()
43 | assert {:ok, %Account{} = account} = Accounts.update_account(account, @update_attrs)
44 | assert account.name == "some updated name"
45 | end
46 |
47 | test "update_account/2 with invalid data returns error changeset" do
48 | account = account_fixture()
49 | assert {:error, %Ecto.Changeset{}} = Accounts.update_account(account, @invalid_attrs)
50 | assert account == Accounts.get_account!(account.id)
51 | end
52 |
53 | test "delete_account/1 deletes the account" do
54 | account = account_fixture()
55 | assert {:ok, %Account{}} = Accounts.delete_account(account)
56 | assert_raise Ecto.NoResultsError, fn -> Accounts.get_account!(account.id) end
57 | end
58 |
59 | test "change_account/1 returns a account changeset" do
60 | account = account_fixture()
61 | assert %Ecto.Changeset{} = Accounts.change_account(account)
62 | end
63 | end
64 | end
65 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/product_form_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ProductFormLive do
2 | use Phoenix.LiveView
3 |
4 | alias Tutorial.Products
5 | alias Tutorial.Products.Product
6 | alias Tutorial.Products.Variant
7 |
8 | def mount(_params, %{"action" => action, "csrf_token" => csrf_token} = session, socket) do
9 | product = get_product(session)
10 | changeset =
11 | Products.change_product(product)
12 | |> Ecto.Changeset.put_assoc(:variants, product.variants)
13 |
14 | assigns = [
15 | conn: socket,
16 | action: action,
17 | csrf_token: csrf_token,
18 | changeset: changeset,
19 | product: product
20 | ]
21 |
22 | {:ok, assign(socket, assigns)}
23 | end
24 |
25 | def render(assigns) do
26 | TutorialWeb.ProductView.render("form.html", assigns)
27 | end
28 |
29 | def handle_event("validate", %{"product" => product_params}, socket) do
30 | changeset =
31 | socket.assigns.product
32 | |> Product.changeset(product_params)
33 | |> Map.put(:action, :insert)
34 |
35 | {:noreply, assign(socket, changeset: changeset)}
36 | end
37 |
38 | def handle_event("add-variant", _, socket) do
39 | vars = Map.get(socket.assigns.changeset.changes, :variants, socket.assigns.product.variants)
40 |
41 | variants =
42 | vars
43 | |> Enum.concat([
44 | Products.change_variant(%Variant{temp_id: get_temp_id()})
45 | ])
46 |
47 | changeset =
48 | socket.assigns.changeset
49 | |> Ecto.Changeset.put_assoc(:variants, variants)
50 |
51 | {:noreply, assign(socket, changeset: changeset)}
52 | end
53 |
54 | def handle_event("remove-variant", %{"remove" => remove_id}, socket) do
55 | variants =
56 | socket.assigns.changeset.changes.variants
57 | |> Enum.reject(fn %{data: variant} ->
58 | variant.temp_id == remove_id
59 | end)
60 |
61 | changeset =
62 | socket.assigns.changeset
63 | |> Ecto.Changeset.put_assoc(:variants, variants)
64 |
65 | {:noreply, assign(socket, changeset: changeset)}
66 | end
67 |
68 | def get_product(%{"id" => id} = _product_params), do: Products.get_product!(id)
69 | def get_product(_product_params), do: %Product{variants: []}
70 |
71 | defp get_temp_id, do: :crypto.strong_rand_bytes(5) |> Base.url_encode64 |> binary_part(0, 5)
72 | end
73 |
--------------------------------------------------------------------------------
/lib/tutorial/accounts.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Accounts do
2 | @moduledoc """
3 | The Accounts context.
4 | """
5 |
6 | import Ecto.Query, warn: false
7 | alias Tutorial.Repo
8 |
9 | alias Tutorial.Accounts.Account
10 |
11 | @doc """
12 | Returns the list of accounts.
13 |
14 | ## Examples
15 |
16 | iex> list_accounts()
17 | [%Account{}, ...]
18 |
19 | """
20 | def list_accounts do
21 | Repo.all(Account)
22 | end
23 |
24 | @doc """
25 | Gets a single account.
26 |
27 | Raises `Ecto.NoResultsError` if the Account does not exist.
28 |
29 | ## Examples
30 |
31 | iex> get_account!(123)
32 | %Account{}
33 |
34 | iex> get_account!(456)
35 | ** (Ecto.NoResultsError)
36 |
37 | """
38 | def get_account!(id), do: Repo.get!(Account, id)
39 |
40 | @doc """
41 | Creates a account.
42 |
43 | ## Examples
44 |
45 | iex> create_account(%{field: value})
46 | {:ok, %Account{}}
47 |
48 | iex> create_account(%{field: bad_value})
49 | {:error, %Ecto.Changeset{}}
50 |
51 | """
52 | def create_account(attrs \\ %{}) do
53 | %Account{}
54 | |> Account.changeset(attrs)
55 | |> Repo.insert()
56 | end
57 |
58 | @doc """
59 | Updates a account.
60 |
61 | ## Examples
62 |
63 | iex> update_account(account, %{field: new_value})
64 | {:ok, %Account{}}
65 |
66 | iex> update_account(account, %{field: bad_value})
67 | {:error, %Ecto.Changeset{}}
68 |
69 | """
70 | def update_account(%Account{} = account, attrs) do
71 | account
72 | |> Account.changeset(attrs)
73 | |> Repo.update()
74 | end
75 |
76 | @doc """
77 | Deletes a account.
78 |
79 | ## Examples
80 |
81 | iex> delete_account(account)
82 | {:ok, %Account{}}
83 |
84 | iex> delete_account(account)
85 | {:error, %Ecto.Changeset{}}
86 |
87 | """
88 | def delete_account(%Account{} = account) do
89 | Repo.delete(account)
90 | end
91 |
92 | @doc """
93 | Returns an `%Ecto.Changeset{}` for tracking account changes.
94 |
95 | ## Examples
96 |
97 | iex> change_account(account)
98 | %Ecto.Changeset{source: %Account{}}
99 |
100 | """
101 | def change_account(%Account{} = account) do
102 | Account.changeset(account, %{})
103 | end
104 | end
105 |
--------------------------------------------------------------------------------
/config/dev.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # Configure your database
4 | config :tutorial, Tutorial.Repo,
5 | username: "postgres",
6 | password: "postgres",
7 | database: "tutorial_dev",
8 | hostname: "localhost",
9 | show_sensitive_data_on_connection_error: true,
10 | pool_size: 10
11 |
12 | # For development, we disable any cache and enable
13 | # debugging and code reloading.
14 | #
15 | # The watchers configuration can be used to run external
16 | # watchers to your application. For example, we use it
17 | # with webpack to recompile .js and .css sources.
18 | config :tutorial, TutorialWeb.Endpoint,
19 | http: [port: 4000],
20 | debug_errors: true,
21 | code_reloader: true,
22 | check_origin: false,
23 | watchers: [
24 | node: [
25 | "node_modules/webpack/bin/webpack.js",
26 | "--mode",
27 | "development",
28 | "--watch-stdin",
29 | cd: Path.expand("../assets", __DIR__)
30 | ]
31 | ]
32 |
33 | # ## SSL Support
34 | #
35 | # In order to use HTTPS in development, a self-signed
36 | # certificate can be generated by running the following
37 | # Mix task:
38 | #
39 | # mix phx.gen.cert
40 | #
41 | # Note that this task requires Erlang/OTP 20 or later.
42 | # Run `mix help phx.gen.cert` for more information.
43 | #
44 | # The `http:` config above can be replaced with:
45 | #
46 | # https: [
47 | # port: 4001,
48 | # cipher_suite: :strong,
49 | # keyfile: "priv/cert/selfsigned_key.pem",
50 | # certfile: "priv/cert/selfsigned.pem"
51 | # ],
52 | #
53 | # If desired, both `http:` and `https:` keys can be
54 | # configured to run both http and https servers on
55 | # different ports.
56 |
57 | # Watch static and templates for browser reloading.
58 | config :tutorial, TutorialWeb.Endpoint,
59 | live_reload: [
60 | patterns: [
61 | ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
62 | ~r"priv/gettext/.*(po)$",
63 | ~r"lib/tutorial_web/{live,views}/.*(ex)$",
64 | ~r"lib/tutorial_web/templates/.*(eex)$"
65 | ]
66 | ]
67 |
68 | # Do not include metadata nor timestamps in development logs
69 | config :logger, :console, format: "[$level] $message\n"
70 |
71 | # Set a higher stacktrace during development. Avoid configuring such
72 | # in production as building large stacktraces may be expensive.
73 | config :phoenix, :stacktrace_depth, 20
74 |
75 | # Initialize plugs at runtime for faster development compilation
76 | config :phoenix, :plug_init_mode, :runtime
77 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/product_list_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ProductListLive do
2 | use Phoenix.LiveView
3 |
4 | alias TutorialWeb.Router.Helpers, as: Routes
5 | alias TutorialWeb.ProductListLive
6 | alias Tutorial.Repo
7 | alias Tutorial.Products
8 | alias Tutorial.Products.Product
9 |
10 | def mount(_params, %{"csrf_token" => csrf_token} = _session, socket) do
11 | if connected?(socket), do: Phoenix.PubSub.subscribe(Tutorial.PubSub, "app:#{csrf_token}")
12 |
13 | assigns = [
14 | conn: socket,
15 | csrf_token: csrf_token
16 | ]
17 |
18 | {:ok, assign(socket, assigns)}
19 | end
20 |
21 | def render(assigns) do
22 | if connected?(assigns.conn) do
23 | TutorialWeb.ProductView.render("products.html", assigns)
24 | else
25 | TutorialWeb.ProductView.render("products_loading.html", assigns)
26 | end
27 | end
28 |
29 | def handle_event("nav", %{"page" => page}, socket) do
30 | {:noreply, push_patch(socket, to: Routes.live_path(socket, ProductListLive, page: page))}
31 | end
32 |
33 | def handle_params(%{"page" => page}, _, socket) do
34 | connected = connected?(socket)
35 | assigns = get_and_assign_page(page, connected)
36 | {:noreply, assign(socket, assigns)}
37 | end
38 |
39 | def handle_params(_, _, socket) do
40 | connected = connected?(socket)
41 | assigns = get_and_assign_page(nil, connected)
42 | {:noreply, assign(socket, assigns)}
43 | end
44 |
45 | def handle_info({"paginate", %{"page" => page}}, socket) do
46 | {:noreply, live_redirect(socket, to: Routes.live_path(socket, ProductListLive, page: page))}
47 | end
48 |
49 | def handle_info(_, socket), do: {:noreply, socket}
50 |
51 | def get_and_assign_page(_page_number, false) do
52 | total_count = Repo.aggregate(Product, :count)
53 | product_count = Enum.min([10, total_count])
54 |
55 | [
56 | products: Enum.to_list(1..product_count)
57 | ]
58 | end
59 |
60 | def get_and_assign_page(page_number, _) do
61 | %{
62 | entries: entries,
63 | page_number: page_number,
64 | page_size: page_size,
65 | total_entries: total_entries,
66 | total_pages: total_pages
67 | } = Products.paginate_products(page: page_number)
68 |
69 | [
70 | products: entries,
71 | page_number: page_number,
72 | page_size: page_size,
73 | total_entries: total_entries,
74 | total_pages: total_pages
75 | ]
76 | end
77 | end
78 |
--------------------------------------------------------------------------------
/lib/tutorial/secrets.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Secrets do
2 | @moduledoc """
3 | The Secrets context.
4 | """
5 |
6 | import Ecto.Query, warn: false
7 | alias Tutorial.Repo
8 |
9 | alias Tutorial.Secrets.Secret
10 |
11 | @doc """
12 | Returns the list of secrets.
13 |
14 | ## Examples
15 |
16 | iex> list_secrets()
17 | [%Secret{}, ...]
18 |
19 | """
20 | def list_secrets(account) do
21 | from(s in Secret, where: s.account_id == ^account.id, order_by: [asc: :id])
22 | |> Repo.all()
23 | end
24 |
25 | @doc """
26 | Gets a single secret.
27 |
28 | Raises `Ecto.NoResultsError` if the Secret does not exist.
29 |
30 | ## Examples
31 |
32 | iex> get_secret!(123)
33 | %Secret{}
34 |
35 | iex> get_secret!(456)
36 | ** (Ecto.NoResultsError)
37 |
38 | """
39 | def get_secret!(account, id), do: Repo.get_by!(Secret, account_id: account.id, id: id)
40 |
41 | @doc """
42 | Creates a secret.
43 |
44 | ## Examples
45 |
46 | iex> create_secret(account, %{field: value})
47 | {:ok, %Secret{}}
48 |
49 | iex> create_secret(account, %{field: bad_value})
50 | {:error, %Ecto.Changeset{}}
51 |
52 | """
53 | def create_secret(account, attrs \\ %{}) do
54 | Ecto.build_assoc(account, :secrets)
55 | |> Secret.changeset(attrs)
56 | |> Repo.insert()
57 | end
58 |
59 | @doc """
60 | Updates a secret.
61 |
62 | ## Examples
63 |
64 | iex> update_secret(secret, %{field: new_value})
65 | {:ok, %Secret{}}
66 |
67 | iex> update_secret(secret, %{field: bad_value})
68 | {:error, %Ecto.Changeset{}}
69 |
70 | """
71 | def update_secret(%Secret{} = secret, attrs) do
72 | secret
73 | |> Secret.changeset(attrs)
74 | |> Repo.update()
75 | end
76 |
77 | @doc """
78 | Deletes a secret.
79 |
80 | ## Examples
81 |
82 | iex> delete_secret(secret)
83 | {:ok, %Secret{}}
84 |
85 | iex> delete_secret(secret)
86 | {:error, %Ecto.Changeset{}}
87 |
88 | """
89 | def delete_secret(%Secret{} = secret) do
90 | Repo.delete(secret)
91 | end
92 |
93 | @doc """
94 | Returns an `%Ecto.Changeset{}` for tracking secret changes.
95 |
96 | ## Examples
97 |
98 | iex> change_secret(secret)
99 | %Ecto.Changeset{source: %Secret{}}
100 |
101 | """
102 | def change_secret(%Secret{} = secret) do
103 | Secret.changeset(secret, %{})
104 | end
105 | end
106 |
--------------------------------------------------------------------------------
/lib/tutorial_web/controllers/secret_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.SecretController do
2 | use TutorialWeb, :controller
3 |
4 | alias Tutorial.Secrets
5 | alias Tutorial.Secrets.Secret
6 |
7 | def index(conn, _params) do
8 | current_account = conn.assigns.current_account
9 | secrets = Secrets.list_secrets(current_account)
10 | render(conn, "index.html", secrets: secrets)
11 | end
12 |
13 | def new(conn, _params) do
14 | changeset = Secrets.change_secret(%Secret{})
15 | render(conn, "new.html", changeset: changeset)
16 | end
17 |
18 | def create(conn, %{"secret" => secret_params}) do
19 | current_account = conn.assigns.current_account
20 |
21 | case Secrets.create_secret(current_account, secret_params) do
22 | {:ok, secret} ->
23 | conn
24 | |> put_flash(:info, "Secret created successfully.")
25 | |> redirect(to: Routes.secret_path(conn, :show, secret))
26 |
27 | {:error, %Ecto.Changeset{} = changeset} ->
28 | render(conn, "new.html", changeset: changeset)
29 | end
30 | end
31 |
32 | def show(conn, %{"id" => id}) do
33 | current_account = conn.assigns.current_account
34 | secret = Secrets.get_secret!(current_account, id)
35 |
36 | render(conn, "show.html", secret: secret)
37 | end
38 |
39 | def edit(conn, %{"id" => id}) do
40 | current_account = conn.assigns.current_account
41 | secret = Secrets.get_secret!(current_account, id)
42 |
43 | changeset = Secrets.change_secret(secret)
44 | render(conn, "edit.html", secret: secret, changeset: changeset)
45 | end
46 |
47 | def update(conn, %{"id" => id, "secret" => secret_params}) do
48 | current_account = conn.assigns.current_account
49 | secret = Secrets.get_secret!(current_account, id)
50 |
51 |
52 | case Secrets.update_secret(secret, secret_params) do
53 | {:ok, secret} ->
54 | conn
55 | |> put_flash(:info, "Secret updated successfully.")
56 | |> redirect(to: Routes.secret_path(conn, :show, secret))
57 |
58 | {:error, %Ecto.Changeset{} = changeset} ->
59 | render(conn, "edit.html", secret: secret, changeset: changeset)
60 | end
61 | end
62 |
63 | def delete(conn, %{"id" => id}) do
64 | current_account = conn.assigns.current_account
65 | secret = Secrets.get_secret!(current_account, id)
66 |
67 | {:ok, _secret} = Secrets.delete_secret(secret)
68 |
69 | conn
70 | |> put_flash(:info, "Secret deleted successfully.")
71 | |> redirect(to: Routes.secret_path(conn, :index))
72 | end
73 | end
74 |
--------------------------------------------------------------------------------
/test/tutorial/taggable_test.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.TaggableTest do
2 | use Tutorial.DataCase
3 |
4 | alias Tutorial.Taggable
5 | alias Tutorial.Taggable.Tag
6 | alias Tutorial.Taggable.Tagging
7 | alias Tutorial.Repo
8 | alias Tutorial.Products
9 |
10 | def product_fixture(attrs \\ %{}) do
11 | {:ok, product} =
12 | attrs
13 | |> Enum.into(%{description: "some description", name: "some name", price: 120.5, properties: %{}})
14 | |> Products.create_product()
15 |
16 | product
17 | end
18 |
19 | def tag_fixture() do
20 | product = product_fixture()
21 |
22 | {:ok, %Tagging{tag: tag}} =
23 | Taggable.tag_product(product, %{tag: %{name: "Stout"}})
24 |
25 | tag
26 | end
27 |
28 | describe "tags and taggings" do
29 | test "list_tags/0 returns all tags" do
30 | tag = tag_fixture()
31 | assert Taggable.list_tags() == [tag]
32 | end
33 |
34 | test "tag_product/2 with valid data creates a tag" do
35 | product = product_fixture()
36 | assert {:ok, %Tagging{} = tagging} = Taggable.tag_product(product, %{tag: %{name: "Stout"}})
37 | assert tagging.tag.name == "Stout"
38 | end
39 |
40 | test "tag_product/2 with valid data appends the tag if it exists" do
41 | product = product_fixture()
42 | tag = tag_fixture()
43 | Taggable.delete_tag_from_product(product, tag)
44 |
45 | assert {:ok, %Tagging{} = tagging} = Taggable.tag_product(product, %{tag: %{name: "Stout"}})
46 | assert tagging.tag.name == "Stout"
47 | end
48 |
49 | test "tag_product/2 with invalid data returns error changeset" do
50 | product = product_fixture()
51 | assert {:error, %Ecto.Changeset{}} = Taggable.tag_product(product, %{tag: %{name: nil}})
52 | end
53 |
54 | test "tag_product/2 with duplicate tag returns error changeset" do
55 | product = product_fixture()
56 | Taggable.tag_product(product, %{tag: %{name: "Stout"}})
57 | assert {:error, %Ecto.Changeset{}} = Taggable.tag_product(product, %{tag: %{name: "Stout"}})
58 | end
59 |
60 | test "delete_tag_from_product/2 deletes the tagging from product but not the tag" do
61 | product = product_fixture()
62 | {:ok, %Tagging{tag: %Tag{} = tag}} = Taggable.tag_product(product, %{tag: %{name: "Lager"}})
63 |
64 | assert %{tags: [^tag]} = product |> Repo.preload(:tags)
65 | assert {:ok, %Tagging{}} = Taggable.delete_tag_from_product(product, tag)
66 | assert %{tags: []} = product |> Repo.preload(:tags)
67 | assert [%Tag{name: "Lager"}] = Taggable.list_tags()
68 | end
69 | end
70 | end
71 |
--------------------------------------------------------------------------------
/priv/gettext/en/LC_MESSAGES/errors.po:
--------------------------------------------------------------------------------
1 | ## `msgid`s in this file come from POT (.pot) files.
2 | ##
3 | ## Do not add, change, or remove `msgid`s manually here as
4 | ## they're tied to the ones in the corresponding POT file
5 | ## (with the same domain).
6 | ##
7 | ## Use `mix gettext.extract --merge` or `mix gettext.merge`
8 | ## to merge POT files into PO files.
9 | msgid ""
10 | msgstr ""
11 | "Language: en\n"
12 |
13 | ## From Ecto.Changeset.cast/4
14 | msgid "can't be blank"
15 | msgstr ""
16 |
17 | ## From Ecto.Changeset.unique_constraint/3
18 | msgid "has already been taken"
19 | msgstr ""
20 |
21 | ## From Ecto.Changeset.put_change/3
22 | msgid "is invalid"
23 | msgstr ""
24 |
25 | ## From Ecto.Changeset.validate_acceptance/3
26 | msgid "must be accepted"
27 | msgstr ""
28 |
29 | ## From Ecto.Changeset.validate_format/3
30 | msgid "has invalid format"
31 | msgstr ""
32 |
33 | ## From Ecto.Changeset.validate_subset/3
34 | msgid "has an invalid entry"
35 | msgstr ""
36 |
37 | ## From Ecto.Changeset.validate_exclusion/3
38 | msgid "is reserved"
39 | msgstr ""
40 |
41 | ## From Ecto.Changeset.validate_confirmation/3
42 | msgid "does not match confirmation"
43 | msgstr ""
44 |
45 | ## From Ecto.Changeset.no_assoc_constraint/3
46 | msgid "is still associated with this entry"
47 | msgstr ""
48 |
49 | msgid "are still associated with this entry"
50 | msgstr ""
51 |
52 | ## From Ecto.Changeset.validate_length/3
53 | msgid "should be %{count} character(s)"
54 | msgid_plural "should be %{count} character(s)"
55 | msgstr[0] ""
56 | msgstr[1] ""
57 |
58 | msgid "should have %{count} item(s)"
59 | msgid_plural "should have %{count} item(s)"
60 | msgstr[0] ""
61 | msgstr[1] ""
62 |
63 | msgid "should be at least %{count} character(s)"
64 | msgid_plural "should be at least %{count} character(s)"
65 | msgstr[0] ""
66 | msgstr[1] ""
67 |
68 | msgid "should have at least %{count} item(s)"
69 | msgid_plural "should have at least %{count} item(s)"
70 | msgstr[0] ""
71 | msgstr[1] ""
72 |
73 | msgid "should be at most %{count} character(s)"
74 | msgid_plural "should be at most %{count} character(s)"
75 | msgstr[0] ""
76 | msgstr[1] ""
77 |
78 | msgid "should have at most %{count} item(s)"
79 | msgid_plural "should have at most %{count} item(s)"
80 | msgstr[0] ""
81 | msgstr[1] ""
82 |
83 | ## From Ecto.Changeset.validate_number/3
84 | msgid "must be less than %{number}"
85 | msgstr ""
86 |
87 | msgid "must be greater than %{number}"
88 | msgstr ""
89 |
90 | msgid "must be less than or equal to %{number}"
91 | msgstr ""
92 |
93 | msgid "must be greater than or equal to %{number}"
94 | msgstr ""
95 |
96 | msgid "must be equal to %{number}"
97 | msgstr ""
98 |
--------------------------------------------------------------------------------
/priv/gettext/errors.pot:
--------------------------------------------------------------------------------
1 | ## This is a PO Template file.
2 | ##
3 | ## `msgid`s here are often extracted from source code.
4 | ## Add new translations manually only if they're dynamic
5 | ## translations that can't be statically extracted.
6 | ##
7 | ## Run `mix gettext.extract` to bring this file up to
8 | ## date. Leave `msgstr`s empty as changing them here has no
9 | ## effect: edit them in PO (`.po`) files instead.
10 |
11 | ## From Ecto.Changeset.cast/4
12 | msgid "can't be blank"
13 | msgstr ""
14 |
15 | ## From Ecto.Changeset.unique_constraint/3
16 | msgid "has already been taken"
17 | msgstr ""
18 |
19 | ## From Ecto.Changeset.put_change/3
20 | msgid "is invalid"
21 | msgstr ""
22 |
23 | ## From Ecto.Changeset.validate_acceptance/3
24 | msgid "must be accepted"
25 | msgstr ""
26 |
27 | ## From Ecto.Changeset.validate_format/3
28 | msgid "has invalid format"
29 | msgstr ""
30 |
31 | ## From Ecto.Changeset.validate_subset/3
32 | msgid "has an invalid entry"
33 | msgstr ""
34 |
35 | ## From Ecto.Changeset.validate_exclusion/3
36 | msgid "is reserved"
37 | msgstr ""
38 |
39 | ## From Ecto.Changeset.validate_confirmation/3
40 | msgid "does not match confirmation"
41 | msgstr ""
42 |
43 | ## From Ecto.Changeset.no_assoc_constraint/3
44 | msgid "is still associated with this entry"
45 | msgstr ""
46 |
47 | msgid "are still associated with this entry"
48 | msgstr ""
49 |
50 | ## From Ecto.Changeset.validate_length/3
51 | msgid "should be %{count} character(s)"
52 | msgid_plural "should be %{count} character(s)"
53 | msgstr[0] ""
54 | msgstr[1] ""
55 |
56 | msgid "should have %{count} item(s)"
57 | msgid_plural "should have %{count} item(s)"
58 | msgstr[0] ""
59 | msgstr[1] ""
60 |
61 | msgid "should be at least %{count} character(s)"
62 | msgid_plural "should be at least %{count} character(s)"
63 | msgstr[0] ""
64 | msgstr[1] ""
65 |
66 | msgid "should have at least %{count} item(s)"
67 | msgid_plural "should have at least %{count} item(s)"
68 | msgstr[0] ""
69 | msgstr[1] ""
70 |
71 | msgid "should be at most %{count} character(s)"
72 | msgid_plural "should be at most %{count} character(s)"
73 | msgstr[0] ""
74 | msgstr[1] ""
75 |
76 | msgid "should have at most %{count} item(s)"
77 | msgid_plural "should have at most %{count} item(s)"
78 | msgstr[0] ""
79 | msgstr[1] ""
80 |
81 | ## From Ecto.Changeset.validate_number/3
82 | msgid "must be less than %{number}"
83 | msgstr ""
84 |
85 | msgid "must be greater than %{number}"
86 | msgstr ""
87 |
88 | msgid "must be less than or equal to %{number}"
89 | msgstr ""
90 |
91 | msgid "must be greater than or equal to %{number}"
92 | msgstr ""
93 |
94 | msgid "must be equal to %{number}"
95 | msgstr ""
96 |
--------------------------------------------------------------------------------
/assets/js/socket.js:
--------------------------------------------------------------------------------
1 | // NOTE: The contents of this file will only be executed if
2 | // you uncomment its entry in "assets/js/app.js".
3 |
4 | // To use Phoenix channels, the first step is to import Socket,
5 | // and connect at the socket path in "lib/web/endpoint.ex".
6 | //
7 | // Pass the token on params as below. Or remove it
8 | // from the params if you are not using authentication.
9 | import {Socket} from "phoenix"
10 |
11 | let socket = new Socket("/socket", {params: {token: window.userToken}})
12 |
13 | // When you connect, you'll often need to authenticate the client.
14 | // For example, imagine you have an authentication plug, `MyAuth`,
15 | // which authenticates the session and assigns a `:current_user`.
16 | // If the current user exists you can assign the user's token in
17 | // the connection for use in the layout.
18 | //
19 | // In your "lib/web/router.ex":
20 | //
21 | // pipeline :browser do
22 | // ...
23 | // plug MyAuth
24 | // plug :put_user_token
25 | // end
26 | //
27 | // defp put_user_token(conn, _) do
28 | // if current_user = conn.assigns[:current_user] do
29 | // token = Phoenix.Token.sign(conn, "user socket", current_user.id)
30 | // assign(conn, :user_token, token)
31 | // else
32 | // conn
33 | // end
34 | // end
35 | //
36 | // Now you need to pass this token to JavaScript. You can do so
37 | // inside a script tag in "lib/web/templates/layout/app.html.eex":
38 | //
39 | //
40 | //
41 | // You will need to verify the user token in the "connect/3" function
42 | // in "lib/web/channels/user_socket.ex":
43 | //
44 | // def connect(%{"token" => token}, socket, _connect_info) do
45 | // # max_age: 1209600 is equivalent to two weeks in seconds
46 | // case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
47 | // {:ok, user_id} ->
48 | // {:ok, assign(socket, :user, user_id)}
49 | // {:error, reason} ->
50 | // :error
51 | // end
52 | // end
53 | //
54 | // Finally, connect to the socket:
55 | socket.connect()
56 |
57 | // Now that you are connected, you can join channels with a topic:
58 | let channel = socket.channel(`app:${document.querySelector("meta[name='csrf-token']").getAttribute("content")}`, {})
59 | channel.join()
60 | .receive("ok", resp => { console.log("Joined successfully", resp) })
61 | .receive("error", resp => { console.log("Unable to join", resp) })
62 |
63 | channel.on("focus", msg => {
64 | const elm = document.getElementById(msg['id'])
65 | elm.focus()
66 | elm.value = ''
67 | })
68 |
69 | export default socket
70 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/modal_component.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ModalComponent do
2 | use Phoenix.LiveComponent
3 |
4 | def mount(socket) do
5 | {:ok, assign(socket, state: "CLOSED", action: nil)}
6 | end
7 |
8 | def render(assigns) do
9 | if assigns.state == "OPEN" do
10 | ~L"""
11 | open = false, 100)"
21 | class="z-50 fixed bottom-0 inset-x-0 px-4 pb-4 sm:inset-0 sm:flex sm:items-center sm:justify-center"
22 | >
23 | <%= if @action == "CLOSE" do %>
24 |
25 | <% end %>
26 |
27 |
28 |
31 |
32 |
45 |
46 | """
47 | else
48 | ~L"""
49 |
50 | """
51 | end
52 | end
53 | end
54 |
--------------------------------------------------------------------------------
/test/tutorial/secrets_test.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.SecretsTest do
2 | use Tutorial.DataCase
3 |
4 | alias Tutorial.Secrets
5 |
6 | describe "secrets" do
7 | alias Tutorial.Secrets.Secret
8 |
9 | @valid_attrs %{key: "some key", value: "some value"}
10 | @update_attrs %{key: "some updated key", value: "some updated value"}
11 | @invalid_attrs %{key: nil, value: nil}
12 |
13 | def account_fixture() do
14 | {:ok, account} = Tutorial.Accounts.create_account(%{name: "Acme Corp"})
15 | account
16 | end
17 |
18 | def secret_fixture(account, attrs \\ %{}) do
19 | {:ok, secret} =
20 | Secrets.create_secret(
21 | account,
22 | Enum.into(attrs, @valid_attrs)
23 | )
24 |
25 | secret
26 | end
27 |
28 | setup do
29 | account = account_fixture()
30 | secret = secret_fixture(account)
31 | {:ok, %{account: account, secret: secret}}
32 | end
33 |
34 | test "list_secrets/0 returns all secrets", %{account: account, secret: secret} do
35 | assert Secrets.list_secrets(account) == [secret]
36 | end
37 |
38 | test "get_secret!/1 returns the secret with given id", %{account: account, secret: secret} do
39 | assert Secrets.get_secret!(account, secret.id) == secret
40 | end
41 |
42 | test "create_secret/1 with valid data creates a secret", %{account: account} do
43 | assert {:ok, %Secret{} = secret} = Secrets.create_secret(account, @valid_attrs)
44 | assert secret.key == "some key"
45 | assert secret.value == "some value"
46 | end
47 |
48 | test "create_secret/1 with invalid data returns error changeset", %{account: account} do
49 | assert {:error, %Ecto.Changeset{}} = Secrets.create_secret(account, @invalid_attrs)
50 | end
51 |
52 | test "update_secret/2 with valid data updates the secret", %{secret: secret} do
53 | assert {:ok, %Secret{} = secret} = Secrets.update_secret(secret, @update_attrs)
54 | assert secret.key == "some updated key"
55 | assert secret.value == "some updated value"
56 | end
57 |
58 | test "update_secret/2 with invalid data returns error changeset", %{account: account, secret: secret} do
59 | assert {:error, %Ecto.Changeset{}} = Secrets.update_secret(secret, @invalid_attrs)
60 | assert secret == Secrets.get_secret!(account, secret.id)
61 | end
62 |
63 | test "delete_secret/1 deletes the secret", %{account: account, secret: secret} do
64 | assert {:ok, %Secret{}} = Secrets.delete_secret(secret)
65 | assert_raise Ecto.NoResultsError, fn -> Secrets.get_secret!(account, secret.id) end
66 | end
67 |
68 | test "change_secret/1 returns a secret changeset", %{secret: secret} do
69 | assert %Ecto.Changeset{} = Secrets.change_secret(secret)
70 | end
71 | end
72 | end
73 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/search_form_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.SearchFormLive do
2 | use Phoenix.LiveView
3 |
4 | def mount(_params, _session, socket) do
5 | assigns = [
6 | conn: socket,
7 | search_results: [],
8 | search_phrase: "",
9 | current_focus: -1
10 | ]
11 |
12 | {:ok, assign(socket, assigns)}
13 | end
14 |
15 | def render(assigns) do
16 | TutorialWeb.PageView.render("search_form.html", assigns)
17 | end
18 |
19 | def handle_event("search", %{"search_phrase" => search_phrase}, socket) do
20 | assigns = [
21 | search_results: search(search_phrase),
22 | search_phrase: search_phrase
23 | ]
24 |
25 | {:noreply, assign(socket, assigns)}
26 | end
27 |
28 | def handle_event("pick", %{"name" => search_phrase}, socket) do
29 | assigns = [
30 | search_results: [],
31 | search_phrase: search_phrase
32 | ]
33 |
34 | {:noreply, assign(socket, assigns)}
35 | end
36 |
37 | def handle_event("submit", _, socket), do: {:noreply, socket} # PREVENT FORM SUBMIT
38 |
39 | def handle_event("set-focus", %{"keyCode" => 38}, socket) do # UP
40 | current_focus =
41 | Enum.max([(socket.assigns.current_focus - 1), 0])
42 | {:noreply, assign(socket, current_focus: current_focus)}
43 | end
44 |
45 | def handle_event("set-focus", %{"keyCode" => 40}, socket) do # DOWN
46 | current_focus =
47 | Enum.min([(socket.assigns.current_focus + 1), (length(socket.assigns.search_results)-1)])
48 | {:noreply, assign(socket, current_focus: current_focus)}
49 | end
50 |
51 | def handle_event("set-focus", %{"keyCode" => 13}, socket) do # ENTER
52 | case Enum.at(socket.assigns.search_results, socket.assigns.current_focus) do
53 | "" <> search_phrase -> handle_event("pick", %{"name" => search_phrase}, socket)
54 | _ -> {:noreply, socket}
55 | end
56 | end
57 |
58 | # FALLBACK FOR NON RELATED KEY STROKES
59 | def handle_event("set-focus", _, socket), do: {:noreply, socket}
60 |
61 | def search(""), do: []
62 | def search(search_phrase) do
63 | states()
64 | |> Enum.filter(& matches?(&1, search_phrase))
65 | end
66 |
67 | def matches?(first, second) do
68 | String.starts_with?(
69 | String.downcase(first), String.downcase(second)
70 | )
71 | end
72 |
73 | def states do
74 | [
75 | "Alabama", "Alaska", "Arizona", "Arkansas", "California",
76 | "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
77 | "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana",
78 | "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
79 | "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
80 | "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
81 | "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
82 | "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
83 | "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"
84 | ]
85 | end
86 | end
87 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/controller.ex:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>Controller do
2 | use <%= inspect context.web_module %>, :controller
3 |
4 | alias <%= inspect context.module %>
5 | alias <%= inspect schema.module %>
6 |
7 | def index(conn, _params) do
8 | <%= schema.plural %> = <%= inspect context.alias %>.list_<%= schema.plural %>()
9 | render(conn, "index.html", <%= schema.plural %>: <%= schema.plural %>)
10 | end
11 |
12 | def new(conn, _params) do
13 | changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(%<%= inspect schema.alias %>{})
14 | render(conn, "new.html", changeset: changeset)
15 | end
16 |
17 | def create(conn, %{<%= inspect schema.singular %> => <%= schema.singular %>_params}) do
18 | case <%= inspect context.alias %>.create_<%= schema.singular %>(<%= schema.singular %>_params) do
19 | {:ok, <%= schema.singular %>} ->
20 | conn
21 | |> put_flash(:info, "<%= schema.human_singular %> created successfully.")
22 | |> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
23 |
24 | {:error, %Ecto.Changeset{} = changeset} ->
25 | render(conn, "new.html", changeset: changeset)
26 | end
27 | end
28 |
29 | def show(conn, %{"id" => id}) do
30 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
31 | render(conn, "show.html", <%= schema.singular %>: <%= schema.singular %>)
32 | end
33 |
34 | def edit(conn, %{"id" => id}) do
35 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
36 | changeset = <%= inspect context.alias %>.change_<%= schema.singular %>(<%= schema.singular %>)
37 | render(conn, "edit.html", <%= schema.singular %>: <%= schema.singular %>, changeset: changeset)
38 | end
39 |
40 | def update(conn, %{"id" => id, <%= inspect schema.singular %> => <%= schema.singular %>_params}) do
41 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
42 |
43 | case <%= inspect context.alias %>.update_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params) do
44 | {:ok, <%= schema.singular %>} ->
45 | conn
46 | |> put_flash(:info, "<%= schema.human_singular %> updated successfully.")
47 | |> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
48 |
49 | {:error, %Ecto.Changeset{} = changeset} ->
50 | render(conn, "edit.html", <%= schema.singular %>: <%= schema.singular %>, changeset: changeset)
51 | end
52 | end
53 |
54 | def delete(conn, %{"id" => id}) do
55 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
56 | {:ok, _<%= schema.singular %>} = <%= inspect context.alias %>.delete_<%= schema.singular %>(<%= schema.singular %>)
57 |
58 | conn
59 | |> put_flash(:info, "<%= schema.human_singular %> deleted successfully.")
60 | |> redirect(to: Routes.<%= schema.route_helper %>_path(conn, :index))
61 | end
62 | end
63 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/modals_live.html.leex:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Open modal one
5 |
6 |
7 |
8 |
9 | Open modal two
10 |
11 |
12 |
13 |
14 | <%= live_component @socket, TutorialWeb.ModalComponent, id: "modal-one" do %>
15 |
38 | <% end %>
39 |
40 | <%= live_component @socket, TutorialWeb.ModalComponent, id: "modal-two" do %>
41 |
42 |
Card title
43 |
Secondary text
44 |
45 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus quia, nulla!
46 | Maiores et perferendis eaque, exercitationem praesentium nihil.
47 |
48 |
49 |
53 |
54 | <% end %>
55 |
--------------------------------------------------------------------------------
/test/tutorial_web/controllers/product_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ProductControllerTest do
2 | use TutorialWeb.ConnCase
3 |
4 | alias Tutorial.Products
5 |
6 | @create_attrs %{description: "some description", name: "some name", price: 120.5}
7 | @update_attrs %{description: "some updated description", name: "some updated name", price: 456.7}
8 | @invalid_attrs %{description: nil, name: nil, price: nil}
9 |
10 | def fixture(:product) do
11 | {:ok, product} = Products.create_product(@create_attrs)
12 | product
13 | end
14 |
15 | describe "index" do
16 | test "lists all products", %{conn: conn} do
17 | conn = get(conn, Routes.product_path(conn, :index))
18 | assert html_response(conn, 200) =~ "Listing Products"
19 | end
20 | end
21 |
22 | describe "new product" do
23 | test "renders form", %{conn: conn} do
24 | conn = get(conn, Routes.product_path(conn, :new))
25 | assert html_response(conn, 200) =~ "New Product"
26 | end
27 | end
28 |
29 | describe "create product" do
30 | test "redirects to show when data is valid", %{conn: conn} do
31 | conn = post(conn, Routes.product_path(conn, :create), product: @create_attrs)
32 |
33 | assert %{id: id} = redirected_params(conn)
34 | assert redirected_to(conn) == Routes.product_path(conn, :show, id)
35 |
36 | conn = get(conn, Routes.product_path(conn, :show, id))
37 | assert html_response(conn, 200) =~ "Show Product"
38 | end
39 |
40 | test "renders errors when data is invalid", %{conn: conn} do
41 | conn = post(conn, Routes.product_path(conn, :create), product: @invalid_attrs)
42 | assert html_response(conn, 200) =~ "New Product"
43 | end
44 | end
45 |
46 | describe "edit product" do
47 | setup [:create_product]
48 |
49 | test "renders form for editing chosen product", %{conn: conn, product: product} do
50 | conn = get(conn, Routes.product_path(conn, :edit, product))
51 | assert html_response(conn, 200) =~ "Edit Product"
52 | end
53 | end
54 |
55 | describe "update product" do
56 | setup [:create_product]
57 |
58 | test "redirects when data is valid", %{conn: conn, product: product} do
59 | conn = put(conn, Routes.product_path(conn, :update, product), product: @update_attrs)
60 | assert redirected_to(conn) == Routes.product_path(conn, :show, product)
61 |
62 | conn = get(conn, Routes.product_path(conn, :show, product))
63 | assert html_response(conn, 200) =~ "some updated description"
64 | end
65 |
66 | test "renders errors when data is invalid", %{conn: conn, product: product} do
67 | conn = put(conn, Routes.product_path(conn, :update, product), product: @invalid_attrs)
68 | assert html_response(conn, 200) =~ "Edit Product"
69 | end
70 | end
71 |
72 | describe "delete product" do
73 | setup [:create_product]
74 |
75 | test "deletes chosen product", %{conn: conn, product: product} do
76 | conn = delete(conn, Routes.product_path(conn, :delete, product))
77 | assert redirected_to(conn) == Routes.product_path(conn, :index)
78 | assert_error_sent 404, fn ->
79 | get(conn, Routes.product_path(conn, :show, product))
80 | end
81 | end
82 | end
83 |
84 | defp create_product(_) do
85 | product = fixture(:product)
86 | {:ok, product: product}
87 | end
88 | end
89 |
--------------------------------------------------------------------------------
/priv/templates/swagger.gen.json/controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>ControllerTest do
2 | use <%= inspect context.web_module %>.ConnCase
3 |
4 | alias <%= inspect context.module %>
5 | alias <%= inspect schema.module %>
6 |
7 | @create_attrs %{
8 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>
9 | }
10 | @update_attrs %{
11 | <%= schema.params.update |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>
12 | }
13 | @invalid_attrs <%= inspect for {key, _} <- schema.params.create, into: %{}, do: {key, nil} %>
14 |
15 | def fixture(:<%= schema.singular %>) do
16 | {:ok, <%= schema.singular %>} = <%= inspect context.alias %>.create_<%= schema.singular %>(@create_attrs)
17 | <%= schema.singular %>
18 | end
19 |
20 | setup %{conn: conn} do
21 | {:ok, conn: put_req_header(conn, "accept", "application/json")}
22 | end
23 |
24 | describe "index" do
25 | test "lists all <%= schema.plural %>", %{conn: conn} do
26 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :index))
27 | assert json_response(conn, 200)["data"] == []
28 | end
29 | end
30 |
31 | describe "create <%= schema.singular %>" do
32 | test "renders <%= schema.singular %> when data is valid", %{conn: conn} do
33 | conn = post(conn, Routes.<%= schema.route_helper %>_path(conn, :create), <%= schema.singular %>: @create_attrs)
34 | assert %{"id" => id} = json_response(conn, 201)["data"]
35 |
36 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, id))
37 |
38 | assert %{
39 | "id" => id<%= for {key, val} <- schema.params.create do %>,
40 | "<%= key %>" => <%= val |> Phoenix.json_library().encode!() |> Phoenix.json_library().decode!() |> inspect() %><% end %>
41 | } = json_response(conn, 200)["data"]
42 | end
43 |
44 | test "renders errors when data is invalid", %{conn: conn} do
45 | conn = post(conn, Routes.<%= schema.route_helper %>_path(conn, :create), <%= schema.singular %>: @invalid_attrs)
46 | assert json_response(conn, 422)["errors"] != %{}
47 | end
48 | end
49 |
50 | describe "update <%= schema.singular %>" do
51 | setup [:create_<%= schema.singular %>]
52 |
53 | test "renders <%= schema.singular %> when data is valid", %{conn: conn, <%= schema.singular %>: %<%= inspect schema.alias %>{id: id} = <%= schema.singular %>} do
54 | conn = put(conn, Routes.<%= schema.route_helper %>_path(conn, :update, <%= schema.singular %>), <%= schema.singular %>: @update_attrs)
55 | assert %{"id" => ^id} = json_response(conn, 200)["data"]
56 |
57 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, id))
58 |
59 | assert %{
60 | "id" => id<%= for {key, val} <- schema.params.update do %>,
61 | "<%= key %>" => <%= Phoenix.json_library().encode!(val) %><% end %>
62 | } = json_response(conn, 200)["data"]
63 | end
64 |
65 | test "renders errors when data is invalid", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
66 | conn = put(conn, Routes.<%= schema.route_helper %>_path(conn, :update, <%= schema.singular %>), <%= schema.singular %>: @invalid_attrs)
67 | assert json_response(conn, 422)["errors"] != %{}
68 | end
69 | end
70 |
71 | describe "delete <%= schema.singular %>" do
72 | setup [:create_<%= schema.singular %>]
73 |
74 | test "deletes chosen <%= schema.singular %>", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
75 | conn = delete(conn, Routes.<%= schema.route_helper %>_path(conn, :delete, <%= schema.singular %>))
76 | assert response(conn, 204)
77 |
78 | assert_error_sent 404, fn ->
79 | get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
80 | end
81 | end
82 | end
83 |
84 | defp create_<%= schema.singular %>(_) do
85 | <%= schema.singular %> = fixture(:<%= schema.singular %>)
86 | {:ok, <%= schema.singular %>: <%= schema.singular %>}
87 | end
88 | end
89 |
--------------------------------------------------------------------------------
/assets/css/app.css:
--------------------------------------------------------------------------------
1 | @tailwind base;
2 | @tailwind components;
3 |
4 | .h1, h1 {
5 | @apply text-4xl
6 | }
7 |
8 | .h2, h2 {
9 | @apply text-3xl
10 | }
11 |
12 | .h3, h3 {
13 | @apply text-2xl
14 | }
15 |
16 | .h4, h4 {
17 | @apply text-xl
18 | }
19 |
20 | .h5, h5 {
21 | @apply text-lg
22 | }
23 |
24 | .h1, .h2, .h3, .h4, .h5, .h6, h1, h2, h3, h4, h5, h6 {
25 | @apply mb-2 font-medium leading-tight
26 | }
27 |
28 | a {
29 | @apply text-blue-500;
30 | }
31 |
32 | a:hover {
33 | @apply text-blue-800;
34 | }
35 |
36 | /*
37 | * ============================
38 | * Alerts
39 | *=============================
40 | */
41 |
42 | .alert {
43 | @apply relative px-5 py-3 mb-4 rounded border border-transparent;
44 | }
45 |
46 | .alert-success {
47 | @apply text-green-800 bg-green-200 border-green-300;
48 | }
49 |
50 | .alert-danger {
51 | @apply text-red-800 bg-red-200 border-red-300;
52 | }
53 |
54 | .alert-info {
55 | @apply text-indigo-800 bg-indigo-200 border-indigo-300;
56 | }
57 |
58 | /*
59 | * ============================
60 | * Buttons
61 | *=============================
62 | */
63 |
64 | .btn {
65 | @apply inline-block font-normal text-center px-3 py-2 leading-normal text-base rounded cursor-pointer;
66 | }
67 |
68 | .btn-primary {
69 | @apply text-white bg-blue-600;
70 | }
71 |
72 | .btn-primary:hover {
73 | @apply text-white bg-blue-700
74 | }
75 |
76 | .btn-secondary {
77 | @apply text-white bg-gray-600;
78 | }
79 |
80 | .btn-secondary:hover {
81 | @apply text-white bg-gray-700
82 | }
83 |
84 | .btn-success {
85 | @apply text-white bg-green-600;
86 | }
87 |
88 | .btn-success:hover {
89 | @apply text-white bg-green-700
90 | }
91 |
92 | .btn-danger {
93 | @apply text-white bg-red-600;
94 | }
95 |
96 | .btn-danger:hover {
97 | @apply text-white bg-red-700
98 | }
99 |
100 | .btn-dark {
101 | @apply text-white bg-gray-900;
102 | }
103 |
104 | .btn-dark:hover {
105 | @apply text-white bg-black
106 | }
107 |
108 | .btn-link {
109 | @apply text-blue-500;
110 | }
111 |
112 | .btn-link:hover {
113 | @apply text-blue-800;
114 | }
115 |
116 | .btn.disabled, .btn:disabled, input[type="submit"]:disabled {
117 | opacity: .65;
118 | @apply pointer-events-none;
119 | }
120 |
121 | /*
122 | * ============================
123 | * Cards
124 | *=============================
125 | */
126 |
127 | .card {
128 | @apply flex flex-col relative bg-white rounded border border-gray-300 shadow;
129 | }
130 |
131 | .card-header {
132 | @apply py-3 px-5 mb-0 text-white bg-blue-800 border-b border-blue-800 rounded rounded-b-none
133 | }
134 |
135 | .card-body {
136 | @apply flex-auto p-5;
137 | }
138 |
139 | .card-img-top {
140 | @apply w-full rounded-t;
141 | }
142 |
143 | .card-link + .card-link {
144 | @apply ml-5;
145 | }
146 |
147 | .card-text {
148 | @apply my-0 mb-4;
149 | }
150 |
151 | .card-title {
152 | @apply mb-3 text-xl;
153 | }
154 |
155 | /*
156 | * ============================
157 | * Forms
158 | *=============================
159 | */
160 |
161 | form {
162 | @apply block;
163 | }
164 |
165 | label {
166 | @apply inline-block mb-2;
167 | }
168 |
169 | .form-check {
170 | @apply relative block pl-5;
171 | }
172 |
173 | .form-group {
174 | @apply mb-4;
175 | }
176 |
177 | .form-check-input {
178 | @apply absolute mt-1 -ml-5;
179 | }
180 |
181 | .form-check-label {
182 | @apply mb-0;
183 | }
184 |
185 | .form-control {
186 | @apply block w-full py-2 px-3 text-base font-normal leading-normal text-gray-700 bg-white border border-gray-400 rounded;
187 | }
188 |
189 | .form-inline {
190 | @apply flex flex-wrap flex-row items-center
191 | }
192 |
193 | .form-inline .form-control {
194 | @apply inline-block w-auto align-middle
195 | }
196 |
197 | .form-text {
198 | @apply block mt-1;
199 | }
200 |
201 | /*
202 | * ============================
203 | * Tables
204 | *=============================
205 | */
206 |
207 | th {
208 | text-align: inherit;
209 | }
210 |
211 | .table {
212 | @apply w-full text-gray-900
213 | }
214 |
215 | .table thead th {
216 | @apply align-bottom border-b-2 border-gray-300
217 | }
218 |
219 | .table td, .table th {
220 | @apply p-3 border-t border-gray-300
221 | }
222 |
223 | .table.table-bordered {
224 | @apply border border-gray-300
225 | }
226 |
227 | .table-bordered td, .table-bordered th {
228 | @apply border border-gray-300
229 | }
230 |
231 | @tailwind utilities;
232 |
--------------------------------------------------------------------------------
/priv/templates/tailwind.gen.html/controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>ControllerTest do
2 | use <%= inspect context.web_module %>.ConnCase
3 |
4 | alias <%= inspect context.module %>
5 |
6 | @create_attrs <%= inspect schema.params.create %>
7 | @update_attrs <%= inspect schema.params.update %>
8 | @invalid_attrs <%= inspect for {key, _} <- schema.params.create, into: %{}, do: {key, nil} %>
9 |
10 | def fixture(:<%= schema.singular %>) do
11 | {:ok, <%= schema.singular %>} = <%= inspect context.alias %>.create_<%= schema.singular %>(@create_attrs)
12 | <%= schema.singular %>
13 | end
14 |
15 | describe "index" do
16 | test "lists all <%= schema.plural %>", %{conn: conn} do
17 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :index))
18 | assert html_response(conn, 200) =~ "Listing <%= schema.human_plural %>"
19 | end
20 | end
21 |
22 | describe "new <%= schema.singular %>" do
23 | test "renders form", %{conn: conn} do
24 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :new))
25 | assert html_response(conn, 200) =~ "New <%= schema.human_singular %>"
26 | end
27 | end
28 |
29 | describe "create <%= schema.singular %>" do
30 | test "redirects to show when data is valid", %{conn: conn} do
31 | conn = post(conn, Routes.<%= schema.route_helper %>_path(conn, :create), <%= schema.singular %>: @create_attrs)
32 |
33 | assert %{id: id} = redirected_params(conn)
34 | assert redirected_to(conn) == Routes.<%= schema.route_helper %>_path(conn, :show, id)
35 |
36 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, id))
37 | assert html_response(conn, 200) =~ "Show <%= schema.human_singular %>"
38 | end
39 |
40 | test "renders errors when data is invalid", %{conn: conn} do
41 | conn = post(conn, Routes.<%= schema.route_helper %>_path(conn, :create), <%= schema.singular %>: @invalid_attrs)
42 | assert html_response(conn, 200) =~ "New <%= schema.human_singular %>"
43 | end
44 | end
45 |
46 | describe "edit <%= schema.singular %>" do
47 | setup [:create_<%= schema.singular %>]
48 |
49 | test "renders form for editing chosen <%= schema.singular %>", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
50 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :edit, <%= schema.singular %>))
51 | assert html_response(conn, 200) =~ "Edit <%= schema.human_singular %>"
52 | end
53 | end
54 |
55 | describe "update <%= schema.singular %>" do
56 | setup [:create_<%= schema.singular %>]
57 |
58 | test "redirects when data is valid", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
59 | conn = put(conn, Routes.<%= schema.route_helper %>_path(conn, :update, <%= schema.singular %>), <%= schema.singular %>: @update_attrs)
60 | assert redirected_to(conn) == Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>)
61 |
62 | conn = get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))<%= if schema.string_attr do %>
63 | assert html_response(conn, 200) =~ <%= inspect Mix.Phoenix.Schema.default_param(schema, :update) %><% else %>
64 | assert html_response(conn, 200)<% end %>
65 | end
66 |
67 | test "renders errors when data is invalid", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
68 | conn = put(conn, Routes.<%= schema.route_helper %>_path(conn, :update, <%= schema.singular %>), <%= schema.singular %>: @invalid_attrs)
69 | assert html_response(conn, 200) =~ "Edit <%= schema.human_singular %>"
70 | end
71 | end
72 |
73 | describe "delete <%= schema.singular %>" do
74 | setup [:create_<%= schema.singular %>]
75 |
76 | test "deletes chosen <%= schema.singular %>", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
77 | conn = delete(conn, Routes.<%= schema.route_helper %>_path(conn, :delete, <%= schema.singular %>))
78 | assert redirected_to(conn) == Routes.<%= schema.route_helper %>_path(conn, :index)
79 | assert_error_sent 404, fn ->
80 | get(conn, Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
81 | end
82 | end
83 | end
84 |
85 | defp create_<%= schema.singular %>(_) do
86 | <%= schema.singular %> = fixture(:<%= schema.singular %>)
87 | {:ok, <%= schema.singular %>: <%= schema.singular %>}
88 | end
89 | end
90 |
--------------------------------------------------------------------------------
/lib/tutorial_web/live/product_tagging_live.ex:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.ProductTaggingLive do
2 | use Phoenix.LiveView
3 |
4 | alias Tutorial.{Repo, Products, Taggable}
5 |
6 | def mount(_params, %{"id" => product_id, "csrf_token" => csrf_token} = _session, socket) do
7 | product = get_product(product_id, connected?(socket))
8 |
9 | assigns = [
10 | conn: socket,
11 | product: product,
12 | taggings: sorted(product.taggings),
13 | tags: [],
14 | # CHANNEL NAME
15 | channel_name: "app:#{csrf_token}",
16 | # THE FOLLOWING ONES REGARDS THE AUTOCOMPLETE
17 | # SEARCH BOX.
18 | search_results: [],
19 | search_phrase: "",
20 | current_focus: -1
21 | ]
22 |
23 | {:ok, assign(socket, assigns)}
24 | end
25 |
26 | def render(assigns) do
27 | TutorialWeb.ProductView.render("product_tagging.html", assigns)
28 | end
29 |
30 | def handle_event("search", %{"search_phrase" => search_phrase}, socket) do
31 | tags = if socket.assigns.tags == [], do: Taggable.list_tags, else: socket.assigns.tags
32 |
33 | assigns = [
34 | tags: tags,
35 | search_results: search(tags, search_phrase),
36 | search_phrase: search_phrase
37 | ]
38 |
39 | {:noreply, assign(socket, assigns)}
40 | end
41 |
42 | def handle_event("pick", %{"name" => search_phrase}, socket) do
43 | product = socket.assigns.product
44 | taggings = add_tagging_to_product(product, search_phrase)
45 | refocus_input(socket)
46 |
47 | assigns = [
48 | taggings: sorted(taggings),
49 | tags: [],
50 | search_results: [],
51 | search_phrase: ""
52 | ]
53 |
54 | {:noreply, assign(socket, assigns)}
55 | end
56 |
57 | def handle_event("delete", %{"tagging" => tagging_id}, socket) do
58 | taggings = delete_tagging_from_product(socket.assigns, tagging_id)
59 |
60 | assigns = [
61 | taggings: taggings
62 | ]
63 |
64 | {:noreply, assign(socket, assigns)}
65 | end
66 |
67 | def handle_event("submit", _, socket), do: {:noreply, socket} # PREVENT FORM SUBMIT
68 |
69 | def handle_event("set-focus", %{"keyCode" => 38}, socket) do # UP
70 | current_focus =
71 | Enum.max([(socket.assigns.current_focus - 1), 0])
72 | {:noreply, assign(socket, current_focus: current_focus)}
73 | end
74 |
75 | def handle_event("set-focus", %{"keyCode" => 40}, socket) do # DOWN
76 | current_focus =
77 | Enum.min([(socket.assigns.current_focus + 1), (length(socket.assigns.search_results)-1)])
78 | {:noreply, assign(socket, current_focus: current_focus)}
79 | end
80 |
81 | def handle_event("set-focus", %{"keyCode" => 13}, socket) do # ENTER
82 | search_phrase =
83 | case Enum.at(socket.assigns.search_results, socket.assigns.current_focus) do
84 | "" <> search_phrase -> search_phrase # PICK ONE FROM THE DROP DOWN LIST
85 | _ -> socket.assigns.search_phrase # PICK ONE FROM INPUT FIELD
86 | end
87 |
88 | handle_event("pick", %{"name" => search_phrase}, socket)
89 | end
90 |
91 | # FALLBACK FOR NON RELATED KEY STROKES
92 | def handle_event("set-focus", _, socket), do: {:noreply, socket}
93 |
94 | def handle_event("focus-input", _, socket), do: {:noreply, refocus_input(socket)}
95 |
96 | defp refocus_input(socket) do
97 | TutorialWeb.Endpoint.broadcast_from(self(), socket.assigns.channel_name, "focus", %{id: "tagging-form"})
98 | socket
99 | end
100 |
101 | defp search(_, ""), do: []
102 | defp search(tags, search_phrase) do
103 | tags
104 | |> Enum.map(& &1.name)
105 | |> Enum.sort()
106 | |> Enum.filter(& matches?(&1, search_phrase))
107 | end
108 |
109 | defp matches?(first, second) do
110 | String.starts_with?(
111 | String.downcase(first), String.downcase(second)
112 | )
113 | end
114 |
115 | defp get_product(_, false), do: %{taggings: []}
116 | defp get_product(product_id, _) do
117 | Products.get_product!(product_id) |> Repo.preload(:tags)
118 | end
119 |
120 | defp add_tagging_to_product(product, search_phrase) do
121 | Taggable.tag_product(product, %{tag: %{name: search_phrase}})
122 | %{taggings: taggings} = get_product(product.id, true)
123 |
124 | taggings
125 | end
126 |
127 | defp delete_tagging_from_product(%{product: product, taggings: taggings}, tagging_id) do
128 | taggings
129 | |> Enum.reject(fn tagging ->
130 | if "#{tagging.id}" == tagging_id do
131 | Taggable.delete_tag_from_product(product, tagging.tag)
132 | true
133 | else
134 | false
135 | end
136 | end)
137 | end
138 |
139 | defp sorted(taggings), do: Enum.sort_by(taggings, &(&1.id)) # MAKE SURE THE TAGS ARE ALWAYS SORTED BY ASC ORDER
140 | end
141 |
--------------------------------------------------------------------------------
/lib/tutorial/products.ex:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.Products do
2 | @moduledoc """
3 | The Products context.
4 | """
5 |
6 | import Ecto.Query, warn: false
7 | alias Tutorial.Repo
8 |
9 | alias Tutorial.Products.Product
10 | alias Tutorial.Products.Variant
11 |
12 | @doc """
13 | Returns the list of products.
14 |
15 | ## Examples
16 |
17 | iex> list_products()
18 | [%Product{}, ...]
19 |
20 | """
21 | def list_products do
22 | Repo.all(Product)
23 | end
24 |
25 | def paginate_products(params \\ []) do
26 | Product
27 | |> Repo.paginate(params)
28 | end
29 |
30 | @doc """
31 | Gets a single product.
32 |
33 | Raises `Ecto.NoResultsError` if the Product does not exist.
34 |
35 | ## Examples
36 |
37 | iex> get_product!(123)
38 | %Product{}
39 |
40 | iex> get_product!(456)
41 | ** (Ecto.NoResultsError)
42 |
43 | """
44 | def get_product!(id), do: Repo.get!(Product, id) |> Repo.preload(variants: from(v in Variant, order_by: v.id))
45 |
46 | @doc """
47 | Creates a product.
48 |
49 | ## Examples
50 |
51 | iex> create_product(%{field: value})
52 | {:ok, %Product{}}
53 |
54 | iex> create_product(%{field: bad_value})
55 | {:error, %Ecto.Changeset{}}
56 |
57 | """
58 | def create_product(attrs \\ %{}) do
59 | %Product{}
60 | |> Product.changeset(attrs)
61 | |> Repo.insert()
62 | end
63 |
64 | @doc """
65 | Updates a product.
66 |
67 | ## Examples
68 |
69 | iex> update_product(product, %{field: new_value})
70 | {:ok, %Product{}}
71 |
72 | iex> update_product(product, %{field: bad_value})
73 | {:error, %Ecto.Changeset{}}
74 |
75 | """
76 | def update_product(%Product{} = product, attrs) do
77 | product
78 | |> Product.changeset(attrs)
79 | |> Repo.update()
80 | end
81 |
82 | @doc """
83 | Deletes a Product.
84 |
85 | ## Examples
86 |
87 | iex> delete_product(product)
88 | {:ok, %Product{}}
89 |
90 | iex> delete_product(product)
91 | {:error, %Ecto.Changeset{}}
92 |
93 | """
94 | def delete_product(%Product{} = product) do
95 | Repo.delete(product)
96 | end
97 |
98 | @doc """
99 | Returns an `%Ecto.Changeset{}` for tracking product changes.
100 |
101 | ## Examples
102 |
103 | iex> change_product(product)
104 | %Ecto.Changeset{source: %Product{}}
105 |
106 | """
107 | def change_product(%Product{} = product) do
108 | Product.changeset(product, %{})
109 | end
110 |
111 | @doc """
112 | Returns the list of variants for a product.
113 |
114 | ## Examples
115 |
116 | iex> list_variants(product)
117 | [%Variant{}, ...]
118 |
119 | """
120 | def list_variants(product) do
121 | from(v in Variant, where: [product_id: ^product.id], order_by: [asc: :id])
122 | |> Repo.all()
123 | end
124 |
125 | @doc """
126 | Gets a single variant belonging to a product.
127 |
128 | Raises `Ecto.NoResultsError` if the Variant does not exist.
129 |
130 | ## Examples
131 |
132 | iex> get_variant!(product, 123)
133 | %Variant{}
134 |
135 | iex> get_variant!(product, 456)
136 | ** (Ecto.NoResultsError)
137 |
138 | """
139 | def get_variant!(product, id), do: Repo.get_by!(Variant, product_id: product.id, id: id)
140 |
141 | @doc """
142 | Creates a variant for a given product.
143 |
144 | ## Examples
145 |
146 | iex> create_variant(%{field: value})
147 | {:ok, %Variant{}}
148 |
149 | iex> create_variant(%{field: bad_value})
150 | {:error, %Ecto.Changeset{}}
151 |
152 | """
153 | def create_variant(product, attrs \\ %{}) do
154 | product
155 | |> Ecto.build_assoc(:variants)
156 | |> Variant.changeset(attrs)
157 | |> Repo.insert()
158 | end
159 |
160 | @doc """
161 | Updates a variant.
162 |
163 | ## Examples
164 |
165 | iex> update_variant(variant, %{field: new_value})
166 | {:ok, %Variant{}}
167 |
168 | iex> update_variant(variant, %{field: bad_value})
169 | {:error, %Ecto.Changeset{}}
170 |
171 | """
172 | def update_variant(%Variant{} = variant, attrs) do
173 | variant
174 | |> Variant.changeset(attrs)
175 | |> Repo.update()
176 | end
177 |
178 | @doc """
179 | Deletes a Variant.
180 |
181 | ## Examples
182 |
183 | iex> delete_variant(variant)
184 | {:ok, %Variant{}}
185 |
186 | iex> delete_variant(variant)
187 | {:error, %Ecto.Changeset{}}
188 |
189 | """
190 | def delete_variant(%Variant{} = variant) do
191 | Repo.delete(variant)
192 | end
193 |
194 | @doc """
195 | Returns an `%Ecto.Changeset{}` for tracking variant changes.
196 |
197 | ## Examples
198 |
199 | iex> change_variant(variant)
200 | %Ecto.Changeset{source: %Variant{}}
201 |
202 | """
203 | def change_variant(%Variant{} = variant) do
204 | Variant.changeset(variant, %{})
205 | end
206 | end
207 |
--------------------------------------------------------------------------------
/test/tutorial/products_test.exs:
--------------------------------------------------------------------------------
1 | defmodule Tutorial.ProductsTest do
2 | use Tutorial.DataCase
3 |
4 | alias Tutorial.Products
5 |
6 | def product_fixture(attrs \\ %{}) do
7 | {:ok, product} =
8 | attrs
9 | |> Enum.into(%{description: "some description", name: "some name", price: 120.5, properties: %{}})
10 | |> Products.create_product()
11 |
12 | product
13 | |> Tutorial.Repo.preload(:variants)
14 | end
15 |
16 | describe "products" do
17 | alias Tutorial.Products.Product
18 |
19 | @valid_attrs %{description: "some description", name: "some name", price: 120.5}
20 | @update_attrs %{description: "some updated description", name: "some updated name", price: 456.7}
21 | @invalid_attrs %{description: nil, name: nil, price: nil}
22 |
23 | test "list_products/0 returns all products" do
24 | %{id: id} = product_fixture()
25 | assert [%Product{id: ^id}] = Products.list_products()
26 | end
27 |
28 | test "get_product!/1 returns the product with given id" do
29 | product = product_fixture()
30 | assert Products.get_product!(product.id) == product
31 | end
32 |
33 | test "create_product/1 with valid data creates a product" do
34 | assert {:ok, %Product{} = product} = Products.create_product(@valid_attrs)
35 | assert product.description == "some description"
36 | assert product.name == "some name"
37 | assert product.price == 120.5
38 | end
39 |
40 | test "create_product/1 with invalid data returns error changeset" do
41 | assert {:error, %Ecto.Changeset{}} = Products.create_product(@invalid_attrs)
42 | end
43 |
44 | test "update_product/2 with valid data updates the product" do
45 | product = product_fixture()
46 | assert {:ok, %Product{} = product} = Products.update_product(product, @update_attrs)
47 | assert product.description == "some updated description"
48 | assert product.name == "some updated name"
49 | assert product.price == 456.7
50 | end
51 |
52 | test "update_product/2 with invalid data returns error changeset" do
53 | product = product_fixture()
54 | assert {:error, %Ecto.Changeset{}} = Products.update_product(product, @invalid_attrs)
55 | assert product == Products.get_product!(product.id)
56 | end
57 |
58 | test "delete_product/1 deletes the product" do
59 | product = product_fixture()
60 | assert {:ok, %Product{}} = Products.delete_product(product)
61 | assert_raise Ecto.NoResultsError, fn -> Products.get_product!(product.id) end
62 | end
63 |
64 | test "change_product/1 returns a product changeset" do
65 | product = product_fixture()
66 | assert %Ecto.Changeset{} = Products.change_product(product)
67 | end
68 | end
69 |
70 | describe "variants" do
71 | alias Tutorial.Products.Variant
72 |
73 | @valid_attrs %{name: "some name", value: "some value"}
74 | @update_attrs %{name: "some updated name", value: "some updated value"}
75 | @invalid_attrs %{name: nil, value: nil}
76 |
77 | def variant_fixture(product, attrs \\ %{}) do
78 | attrs = Enum.into(attrs, @valid_attrs)
79 | {:ok, variant} = Products.create_variant(product, attrs)
80 |
81 | variant
82 | end
83 |
84 | setup do
85 | product = product_fixture()
86 | {:ok, product: product}
87 | end
88 |
89 | test "list_variants/0 returns all variants", %{product: product} do
90 | variant = variant_fixture(product)
91 | assert Products.list_variants(product) == [variant]
92 | end
93 |
94 | test "get_variant!/1 returns the variant with given id", %{product: product} do
95 | variant = variant_fixture(product)
96 | assert Products.get_variant!(product, variant.id) == variant
97 | end
98 |
99 | test "create_variant/1 with valid data creates a variant", %{product: product} do
100 | assert {:ok, %Variant{} = variant} = Products.create_variant(product, @valid_attrs)
101 | assert variant.name == "some name"
102 | assert variant.value == "some value"
103 | end
104 |
105 | test "create_variant/1 with invalid data returns error changeset", %{product: product} do
106 | assert {:error, %Ecto.Changeset{}} = Products.create_variant(product, @invalid_attrs)
107 | end
108 |
109 | test "create_variant/1 with valid but duplicate data for same product returns error changeset", %{product: product} do
110 | Products.create_variant(product, @valid_attrs)
111 | assert {:error, %Ecto.Changeset{}} = Products.create_variant(product, @valid_attrs)
112 | end
113 |
114 | test "create_variant/1 with valid but duplicate data for another product is valid", %{product: product} do
115 | other_product = product_fixture()
116 | Products.create_variant(other_product, @valid_attrs)
117 |
118 | assert {:ok, %Variant{} = variant} = Products.create_variant(product, @valid_attrs)
119 | end
120 |
121 | test "update_variant/2 with valid data updates the variant", %{product: product} do
122 | variant = variant_fixture(product)
123 | assert {:ok, %Variant{} = variant} = Products.update_variant(variant, @update_attrs)
124 | assert variant.name == "some updated name"
125 | assert variant.value == "some updated value"
126 | end
127 |
128 | test "update_variant/2 with invalid data returns error changeset", %{product: product} do
129 | variant = variant_fixture(product)
130 | assert {:error, %Ecto.Changeset{}} = Products.update_variant(variant, @invalid_attrs)
131 | assert variant == Products.get_variant!(product, variant.id)
132 | end
133 |
134 | test "delete_variant/1 deletes the variant", %{product: product} do
135 | variant = variant_fixture(product)
136 | assert {:ok, %Variant{}} = Products.delete_variant(variant)
137 | assert_raise Ecto.NoResultsError, fn -> Products.get_variant!(product, variant.id) end
138 | end
139 |
140 | test "change_variant/1 returns a variant changeset", %{product: product} do
141 | variant = variant_fixture(product)
142 | assert %Ecto.Changeset{} = Products.change_variant(variant)
143 | end
144 | end
145 | end
146 |
--------------------------------------------------------------------------------
/lib/mix/tasks/swagger.gen.json.ex:
--------------------------------------------------------------------------------
1 | defmodule Mix.Tasks.Swagger.Gen.Json do
2 | @shortdoc "Generates controller, views, and context for a JSON resource with swagger documentation"
3 |
4 | @moduledoc """
5 | Generates controller, views, and context for a JSON resource.
6 |
7 | mix swagger.gen.json Accounts User users name:string age:integer
8 |
9 | The first argument is the context module followed by the schema module
10 | and its plural name (used as the schema table name).
11 |
12 | The context is an Elixir module that serves as an API boundary for
13 | the given resource. A context often holds many related resources.
14 | Therefore, if the context already exists, it will be augmented with
15 | functions for the given resource.
16 |
17 | > Note: A resource may also be split
18 | > over distinct contexts (such as `Accounts.User` and `Payments.User`).
19 |
20 | The schema is responsible for mapping the database fields into an
21 | Elixir struct.
22 |
23 | Overall, this generator will add the following files to `lib/`:
24 |
25 | * a context module in `lib/app/accounts.ex` for the accounts API
26 | * a schema in `lib/app/accounts/user.ex`, with an `users` table
27 | * a view in `lib/app_web/views/user_view.ex`
28 | * a controller in `lib/app_web/controllers/user_controller.ex`
29 |
30 | A migration file for the repository and test files for the context and
31 | controller features will also be generated.
32 |
33 | The location of the web files (controllers, views, templates, etc) in an
34 | umbrella application will vary based on the `:context_app` config located
35 | in your applications `:generators` configuration. When set, the Phoenix
36 | generators will generate web files directly in your lib and test folders
37 | since the application is assumed to be isolated to web specific functionality.
38 | If `:context_app` is not set, the generators will place web related lib
39 | and test files in a `web/` directory since the application is assumed
40 | to be handling both web and domain specific functionality.
41 | Example configuration:
42 |
43 | config :my_app_web, :generators, context_app: :my_app
44 |
45 | Alternatively, the `--context-app` option may be supplied to the generator:
46 |
47 | mix swagger.gen.json Sales User users --context-app warehouse
48 |
49 | ## Web namespace
50 |
51 | By default, the controller and view will be namespaced by the schema name.
52 | You can customize the web module namespace by passing the `--web` flag with a
53 | module name, for example:
54 |
55 | mix swagger.gen.json Sales User users --web Sales
56 |
57 | Which would generate a `lib/app_web/controllers/sales/user_controller.ex` and
58 | `lib/app_web/views/sales/user_view.ex`.
59 |
60 | ## Generating without a schema or context file
61 |
62 | In some cases, you may wish to bootstrap JSON views, controllers, and
63 | controller tests, but leave internal implementation of the context or schema
64 | to yourself. You can use the `--no-context` and `--no-schema` flags for
65 | file generation control.
66 |
67 | ## table
68 |
69 | By default, the table name for the migration and schema will be
70 | the plural name provided for the resource. To customize this value,
71 | a `--table` option may be provided. For example:
72 |
73 | mix swagger.gen.json Accounts User users --table cms_users
74 |
75 | ## binary_id
76 |
77 | Generated migration can use `binary_id` for schema's primary key
78 | and its references with option `--binary-id`.
79 |
80 | ## Default options
81 |
82 | This generator uses default options provided in the `:generators`
83 | configuration of your application. These are the defaults:
84 |
85 | config :your_app, :generators,
86 | migration: true,
87 | binary_id: false,
88 | sample_binary_id: "11111111-1111-1111-1111-111111111111"
89 |
90 | You can override those options per invocation by providing corresponding
91 | switches, e.g. `--no-binary-id` to use normal ids despite the default
92 | configuration or `--migration` to force generation of the migration.
93 |
94 | Read the documentation for `swagger.gen.schema` for more information on
95 | attributes.
96 | """
97 |
98 | use Mix.Task
99 |
100 | alias Mix.Phoenix.Context
101 | alias Mix.Tasks.Phx.Gen
102 |
103 | @doc false
104 | def run(args) do
105 | if Mix.Project.umbrella?() do
106 | Mix.raise "mix swagger.gen.json can only be run inside an application directory"
107 | end
108 |
109 | {context, schema} = Gen.Context.build(args)
110 | Gen.Context.prompt_for_code_injection(context)
111 |
112 | binding = [context: context, schema: schema]
113 | paths = Mix.Phoenix.generator_paths()
114 |
115 | prompt_for_conflicts(context)
116 |
117 | context
118 | |> copy_new_files(paths, binding)
119 | |> print_shell_instructions()
120 | end
121 |
122 | defp prompt_for_conflicts(context) do
123 | context
124 | |> files_to_be_generated()
125 | |> Kernel.++(context_files(context))
126 | |> Mix.Phoenix.prompt_for_conflicts()
127 | end
128 | defp context_files(%Context{generate?: true} = context) do
129 | Gen.Context.files_to_be_generated(context)
130 | end
131 | defp context_files(%Context{generate?: false}) do
132 | []
133 | end
134 |
135 | @doc false
136 | def files_to_be_generated(%Context{schema: schema, context_app: context_app}) do
137 | web_prefix = Mix.Phoenix.web_path(context_app)
138 | test_prefix = Mix.Phoenix.web_test_path(context_app)
139 | web_path = to_string(schema.web_path)
140 |
141 | [
142 | {:eex, "controller.ex", Path.join([web_prefix, "controllers", web_path, "#{schema.singular}_controller.ex"])},
143 | {:eex, "view.ex", Path.join([web_prefix, "views", web_path, "#{schema.singular}_view.ex"])},
144 | {:eex, "controller_test.exs", Path.join([test_prefix, "controllers", web_path, "#{schema.singular}_controller_test.exs"])},
145 | {:new_eex, "changeset_view.ex", Path.join([web_prefix, "views/changeset_view.ex"])},
146 | {:new_eex, "fallback_controller.ex", Path.join([web_prefix, "controllers/fallback_controller.ex"])},
147 | ]
148 | end
149 |
150 | @doc false
151 | def copy_new_files(%Context{} = context, paths, binding) do
152 | files = files_to_be_generated(context)
153 | Mix.Phoenix.copy_from paths, "priv/templates/swagger.gen.json", binding, files
154 | if context.generate?, do: Gen.Context.copy_new_files(context, paths, binding)
155 |
156 | context
157 | end
158 |
159 | @doc false
160 | def print_shell_instructions(%Context{schema: schema, context_app: ctx_app} = context) do
161 | if schema.web_namespace do
162 | Mix.shell().info """
163 |
164 | Add the resource to your #{schema.web_namespace} :api scope in #{Mix.Phoenix.web_path(ctx_app)}/router.ex:
165 |
166 | scope "/#{schema.web_path}", #{inspect Module.concat(context.web_module, schema.web_namespace)} do
167 | pipe_through :api
168 | ...
169 | resources "/#{schema.plural}", #{inspect schema.alias}Controller
170 | end
171 | """
172 | else
173 | Mix.shell().info """
174 |
175 | Add the resource to your :api scope in #{Mix.Phoenix.web_path(ctx_app)}/router.ex:
176 |
177 | resources "/#{schema.plural}", #{inspect schema.alias}Controller, except: [:new, :edit]
178 | """
179 | end
180 | if context.generate?, do: Gen.Context.print_shell_instructions(context)
181 | end
182 | end
183 |
--------------------------------------------------------------------------------
/priv/templates/swagger.gen.json/controller.ex:
--------------------------------------------------------------------------------
1 | defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>Controller do
2 | use <%= inspect context.web_module %>, :controller
3 | use PhoenixSwagger
4 |
5 | alias <%= inspect context.module %>
6 | alias <%= inspect schema.module %>
7 |
8 | action_fallback <%= inspect context.web_module %>.FallbackController
9 |
10 | swagger_path :index do
11 | get "/api/<%= schema.plural %>"
12 | summary "List <%= schema.plural %>"
13 | description "List all <%= schema.plural %> in the database"
14 | tag "<%= inspect schema.alias %>s"
15 | produces "application/json"
16 | response(200, "OK", Schema.ref(:<%= inspect schema.alias %>sResponse),
17 | example: %{
18 | data: [
19 | %{
20 | id: 1,
21 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>,
22 | }
23 | ]
24 | }
25 | )
26 | end
27 |
28 | def index(conn, _params) do
29 | <%= schema.plural %> = <%= inspect context.alias %>.list_<%= schema.plural %>()
30 | render(conn, "index.json", <%= schema.plural %>: <%= schema.plural %>)
31 | end
32 |
33 | swagger_path :create do
34 | post "/api/<%= schema.plural %>"
35 | summary "Create <%= schema.singular %>"
36 | description "Creates a new <%= schema.singular %>"
37 | tag "<%= inspect schema.alias %>s"
38 | consumes "application/json"
39 | produces "application/json"
40 |
41 | parameter(:<%= schema.singular %>, :body, Schema.ref(:<%= inspect schema.alias %>Request), "The <%= schema.singular %> details",
42 | example: %{
43 | <%= schema.singular %>: %{<%= schema.params.create |> Enum.map(fn {key, val} -> "#{key}: #{inspect(val)}" end) |> Enum.join(", ") %>}
44 | }
45 | )
46 |
47 | response(201, "<%= inspect schema.alias %> created OK", Schema.ref(:<%= inspect schema.alias %>Response),
48 | example: %{
49 | data: %{
50 | id: 1,
51 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>,
52 | }
53 | }
54 | )
55 | end
56 |
57 | def create(conn, %{<%= inspect schema.singular %> => <%= schema.singular %>_params}) do
58 | with {:ok, %<%= inspect schema.alias %>{} = <%= schema.singular %>} <- <%= inspect context.alias %>.create_<%= schema.singular %>(<%= schema.singular %>_params) do
59 | conn
60 | |> put_status(:created)
61 | |> put_resp_header("location", Routes.<%= schema.route_helper %>_path(conn, :show, <%= schema.singular %>))
62 | |> render("show.json", <%= schema.singular %>: <%= schema.singular %>)
63 | end
64 | end
65 |
66 | swagger_path :show do
67 | summary "Show <%= inspect schema.alias %>"
68 | description "Show a <%= schema.singular %> by ID"
69 | tag "<%= inspect schema.alias %>s"
70 | produces "application/json"
71 | parameter :id, :path, :integer, "<%= inspect schema.alias %> ID", required: true, example: 123
72 |
73 | response(200, "OK", Schema.ref(:<%= inspect schema.alias %>Response),
74 | example: %{
75 | data: %{
76 | id: 123,
77 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>,
78 | }
79 | }
80 | )
81 | end
82 |
83 | def show(conn, %{"id" => id}) do
84 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
85 | render(conn, "show.json", <%= schema.singular %>: <%= schema.singular %>)
86 | end
87 |
88 | swagger_path :update do
89 | put "/api/<%= schema.plural %>/{id}"
90 | summary "Update <%= schema.singular %>"
91 | description "Update all attributes of a <%= schema.singular %>"
92 | tag "<%= inspect schema.alias %>s"
93 | consumes "application/json"
94 | produces "application/json"
95 |
96 | parameters do
97 | id(:path, :integer, "<%= inspect schema.alias %> ID", required: true, example: 3)
98 |
99 | <%= schema.singular %>(:body, Schema.ref(:<%= inspect schema.alias %>Request), "The <%= schema.singular %> details",
100 | example: %{
101 | <%= schema.singular %>: %{<%= schema.params.create |> Enum.map(fn {key, val} -> "#{key}: #{inspect(val)}" end) |> Enum.join(", ") %>}
102 | }
103 | )
104 | end
105 |
106 | response(200, "Updated Successfully", Schema.ref(:<%= inspect schema.alias %>Response),
107 | example: %{
108 | data: %{
109 | id: 3,
110 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>,
111 | }
112 | }
113 | )
114 | end
115 |
116 | def update(conn, %{"id" => id, <%= inspect schema.singular %> => <%= schema.singular %>_params}) do
117 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
118 |
119 | with {:ok, %<%= inspect schema.alias %>{} = <%= schema.singular %>} <- <%= inspect context.alias %>.update_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params) do
120 | render(conn, "show.json", <%= schema.singular %>: <%= schema.singular %>)
121 | end
122 | end
123 |
124 | swagger_path :delete do
125 | PhoenixSwagger.Path.delete "/api/<%= schema.plural %>/{id}"
126 | summary "Delete <%= inspect schema.alias %>"
127 | description "Delete a <%= schema.singular %> by ID"
128 | tag "<%= inspect schema.alias %>s"
129 | parameter :id, :path, :integer, "<%= inspect schema.alias %> ID", required: true, example: 3
130 | response 203, "No Content - Deleted Successfully"
131 | end
132 |
133 | def delete(conn, %{"id" => id}) do
134 | <%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
135 |
136 | with {:ok, %<%= inspect schema.alias %>{}} <- <%= inspect context.alias %>.delete_<%= schema.singular %>(<%= schema.singular %>) do
137 | send_resp(conn, :no_content, "")
138 | end
139 | end
140 |
141 | def swagger_definitions do
142 | %{
143 | <%= inspect schema.alias %>: swagger_schema do
144 | title "<%= inspect schema.alias %>"
145 | description "A <%= schema.singular %> of the app"
146 |
147 | properties do
148 | id :integer, "<%= inspect schema.alias %> ID"
149 | <%= schema.params.create |> Enum.map(fn {key, _val} -> ~s( #{key} :string, "#{inspect schema.alias} #{key}") end) |> Enum.join("\n") %>
150 | end
151 |
152 | example(%{
153 | id: 123,
154 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>,
155 | })
156 | end,
157 | <%= inspect schema.alias %>Request: swagger_schema do
158 | title "<%= inspect schema.alias %>Request"
159 | description "POST body for creating a <%= schema.singular %>"
160 | property :<%= schema.singular %>, Schema.ref(:<%= inspect schema.alias %>), "The <%= schema.singular %> details"
161 | example(%{
162 | <%= schema.singular %>: %{
163 | <%= schema.params.create |> Enum.map(fn {key, val} -> " #{key}: #{inspect(val)}" end) |> Enum.join(",\n") %>,
164 | }
165 | })
166 | end,
167 | <%= inspect schema.alias %>Response: swagger_schema do
168 | title "<%= inspect schema.alias %>Response"
169 | description "Response schema for single <%= schema.singular %>"
170 | property :data, Schema.ref(:<%= inspect schema.alias %>), "The <%= schema.singular %> details"
171 | end,
172 | <%= inspect schema.alias %>sResponse: swagger_schema do
173 | title "<%= inspect schema.alias %>sReponse"
174 | description "Response schema for multiple <%= schema.plural %>"
175 | property :data, Schema.array(:<%= inspect schema.alias %>), "The <%= schema.plural %> details"
176 | end
177 | }
178 | end
179 | end
180 |
--------------------------------------------------------------------------------
/test/tutorial_web/controllers/secret_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule TutorialWeb.SecretControllerTest do
2 | use TutorialWeb.ConnCase
3 |
4 | alias Tutorial.Secrets
5 | alias Tutorial.Repo
6 | alias Tutorial.Users.User
7 |
8 | @create_attrs %{key: "some key", value: "some value"}
9 | @update_attrs %{key: "some updated key", value: "some updated value"}
10 | @invalid_attrs %{key: nil, value: nil}
11 |
12 | defp login(%{conn: conn}) do
13 | user = create_user()
14 | conn = Pow.Plug.assign_current_user(conn, user, otp_app: :mail_flow_admin)
15 |
16 | {:ok, conn: conn}
17 | end
18 |
19 | defp create_user do
20 | user_attrs = %{account_name: "Acme Corp", email: "john.doe@example.com", password: "SuperSecret123", password_confirmation: "SuperSecret123"}
21 |
22 | {:ok, user} = User.changeset(%User{}, user_attrs)
23 | |> Repo.insert()
24 |
25 | user
26 | |> Tutorial.Repo.preload(:account)
27 | end
28 |
29 | defp create_secret(%{conn: conn}) do
30 | {:ok, secret} =
31 | case conn.assigns do
32 | %{current_user: %{account: account}} -> Secrets.create_secret(account, @create_attrs)
33 | _ ->
34 | another_account = create_user().account
35 | Secrets.create_secret(another_account, @create_attrs)
36 | end
37 |
38 | {:ok, secret: secret}
39 | end
40 |
41 | describe "index as not logged in" do
42 | test "redirects to login", %{conn: conn} do
43 | conn = get(conn, Routes.secret_path(conn, :index))
44 | assert redirected_to(conn) =~ Routes.pow_session_path(conn, :new)
45 | end
46 | end
47 |
48 | describe "index" do
49 | setup [:login]
50 |
51 | test "lists all secrets", %{conn: conn} do
52 | conn = get(conn, Routes.secret_path(conn, :index))
53 | assert html_response(conn, 200) =~ "Secrets"
54 | end
55 | end
56 |
57 | describe "new secret as not logged in" do
58 | test "redirects to login", %{conn: conn} do
59 | conn = get(conn, Routes.secret_path(conn, :new))
60 | assert redirected_to(conn) =~ Routes.pow_session_path(conn, :new)
61 | end
62 | end
63 |
64 | describe "new secret" do
65 | setup [:login]
66 |
67 | test "renders form", %{conn: conn} do
68 | conn = get(conn, Routes.secret_path(conn, :new))
69 | assert html_response(conn, 200) =~ "New Secret"
70 | end
71 | end
72 |
73 | describe "create secret as not logged in" do
74 | test "redirects to login", %{conn: conn} do
75 | conn = post(conn, Routes.secret_path(conn, :create), secret: @create_attrs)
76 | assert redirected_to(conn) =~ Routes.pow_session_path(conn, :new)
77 | end
78 | end
79 |
80 | describe "create secret" do
81 | setup [:login]
82 |
83 | test "redirects to show when data is valid", %{conn: conn} do
84 | response = post(conn, Routes.secret_path(conn, :create), secret: @create_attrs)
85 |
86 | assert %{id: id} = redirected_params(response)
87 | assert redirected_to(response) == Routes.secret_path(response, :show, id)
88 |
89 | conn = get(conn, Routes.secret_path(conn, :show, id))
90 | assert html_response(conn, 200) =~ "Show Secret"
91 | end
92 |
93 | test "renders errors when data is invalid", %{conn: conn} do
94 | conn = post(conn, Routes.secret_path(conn, :create), secret: @invalid_attrs)
95 | assert html_response(conn, 200) =~ "New Secret"
96 | end
97 | end
98 |
99 | describe "edit secret as not logged in" do
100 | setup [:create_secret]
101 |
102 | test "redirects to login", %{conn: conn, secret: secret} do
103 | conn = get(conn, Routes.secret_path(conn, :edit, secret))
104 | assert redirected_to(conn) =~ Routes.pow_session_path(conn, :new)
105 | end
106 | end
107 |
108 | describe "edit secret" do
109 | setup [:login, :create_secret]
110 |
111 | test "renders form for editing chosen secret", %{conn: conn, secret: secret} do
112 | conn = get(conn, Routes.secret_path(conn, :edit, secret))
113 | assert html_response(conn, 200) =~ "Edit Secret"
114 | end
115 | end
116 |
117 | describe "update secret as not logged in" do
118 | setup [:create_secret]
119 |
120 | test "redirects to login", %{conn: conn, secret: secret} do
121 | conn = put(conn, Routes.secret_path(conn, :update, secret), secret: @update_attrs)
122 | assert redirected_to(conn) =~ Routes.pow_session_path(conn, :new)
123 | end
124 | end
125 |
126 | describe "update secret" do
127 | setup [:login, :create_secret]
128 |
129 | test "redirects when data is valid", %{conn: conn, secret: secret} do
130 | response = put(conn, Routes.secret_path(conn, :update, secret), secret: @update_attrs)
131 | assert redirected_to(response) == Routes.secret_path(response, :show, secret)
132 |
133 | conn = get(conn, Routes.secret_path(conn, :show, secret))
134 | assert html_response(conn, 200) =~ "some updated key"
135 | end
136 |
137 | test "renders errors when data is invalid", %{conn: conn, secret: secret} do
138 | conn = put(conn, Routes.secret_path(conn, :update, secret), secret: @invalid_attrs)
139 | assert html_response(conn, 200) =~ "Edit Secret"
140 | end
141 | end
142 |
143 | describe "delete secret as not logged in" do
144 | setup [:create_secret]
145 |
146 | test "redirects to login", %{conn: conn, secret: secret} do
147 | conn = delete(conn, Routes.secret_path(conn, :delete, secret))
148 | assert redirected_to(conn) =~ Routes.pow_session_path(conn, :new)
149 | end
150 | end
151 |
152 | describe "delete secret" do
153 | setup [:login, :create_secret]
154 |
155 | test "deletes chosen secret", %{conn: conn, secret: secret} do
156 | response = delete(conn, Routes.secret_path(conn, :delete, secret))
157 | assert redirected_to(response) == Routes.secret_path(response, :index)
158 |
159 | assert_error_sent 404, fn ->
160 | get(conn, Routes.secret_path(conn, :show, secret))
161 | end
162 | end
163 | end
164 |
165 | #
166 | # describe "index" do
167 | # test "lists all secrets", %{conn: conn} do
168 | # conn = get(conn, Routes.secret_path(conn, :index))
169 | # assert html_response(conn, 200) =~ "Listing Secrets"
170 | # end
171 | # end
172 | #
173 | # describe "new secret" do
174 | # test "renders form", %{conn: conn} do
175 | # conn = get(conn, Routes.secret_path(conn, :new))
176 | # assert html_response(conn, 200) =~ "New Secret"
177 | # end
178 | # end
179 | #
180 | # describe "create secret" do
181 | # test "redirects to show when data is valid", %{conn: conn} do
182 | # conn = post(conn, Routes.secret_path(conn, :create), secret: @create_attrs)
183 | #
184 | # assert %{id: id} = redirected_params(conn)
185 | # assert redirected_to(conn) == Routes.secret_path(conn, :show, id)
186 | #
187 | # conn = get(conn, Routes.secret_path(conn, :show, id))
188 | # assert html_response(conn, 200) =~ "Show Secret"
189 | # end
190 | #
191 | # test "renders errors when data is invalid", %{conn: conn} do
192 | # conn = post(conn, Routes.secret_path(conn, :create), secret: @invalid_attrs)
193 | # assert html_response(conn, 200) =~ "New Secret"
194 | # end
195 | # end
196 | #
197 | # describe "edit secret" do
198 | # setup [:create_secret]
199 | #
200 | # test "renders form for editing chosen secret", %{conn: conn, secret: secret} do
201 | # conn = get(conn, Routes.secret_path(conn, :edit, secret))
202 | # assert html_response(conn, 200) =~ "Edit Secret"
203 | # end
204 | # end
205 | #
206 | # describe "update secret" do
207 | # setup [:create_secret]
208 | #
209 | # test "redirects when data is valid", %{conn: conn, secret: secret} do
210 | # conn = put(conn, Routes.secret_path(conn, :update, secret), secret: @update_attrs)
211 | # assert redirected_to(conn) == Routes.secret_path(conn, :show, secret)
212 | #
213 | # conn = get(conn, Routes.secret_path(conn, :show, secret))
214 | # assert html_response(conn, 200) =~ "some updated key"
215 | # end
216 | #
217 | # test "renders errors when data is invalid", %{conn: conn, secret: secret} do
218 | # conn = put(conn, Routes.secret_path(conn, :update, secret), secret: @invalid_attrs)
219 | # assert html_response(conn, 200) =~ "Edit Secret"
220 | # end
221 | # end
222 | #
223 | # describe "delete secret" do
224 | # setup [:create_secret]
225 | #
226 | # test "deletes chosen secret", %{conn: conn, secret: secret} do
227 | # conn = delete(conn, Routes.secret_path(conn, :delete, secret))
228 | # assert redirected_to(conn) == Routes.secret_path(conn, :index)
229 | # assert_error_sent 404, fn ->
230 | # get(conn, Routes.secret_path(conn, :show, secret))
231 | # end
232 | # end
233 | # end
234 | end
235 |
--------------------------------------------------------------------------------
/lib/mix/tasks/tailwind.gen.html.ex:
--------------------------------------------------------------------------------
1 | defmodule Mix.Tasks.Tailwind.Gen.Html do
2 | @shortdoc "Generates controller, views, and context for an HTML resource"
3 |
4 | @moduledoc """
5 | Generates controller, views, and context for an HTML resource.
6 |
7 | mix tailwind.gen.html Accounts User users name:string age:integer
8 |
9 | The first argument is the context module followed by the schema module
10 | and its plural name (used as the schema table name).
11 |
12 | The context is an Elixir module that serves as an API boundary for
13 | the given resource. A context often holds many related resources.
14 | Therefore, if the context already exists, it will be augmented with
15 | functions for the given resource.
16 |
17 | > Note: A resource may also be split
18 | > over distinct contexts (such as `Accounts.User` and `Payments.User`).
19 |
20 | The schema is responsible for mapping the database fields into an
21 | Elixir struct.
22 |
23 | Overall, this generator will add the following files to `lib/`:
24 |
25 | * a context module in `lib/app/accounts.ex` for the accounts API
26 | * a schema in `lib/app/accounts/user.ex`, with an `users` table
27 | * a view in `lib/app_web/views/user_view.ex`
28 | * a controller in `lib/app_web/controllers/user_controller.ex`
29 | * default CRUD templates in `lib/app_web/templates/user`
30 |
31 | A migration file for the repository and test files for the context and
32 | controller features will also be generated.
33 |
34 | The location of the web files (controllers, views, templates, etc) in an
35 | umbrella application will vary based on the `:context_app` config located
36 | in your applications `:generators` configuration. When set, the Phoenix
37 | generators will generate web files directly in your lib and test folders
38 | since the application is assumed to be isolated to web specific functionality.
39 | If `:context_app` is not set, the generators will place web related lib
40 | and test files in a `web/` directory since the application is assumed
41 | to be handling both web and domain specific functionality.
42 | Example configuration:
43 |
44 | config :my_app_web, :generators, context_app: :my_app
45 |
46 | Alternatively, the `--context-app` option may be supplied to the generator:
47 |
48 | mix tailwind.gen.html Sales User users --context-app warehouse
49 |
50 | ## Web namespace
51 |
52 | By default, the controller and view will be namespaced by the schema name.
53 | You can customize the web module namespace by passing the `--web` flag with a
54 | module name, for example:
55 |
56 | mix tailwind.gen.html Sales User users --web Sales
57 |
58 | Which would generate a `lib/app_web/controllers/sales/user_controller.ex` and
59 | `lib/app_web/views/sales/user_view.ex`.
60 |
61 | ## Generating without a schema or context file
62 |
63 | In some cases, you may wish to bootstrap HTML templates, controllers, and
64 | controller tests, but leave internal implementation of the context or schema
65 | to yourself. You can use the `--no-context` and `--no-schema` flags for
66 | file generation control.
67 |
68 | ## table
69 |
70 | By default, the table name for the migration and schema will be
71 | the plural name provided for the resource. To customize this value,
72 | a `--table` option may be provided. For example:
73 |
74 | mix tailwind.gen.html Accounts User users --table cms_users
75 |
76 | ## binary_id
77 |
78 | Generated migration can use `binary_id` for schema's primary key
79 | and its references with option `--binary-id`.
80 |
81 | ## Default options
82 |
83 | This generator uses default options provided in the `:generators`
84 | configuration of your application. These are the defaults:
85 |
86 | config :your_app, :generators,
87 | migration: true,
88 | binary_id: false,
89 | sample_binary_id: "11111111-1111-1111-1111-111111111111"
90 |
91 | You can override those options per invocation by providing corresponding
92 | switches, e.g. `--no-binary-id` to use normal ids despite the default
93 | configuration or `--migration` to force generation of the migration.
94 |
95 | Read the documentation for `tailwind.gen.schema` for more information on
96 | attributes.
97 | """
98 | use Mix.Task
99 |
100 | alias Mix.Phoenix.{Context, Schema}
101 | alias Mix.Tasks.Phx.Gen
102 |
103 | @doc false
104 | def run(args) do
105 | if Mix.Project.umbrella?() do
106 | Mix.raise "mix tailwind.gen.html can only be run inside an application directory"
107 | end
108 |
109 | {context, schema} = Gen.Context.build(args)
110 | Gen.Context.prompt_for_code_injection(context)
111 |
112 | binding = [context: context, schema: schema, inputs: inputs(schema)]
113 | paths = Mix.Phoenix.generator_paths()
114 |
115 | prompt_for_conflicts(context)
116 |
117 | context
118 | |> copy_new_files(paths, binding)
119 | |> print_shell_instructions()
120 | end
121 |
122 | defp prompt_for_conflicts(context) do
123 | context
124 | |> files_to_be_generated()
125 | |> Kernel.++(context_files(context))
126 | |> Mix.Phoenix.prompt_for_conflicts()
127 | end
128 | defp context_files(%Context{generate?: true} = context) do
129 | Gen.Context.files_to_be_generated(context)
130 | end
131 | defp context_files(%Context{generate?: false}) do
132 | []
133 | end
134 |
135 | @doc false
136 | def files_to_be_generated(%Context{schema: schema, context_app: context_app}) do
137 | web_prefix = Mix.Phoenix.web_path(context_app)
138 | test_prefix = Mix.Phoenix.web_test_path(context_app)
139 | web_path = to_string(schema.web_path)
140 |
141 | [
142 | {:eex, "controller.ex", Path.join([web_prefix, "controllers", web_path, "#{schema.singular}_controller.ex"])},
143 | {:eex, "edit.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "edit.html.eex"])},
144 | {:eex, "form.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "form.html.eex"])},
145 | {:eex, "index.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "index.html.eex"])},
146 | {:eex, "new.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "new.html.eex"])},
147 | {:eex, "show.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "show.html.eex"])},
148 | {:eex, "view.ex", Path.join([web_prefix, "views", web_path, "#{schema.singular}_view.ex"])},
149 | {:eex, "controller_test.exs", Path.join([test_prefix, "controllers", web_path, "#{schema.singular}_controller_test.exs"])},
150 | ]
151 | end
152 |
153 | @doc false
154 | def copy_new_files(%Context{} = context, paths, binding) do
155 | files = files_to_be_generated(context)
156 | Mix.Phoenix.copy_from(paths, "priv/templates/tailwind.gen.html", binding, files)
157 | if context.generate?, do: Gen.Context.copy_new_files(context, paths, binding)
158 | context
159 | end
160 |
161 | @doc false
162 | def print_shell_instructions(%Context{schema: schema, context_app: ctx_app} = context) do
163 | if schema.web_namespace do
164 | Mix.shell().info """
165 |
166 | Add the resource to your #{schema.web_namespace} :browser scope in #{Mix.Phoenix.web_path(ctx_app)}/router.ex:
167 |
168 | scope "/#{schema.web_path}", #{inspect Module.concat(context.web_module, schema.web_namespace)}, as: :#{schema.web_path} do
169 | pipe_through :browser
170 | ...
171 | resources "/#{schema.plural}", #{inspect schema.alias}Controller
172 | end
173 | """
174 | else
175 | Mix.shell().info """
176 |
177 | Add the resource to your browser scope in #{Mix.Phoenix.web_path(ctx_app)}/router.ex:
178 |
179 | resources "/#{schema.plural}", #{inspect schema.alias}Controller
180 | """
181 | end
182 | if context.generate?, do: Gen.Context.print_shell_instructions(context)
183 | end
184 |
185 | defp inputs(%Schema{} = schema) do
186 | Enum.map(schema.attrs, fn
187 | {_, {:references, _}} ->
188 | {nil, nil, nil}
189 | {key, :integer} ->
190 | {label(key), ~s(<%= number_input f, #{inspect(key)}, class: "form-control" %>), error(key)}
191 | {key, :float} ->
192 | {label(key), ~s(<%= number_input f, #{inspect(key)}, step: "any", class: "form-control" %>), error(key)}
193 | {key, :decimal} ->
194 | {label(key), ~s(<%= number_input f, #{inspect(key)}, step: "any", class: "form-control" %>), error(key)}
195 | {key, :boolean} ->
196 | {label(key), ~s(<%= checkbox f, #{inspect(key)} %>), error(key)}
197 | {key, :text} ->
198 | {label(key), ~s(<%= textarea f, #{inspect(key)}, class: "form-control" %>), error(key)}
199 | {key, :date} ->
200 | {label(key), ~s(<%= date_select f, #{inspect(key)} %>), error(key)}
201 | {key, :time} ->
202 | {label(key), ~s(<%= time_select f, #{inspect(key)} %>), error(key)}
203 | {key, :utc_datetime} ->
204 | {label(key), ~s(<%= datetime_select f, #{inspect(key)} %>), error(key)}
205 | {key, :naive_datetime} ->
206 | {label(key), ~s(<%= datetime_select f, #{inspect(key)} %>), error(key)}
207 | {key, {:array, :integer}} ->
208 | {label(key), ~s(<%= multiple_select f, #{inspect(key)}, ["1": 1, "2": 2], class: "form-control" %>), error(key)}
209 | {key, {:array, _}} ->
210 | {label(key), ~s(<%= multiple_select f, #{inspect(key)}, ["Option 1": "option1", "Option 2": "option2"], class: "form-control" %>), error(key)}
211 | {key, _} ->
212 | {label(key), ~s(<%= text_input f, #{inspect(key)}, class: "form-control" %>), error(key)}
213 | end)
214 | end
215 |
216 | defp label(key) do
217 | ~s(<%= label f, #{inspect(key)} %>)
218 | end
219 |
220 | defp error(field) do
221 | ~s(<%= error_tag f, #{inspect(field)} %>)
222 | end
223 | end
224 |
--------------------------------------------------------------------------------
/assets/css/phoenix.css:
--------------------------------------------------------------------------------
1 | /* Includes some default style for the starter application.
2 | * This can be safely deleted to start fresh.
3 | */
4 |
5 | /* Milligram v1.3.0 https://milligram.github.io
6 | * Copyright (c) 2017 CJ Patoilo Licensed under the MIT license
7 | */
8 |
9 | *,*:after,*:before{box-sizing:inherit}html{box-sizing:border-box;font-size:62.5%}body{color:#000000;font-family:'Helvetica', 'Arial', sans-serif;font-size:1.6em;font-weight:300;line-height:1.6}blockquote{border-left:0.3rem solid #d1d1d1;margin-left:0;margin-right:0;padding:1rem 1.5rem}blockquote *:last-child{margin-bottom:0}.button,button,input[type='button'],input[type='reset'],input[type='submit']{background-color:#0069d9;border:0.1rem solid #0069d9;border-radius:.4rem;color:#fff;cursor:pointer;display:inline-block;font-size:1.1rem;font-weight:700;height:3.8rem;letter-spacing:.1rem;line-height:3.8rem;padding:0 3.0rem;text-align:center;text-decoration:none;text-transform:uppercase;white-space:nowrap}.button:focus,.button:hover,button:focus,button:hover,input[type='button']:focus,input[type='button']:hover,input[type='reset']:focus,input[type='reset']:hover,input[type='submit']:focus,input[type='submit']:hover{background-color:#606c76;border-color:#606c76;color:#fff;outline:0}.button[disabled],button[disabled],input[type='button'][disabled],input[type='reset'][disabled],input[type='submit'][disabled]{cursor:default;opacity:.5}.button[disabled]:focus,.button[disabled]:hover,button[disabled]:focus,button[disabled]:hover,input[type='button'][disabled]:focus,input[type='button'][disabled]:hover,input[type='reset'][disabled]:focus,input[type='reset'][disabled]:hover,input[type='submit'][disabled]:focus,input[type='submit'][disabled]:hover{background-color:#0069d9;border-color:#0069d9}.button.button-outline,button.button-outline,input[type='button'].button-outline,input[type='reset'].button-outline,input[type='submit'].button-outline{background-color:transparent;color:#0069d9}.button.button-outline:focus,.button.button-outline:hover,button.button-outline:focus,button.button-outline:hover,input[type='button'].button-outline:focus,input[type='button'].button-outline:hover,input[type='reset'].button-outline:focus,input[type='reset'].button-outline:hover,input[type='submit'].button-outline:focus,input[type='submit'].button-outline:hover{background-color:transparent;border-color:#606c76;color:#606c76}.button.button-outline[disabled]:focus,.button.button-outline[disabled]:hover,button.button-outline[disabled]:focus,button.button-outline[disabled]:hover,input[type='button'].button-outline[disabled]:focus,input[type='button'].button-outline[disabled]:hover,input[type='reset'].button-outline[disabled]:focus,input[type='reset'].button-outline[disabled]:hover,input[type='submit'].button-outline[disabled]:focus,input[type='submit'].button-outline[disabled]:hover{border-color:inherit;color:#0069d9}.button.button-clear,button.button-clear,input[type='button'].button-clear,input[type='reset'].button-clear,input[type='submit'].button-clear{background-color:transparent;border-color:transparent;color:#0069d9}.button.button-clear:focus,.button.button-clear:hover,button.button-clear:focus,button.button-clear:hover,input[type='button'].button-clear:focus,input[type='button'].button-clear:hover,input[type='reset'].button-clear:focus,input[type='reset'].button-clear:hover,input[type='submit'].button-clear:focus,input[type='submit'].button-clear:hover{background-color:transparent;border-color:transparent;color:#606c76}.button.button-clear[disabled]:focus,.button.button-clear[disabled]:hover,button.button-clear[disabled]:focus,button.button-clear[disabled]:hover,input[type='button'].button-clear[disabled]:focus,input[type='button'].button-clear[disabled]:hover,input[type='reset'].button-clear[disabled]:focus,input[type='reset'].button-clear[disabled]:hover,input[type='submit'].button-clear[disabled]:focus,input[type='submit'].button-clear[disabled]:hover{color:#0069d9}code{background:#f4f5f6;border-radius:.4rem;font-size:86%;margin:0 .2rem;padding:.2rem .5rem;white-space:nowrap}pre{background:#f4f5f6;border-left:0.3rem solid #0069d9;overflow-y:hidden}pre>code{border-radius:0;display:block;padding:1rem 1.5rem;white-space:pre}hr{border:0;border-top:0.1rem solid #f4f5f6;margin:3.0rem 0}input[type='email'],input[type='number'],input[type='password'],input[type='search'],input[type='tel'],input[type='text'],input[type='url'],textarea,select{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:transparent;border:0.1rem solid #d1d1d1;border-radius:.4rem;box-shadow:none;box-sizing:inherit;height:3.8rem;padding:.6rem 1.0rem;width:100%}input[type='email']:focus,input[type='number']:focus,input[type='password']:focus,input[type='search']:focus,input[type='tel']:focus,input[type='text']:focus,input[type='url']:focus,textarea:focus,select:focus{border-color:#0069d9;outline:0}select{background:url('data:image/svg+xml;utf8, ') center right no-repeat;padding-right:3.0rem}select:focus{background-image:url('data:image/svg+xml;utf8, ')}textarea{min-height:6.5rem}label,legend{display:block;font-size:1.6rem;font-weight:700;margin-bottom:.5rem}fieldset{border-width:0;padding:0}input[type='checkbox'],input[type='radio']{display:inline}.label-inline{display:inline-block;font-weight:normal;margin-left:.5rem}.row{display:flex;flex-direction:column;padding:0;width:100%}.row.row-no-padding{padding:0}.row.row-no-padding>.column{padding:0}.row.row-wrap{flex-wrap:wrap}.row.row-top{align-items:flex-start}.row.row-bottom{align-items:flex-end}.row.row-center{align-items:center}.row.row-stretch{align-items:stretch}.row.row-baseline{align-items:baseline}.row .column{display:block;flex:1 1 auto;margin-left:0;max-width:100%;width:100%}.row .column.column-offset-10{margin-left:10%}.row .column.column-offset-20{margin-left:20%}.row .column.column-offset-25{margin-left:25%}.row .column.column-offset-33,.row .column.column-offset-34{margin-left:33.3333%}.row .column.column-offset-50{margin-left:50%}.row .column.column-offset-66,.row .column.column-offset-67{margin-left:66.6666%}.row .column.column-offset-75{margin-left:75%}.row .column.column-offset-80{margin-left:80%}.row .column.column-offset-90{margin-left:90%}.row .column.column-10{flex:0 0 10%;max-width:10%}.row .column.column-20{flex:0 0 20%;max-width:20%}.row .column.column-25{flex:0 0 25%;max-width:25%}.row .column.column-33,.row .column.column-34{flex:0 0 33.3333%;max-width:33.3333%}.row .column.column-40{flex:0 0 40%;max-width:40%}.row .column.column-50{flex:0 0 50%;max-width:50%}.row .column.column-60{flex:0 0 60%;max-width:60%}.row .column.column-66,.row .column.column-67{flex:0 0 66.6666%;max-width:66.6666%}.row .column.column-75{flex:0 0 75%;max-width:75%}.row .column.column-80{flex:0 0 80%;max-width:80%}.row .column.column-90{flex:0 0 90%;max-width:90%}.row .column .column-top{align-self:flex-start}.row .column .column-bottom{align-self:flex-end}.row .column .column-center{-ms-grid-row-align:center;align-self:center}@media (min-width: 40rem){.row{flex-direction:row;margin-left:-1.0rem;width:calc(100% + 2.0rem)}.row .column{margin-bottom:inherit;padding:0 1.0rem}}a{color:#0069d9;text-decoration:none}a:focus,a:hover{color:#606c76}dl,ol,ul{list-style:none;margin-top:0;padding-left:0}dl dl,dl ol,dl ul,ol dl,ol ol,ol ul,ul dl,ul ol,ul ul{font-size:90%;margin:1.5rem 0 1.5rem 3.0rem}ol{list-style:decimal inside}ul{list-style:circle inside}.button,button,dd,dt,li{margin-bottom:1.0rem}fieldset,input,select,textarea{margin-bottom:1.5rem}blockquote,dl,figure,form,ol,p,pre,table,ul{margin-bottom:2.5rem}table{border-spacing:0;width:100%}td,th{border-bottom:0.1rem solid #e1e1e1;padding:1.2rem 1.5rem;text-align:left}td:first-child,th:first-child{padding-left:0}td:last-child,th:last-child{padding-right:0}b,strong{font-weight:bold}p{margin-top:0}h1,h2,h3,h4,h5,h6{font-weight:300;letter-spacing:-.1rem;margin-bottom:2.0rem;margin-top:0}h1{font-size:4.6rem;line-height:1.2}h2{font-size:3.6rem;line-height:1.25}h3{font-size:2.8rem;line-height:1.3}h4{font-size:2.2rem;letter-spacing:-.08rem;line-height:1.35}h5{font-size:1.8rem;letter-spacing:-.05rem;line-height:1.5}h6{font-size:1.6rem;letter-spacing:0;line-height:1.4}img{max-width:100%}.clearfix:after{clear:both;content:' ';display:table}.float-left{float:left}.float-right{float:right}
10 |
11 | /* General style */
12 | h1{font-size: 3.6rem; line-height: 1.25}
13 | h2{font-size: 2.8rem; line-height: 1.3}
14 | h3{font-size: 2.2rem; letter-spacing: -.08rem; line-height: 1.35}
15 | h4{font-size: 1.8rem; letter-spacing: -.05rem; line-height: 1.5}
16 | h5{font-size: 1.6rem; letter-spacing: 0; line-height: 1.4}
17 | h6{font-size: 1.4rem; letter-spacing: 0; line-height: 1.2}
18 |
19 | .container{
20 | margin: 0 auto;
21 | max-width: 80.0rem;
22 | padding: 0 2.0rem;
23 | position: relative;
24 | width: 100%
25 | }
26 | select {
27 | width: auto;
28 | }
29 |
30 | /* Alerts and form errors */
31 | .alert {
32 | padding: 15px;
33 | margin-bottom: 20px;
34 | border: 1px solid transparent;
35 | border-radius: 4px;
36 | }
37 | .alert-info {
38 | color: #31708f;
39 | background-color: #d9edf7;
40 | border-color: #bce8f1;
41 | }
42 | .alert-warning {
43 | color: #8a6d3b;
44 | background-color: #fcf8e3;
45 | border-color: #faebcc;
46 | }
47 | .alert-danger {
48 | color: #a94442;
49 | background-color: #f2dede;
50 | border-color: #ebccd1;
51 | }
52 | .alert p {
53 | margin-bottom: 0;
54 | }
55 | .alert:empty {
56 | display: none;
57 | }
58 | .help-block {
59 | color: #a94442;
60 | display: block;
61 | margin: -1rem 0 2rem;
62 | }
63 |
64 | /* Phoenix promo and logo */
65 | .phx-hero {
66 | text-align: center;
67 | border-bottom: 1px solid #e3e3e3;
68 | background: #eee;
69 | border-radius: 6px;
70 | padding: 3em;
71 | margin-bottom: 3rem;
72 | font-weight: 200;
73 | font-size: 120%;
74 | }
75 | .phx-hero p {
76 | margin: 0;
77 | }
78 | .phx-logo {
79 | min-width: 300px;
80 | margin: 1rem;
81 | display: block;
82 | }
83 | .phx-logo img {
84 | width: auto;
85 | display: block;
86 | }
87 |
88 | /* Headers */
89 | header {
90 | width: 100%;
91 | background: #fdfdfd;
92 | border-bottom: 1px solid #eaeaea;
93 | margin-bottom: 2rem;
94 | }
95 | header section {
96 | align-items: center;
97 | display: flex;
98 | flex-direction: column;
99 | justify-content: space-between;
100 | }
101 | header section :first-child {
102 | order: 2;
103 | }
104 | header section :last-child {
105 | order: 1;
106 | }
107 | header nav ul,
108 | header nav li {
109 | margin: 0;
110 | padding: 0;
111 | display: block;
112 | text-align: right;
113 | white-space: nowrap;
114 | }
115 | header nav ul {
116 | margin: 1rem;
117 | margin-top: 0;
118 | }
119 | header nav a {
120 | display: block;
121 | }
122 |
123 | @media (min-width: 40.0rem) { /* Small devices (landscape phones, 576px and up) */
124 | header section {
125 | flex-direction: row;
126 | }
127 | header nav ul {
128 | margin: 1rem;
129 | }
130 | .phx-logo {
131 | flex-basis: 527px;
132 | margin: 2rem 1rem;
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/mix.lock:
--------------------------------------------------------------------------------
1 | %{
2 | "certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
3 | "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
4 | "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
5 | "cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"},
6 | "cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm", "79f954a7021b302186a950a32869dbc185523d99d3e44ce430cd1f3289f41ed4"},
7 | "db_connection": {:hex, :db_connection, "2.2.0", "e923e88887cd60f9891fd324ac5e0290954511d090553c415fbf54be4c57ee63", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "bdf196feedfa6b83071e808b2b086fb113f8a1c4c7761f6eff6fe4b96aba0086"},
8 | "decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"},
9 | "ecto": {:hex, :ecto, "3.3.1", "82ab74298065bf0c64ca299f6c6785e68ea5d6b980883ee80b044499df35aba1", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "e6c614dfe3bcff2d575ce16d815dbd43f4ee1844599a83de1eea81976a31c174"},
10 | "ecto_sql": {:hex, :ecto_sql, "3.3.2", "92804e0de69bb63e621273c3492252cb08a29475c05d40eeb6f41ad2d483cfd3", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b82d89d4e6a9f7f7f04783b07e8b0af968e0be2f01ee4b39047fe727c5c07471"},
11 | "ex_json_schema": {:hex, :ex_json_schema, "0.7.3", "3289bf2edf57eb1ae0d5af35bc6d6c37d7e6d935f72e0120c7f0704510956049", [:mix], [], "hexpm", "d5389c44e2804d4e6cada6f4a99d68f9d2bc0e38c2e0fd21383f1878425bd5a9"},
12 | "faker": {:hex, :faker, "0.13.0", "8abcb996f010ccd6c85588c89fc047f11134e04da019b70252f95431d721a3dc", [:mix], [], "hexpm", "b0016680cae6776e3d1caa34d70438acc09c11c003e80fd3d44f79ec7370be00"},
13 | "file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm", "b4cfa2d69c7f0b18fd06db222b2398abeef743a72504e6bd7df9c52f171b047f"},
14 | "floki": {:hex, :floki, "0.24.0", "81ec04f66cdc7d142fa8c9f402e4493a25bf9eb8bc39f971f58a8ea84a2e35d3", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "e8a5af967e4a1804bb034ea6e713338adf7bcb082608baae8783f2a41bf754ad"},
15 | "gettext": {:hex, :gettext, "0.17.4", "f13088e1ec10ce01665cf25f5ff779e7df3f2dc71b37084976cf89d1aa124d5c", [:mix], [], "hexpm", "3c75b5ea8288e2ee7ea503ff9e30dfe4d07ad3c054576a6e60040e79a801e14d"},
16 | "hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"},
17 | "html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"},
18 | "idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
19 | "jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"},
20 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
21 | "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"},
22 | "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
23 | "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
24 | "phoenix": {:hex, :phoenix, "1.4.16", "2cbbe0c81e6601567c44cc380c33aa42a1372ac1426e3de3d93ac448a7ec4308", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "856cc1a032fa53822737413cf51aa60e750525d7ece7d1c0576d90d7c0f05c24"},
25 | "phoenix_ecto": {:hex, :phoenix_ecto, "4.1.0", "a044d0756d0464c5a541b4a0bf4bcaf89bffcaf92468862408290682c73ae50d", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c5e666a341ff104d0399d8f0e4ff094559b2fde13a5985d4cb5023b2c2ac558b"},
26 | "phoenix_html": {:hex, :phoenix_html, "2.14.1", "7dabafadedb552db142aacbd1f11de1c0bbaa247f90c449ca549d5e30bbc66b4", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "536d5200ad37fecfe55b3241d90b7a8c3a2ca60cd012fc065f776324fa9ab0a9"},
27 | "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.1", "274a4b07c4adbdd7785d45a8b0bb57634d0b4f45b18d2c508b26c0344bd59b8f", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "41b4103a2fa282cfd747d377233baf213c648fdcc7928f432937676532490eee"},
28 | "phoenix_live_view": {:hex, :phoenix_live_view, "0.10.0", "5cb089e483ac9f33c1eeba51f24a395de5966d3a9f3f204b0bb9a5a938f2a695", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.4.16", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "236b1ba30062ee7a9786bf2306efecb01fc3bd86be199fbb66318c19f24a37e7"},
29 | "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm", "1f13f9f0f3e769a667a6b6828d29dec37497a082d195cc52dbef401a9b69bf38"},
30 | "phoenix_swagger": {:hex, :phoenix_swagger, "0.8.2", "cc49d9641d7e7c87766ba800110ff67d2fb55379f83982ee33d85d1e0b39d100", [:mix], [{:ex_json_schema, "~> 0.6", [hex: :ex_json_schema, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "e6d177764d75d388b199a863c5f7502ac8c202cd3fca61220807cbdcb31efef2"},
31 | "plug": {:hex, :plug, "1.10.0", "6508295cbeb4c654860845fb95260737e4a8838d34d115ad76cd487584e2fc4d", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "422a9727e667be1bf5ab1de03be6fa0ad67b775b2d84ed908f3264415ef29d4a"},
32 | "plug_cowboy": {:hex, :plug_cowboy, "2.1.2", "8b0addb5908c5238fac38e442e81b6fcd32788eaa03246b4d55d147c47c5805e", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "7d722581ce865a237e14da6d946f92704101740a256bd13ec91e63c0b122fc70"},
33 | "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"},
34 | "postgrex": {:hex, :postgrex, "0.15.3", "5806baa8a19a68c4d07c7a624ccdb9b57e89cbc573f1b98099e3741214746ae4", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4737ce62a31747b4c63c12b20c62307e51bb4fcd730ca0c32c280991e0606c90"},
35 | "pow": {:hex, :pow, "1.0.19", "e6295de629338661afdc52b3420f1fa37c191d246aef5d844161843fed6fe88b", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3.0 or ~> 1.4.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and <= 3.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 2.0.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "da77fab98e038b39c5360a77dc98e606cdd1446dabdd65a88991e8b23f67a356"},
36 | "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
37 | "scrivener": {:hex, :scrivener, "2.7.0", "fa94cdea21fad0649921d8066b1833d18d296217bfdf4a5389a2f45ee857b773", [:mix], [], "hexpm", "30da36a427f2519cf75993271fb7c5aad1759682a70f90d880a85c3d743d2c57"},
38 | "scrivener_ecto": {:hex, :scrivener_ecto, "2.2.0", "53d5f1ba28f35f17891cf526ee102f8f225b7024d1cdaf8984875467158c9c5e", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:scrivener, "~> 2.4", [hex: :scrivener, repo: "hexpm", optional: false]}], "hexpm", "3eadfc0a762db4ba8acceee3450404f6ce5e710e52ccf04aae69fca5afe0cd2f"},
39 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
40 | "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
41 | "timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "f354efb2400dd7a80fd9eb6c8419068c4f632da4ac47f3d8822d6e33f08bc852"},
42 | "tzdata": {:hex, :tzdata, "1.0.3", "73470ad29dde46e350c60a66e6b360d3b99d2d18b74c4c349dbebbc27a09a3eb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a6e1ee7003c4d04ecbd21dd3ec690d4c6662db5d3bbdd7262d53cdf5e7c746c1"},
43 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
44 | }
45 |
--------------------------------------------------------------------------------