Skip to content

Commit 19078f2

Browse files
committed
Refactor ElixirBoilerplateWeb to segment in each components in separate modules
1 parent ff0e9b3 commit 19078f2

File tree

15 files changed

+62
-94
lines changed

15 files changed

+62
-94
lines changed

assets/js/app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
22
import 'phoenix_html';
33

4-
const DELAY_IN_MILISECONDS = 200;
5-
64
// Establish Phoenix Socket and LiveView configuration.
75
import {Socket} from 'phoenix';
86
import {LiveSocket} from 'phoenix_live_view';

lib/elixir_boilerplate_web/api/version/controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirBoilerplateWeb.Api.Version.Controller do
2-
use ElixirBoilerplateWeb, :controller
2+
use ElixirBoilerplateWeb.Controller
33

44
@spec index(Plug.Conn.t(), map) :: Plug.Conn.t()
55
def index(conn, _) do
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule ElixirBoilerplateWeb.Component do
2+
defmacro __using__(_opts) do
3+
quote do
4+
use Phoenix.LiveComponent
5+
6+
unquote(ElixirBoilerplateWeb.html_helpers())
7+
end
8+
end
9+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule ElixirBoilerplateWeb.Controller do
2+
defmacro __using__(_opts) do
3+
quote do
4+
use Phoenix.Controller,
5+
namespace: ElixirBoilerplateWeb,
6+
formats: [:html, :json],
7+
layouts: [html: ElixirBoilerplateWeb.Layouts]
8+
9+
import Plug.Conn
10+
import ElixirBoilerplate.Gettext
11+
12+
unquote(ElixirBoilerplateWeb.verified_routes())
13+
end
14+
end
15+
end
Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,13 @@
11
defmodule ElixirBoilerplateWeb do
2-
@moduledoc """
3-
The entrypoint for defining your web interface, such
4-
as controllers, components, channels, and so on.
5-
This can be used in your application as:
6-
use ElixirBoilerplateWeb, :controller
7-
use ElixirBoilerplateWeb, :html
8-
The definitions below will be executed for every controller,
9-
component, etc, so keep them short and clean, focused
10-
on imports, uses and aliases.
11-
Do NOT define functions inside the quoted expressions
12-
below. Instead, define additional modules and import
13-
those modules here.
14-
"""
15-
162
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
173

18-
def router do
19-
quote do
20-
use Phoenix.Router, helpers: false
21-
22-
# Import common connection and controller functions to use in pipelines
23-
import Plug.Conn
24-
import Phoenix.Controller
25-
import Phoenix.LiveView.Router
26-
end
27-
end
28-
29-
def channel do
30-
quote do
31-
use Phoenix.Channel
32-
end
33-
end
34-
35-
def controller do
36-
quote do
37-
use Phoenix.Controller,
38-
namespace: ElixirBoilerplateWeb,
39-
formats: [:html, :json],
40-
layouts: [html: ElixirBoilerplateWeb.Layouts]
41-
42-
import Plug.Conn
43-
import ElixirBoilerplate.Gettext
44-
45-
unquote(verified_routes())
46-
end
47-
end
48-
49-
def live_view do
50-
quote do
51-
use Phoenix.LiveView,
52-
layout: {ElixirBoilerplateWeb.Layouts, :live}
53-
54-
unquote(html_helpers())
55-
end
56-
end
57-
58-
def live_component do
59-
quote do
60-
use Phoenix.LiveComponent
61-
62-
unquote(html_helpers())
63-
end
64-
end
65-
66-
def html do
67-
quote do
68-
use Phoenix.Component
69-
70-
# Import convenience functions from controllers
71-
import Phoenix.Controller,
72-
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
73-
74-
# Include general helpers for rendering HTML
75-
unquote(html_helpers())
76-
end
77-
end
78-
79-
defp html_helpers do
4+
def html_helpers do
805
quote do
816
# HTML escaping functionality
827
import Phoenix.HTML
8+
839
# Core UI components and translation
8410
import ElixirBoilerplate.Gettext
85-
import ElixirBoilerplateWeb.Components.Branding
8611
import ElixirBoilerplateWeb.Components.Core
8712

8813
# Shortcut for generating JS commands
@@ -101,11 +26,4 @@ defmodule ElixirBoilerplateWeb do
10126
statics: ElixirBoilerplateWeb.static_paths()
10227
end
10328
end
104-
105-
@doc """
106-
When used, dispatch to the appropriate controller/view/etc.
107-
"""
108-
defmacro __using__(which) when is_atom(which) do
109-
apply(__MODULE__, which, [])
110-
end
11129
end

lib/elixir_boilerplate_web/errors/error_html.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirBoilerplateWeb.Errors.ErrorHTML do
2-
use ElixirBoilerplateWeb, :html
2+
use ElixirBoilerplateWeb.HTML
33

44
# If you want to customize your error pages,
55
# uncomment the embed_templates/1 call below

lib/elixir_boilerplate_web/home/html.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
defmodule ElixirBoilerplateWeb.Home.HTML do
2-
use ElixirBoilerplateWeb, :html
2+
use ElixirBoilerplateWeb.HTML
3+
4+
alias ElixirBoilerplateWeb.Components.Branding
35

46
embed_templates("templates/*")
57

lib/elixir_boilerplate_web/home/live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirBoilerplateWeb.Home.Live do
2-
use ElixirBoilerplateWeb, :live_view
2+
use ElixirBoilerplateWeb.LiveView
33

44
def mount(_, _, socket) do
55
socket = assign(socket, :message, "Hello, world!")

lib/elixir_boilerplate_web/home/templates/index.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="mx-auto max-w-2xl">
55
<div class="home">
66
<a target="_blank" href="https://github.com/mirego/elixir-boilerplate">
7-
<.logo width={500} />
7+
<Branding.logo width={500} />
88
</a>
99

1010
<p>

lib/elixir_boilerplate_web/home/templates/index_live.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="mx-auto max-w-2xl">
55
<div class="home">
66
<a target="_blank" href="https://github.com/mirego/elixir-boilerplate">
7-
<.logo width={500} />
7+
<Branding.logo width={500} />
88
</a>
99

1010
<p>

lib/elixir_boilerplate_web/html.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule ElixirBoilerplateWeb.HTML do
2+
defmacro __using__(_opts) do
3+
quote do
4+
use Phoenix.Component
5+
6+
# Import convenience functions from controllers
7+
import Phoenix.Controller,
8+
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
9+
10+
# Include general helpers for rendering HTML
11+
unquote(ElixirBoilerplateWeb.html_helpers())
12+
end
13+
end
14+
end

lib/elixir_boilerplate_web/layouts/layouts.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirBoilerplateWeb.Layouts do
2-
use ElixirBoilerplateWeb, :html
2+
use ElixirBoilerplateWeb.HTML
33

44
embed_templates("templates/*")
55

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule ElixirBoilerplateWeb.LiveView do
2+
defmacro __using__(_opts) do
3+
quote do
4+
use Phoenix.LiveView, layout: {ElixirBoilerplateWeb.Layouts, :live}
5+
6+
unquote(ElixirBoilerplateWeb.html_helpers())
7+
end
8+
end
9+
end

lib/elixir_boilerplate_web/router.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
defmodule ElixirBoilerplateWeb.Router do
22
use Phoenix.Router, helpers: false
33

4+
# Import common connection and controller functions to use in pipelines
5+
import Plug.Conn
6+
import Phoenix.Controller
47
import Phoenix.LiveView.Router
58

69
pipeline :browser do

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ defmodule ElixirBoilerplate.Mixfile do
104104
{:excoveralls, "~> 0.16", only: :test},
105105

106106
# Dialyzer
107-
{:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false},
107+
{:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false}
108108
]
109109
end
110110

0 commit comments

Comments
 (0)