Skip to content

Commit

Permalink
Refactor ElixirBoilerplateWeb to segment in each components in separa…
Browse files Browse the repository at this point in the history
…te modules
  • Loading branch information
gcauchon committed Sep 8, 2023
1 parent ff0e9b3 commit 19078f2
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 94 deletions.
2 changes: 0 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import 'phoenix_html';

const DELAY_IN_MILISECONDS = 200;

// Establish Phoenix Socket and LiveView configuration.
import {Socket} from 'phoenix';
import {LiveSocket} from 'phoenix_live_view';
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_boilerplate_web/api/version/controller.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule ElixirBoilerplateWeb.Api.Version.Controller do
use ElixirBoilerplateWeb, :controller
use ElixirBoilerplateWeb.Controller

@spec index(Plug.Conn.t(), map) :: Plug.Conn.t()
def index(conn, _) do
Expand Down
9 changes: 9 additions & 0 deletions lib/elixir_boilerplate_web/component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ElixirBoilerplateWeb.Component do
defmacro __using__(_opts) do
quote do
use Phoenix.LiveComponent

unquote(ElixirBoilerplateWeb.html_helpers())
end
end
end
15 changes: 15 additions & 0 deletions lib/elixir_boilerplate_web/controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule ElixirBoilerplateWeb.Controller do
defmacro __using__(_opts) do
quote do
use Phoenix.Controller,
namespace: ElixirBoilerplateWeb,
formats: [:html, :json],
layouts: [html: ElixirBoilerplateWeb.Layouts]

import Plug.Conn
import ElixirBoilerplate.Gettext

unquote(ElixirBoilerplateWeb.verified_routes())
end
end
end
86 changes: 2 additions & 84 deletions lib/elixir_boilerplate_web/elixir_boilerplate_web.ex
Original file line number Diff line number Diff line change
@@ -1,88 +1,13 @@
defmodule ElixirBoilerplateWeb do
@moduledoc """
The entrypoint for defining your web interface, such
as controllers, components, channels, and so on.
This can be used in your application as:
use ElixirBoilerplateWeb, :controller
use ElixirBoilerplateWeb, :html
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
below. Instead, define additional modules and import
those modules here.
"""

def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)

def router do
quote do
use Phoenix.Router, helpers: false

# Import common connection and controller functions to use in pipelines
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end
end

def channel do
quote do
use Phoenix.Channel
end
end

def controller do
quote do
use Phoenix.Controller,
namespace: ElixirBoilerplateWeb,
formats: [:html, :json],
layouts: [html: ElixirBoilerplateWeb.Layouts]

import Plug.Conn
import ElixirBoilerplate.Gettext

unquote(verified_routes())
end
end

def live_view do
quote do
use Phoenix.LiveView,
layout: {ElixirBoilerplateWeb.Layouts, :live}

unquote(html_helpers())
end
end

def live_component do
quote do
use Phoenix.LiveComponent

unquote(html_helpers())
end
end

def html do
quote do
use Phoenix.Component

# Import convenience functions from controllers
import Phoenix.Controller,
only: [get_csrf_token: 0, view_module: 1, view_template: 1]

# Include general helpers for rendering HTML
unquote(html_helpers())
end
end

defp html_helpers do
def html_helpers do
quote do
# HTML escaping functionality
import Phoenix.HTML

# Core UI components and translation
import ElixirBoilerplate.Gettext
import ElixirBoilerplateWeb.Components.Branding
import ElixirBoilerplateWeb.Components.Core

# Shortcut for generating JS commands
Expand All @@ -101,11 +26,4 @@ defmodule ElixirBoilerplateWeb do
statics: ElixirBoilerplateWeb.static_paths()
end
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
2 changes: 1 addition & 1 deletion lib/elixir_boilerplate_web/errors/error_html.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule ElixirBoilerplateWeb.Errors.ErrorHTML do
use ElixirBoilerplateWeb, :html
use ElixirBoilerplateWeb.HTML

# If you want to customize your error pages,
# uncomment the embed_templates/1 call below
Expand Down
4 changes: 3 additions & 1 deletion lib/elixir_boilerplate_web/home/html.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule ElixirBoilerplateWeb.Home.HTML do
use ElixirBoilerplateWeb, :html
use ElixirBoilerplateWeb.HTML

alias ElixirBoilerplateWeb.Components.Branding

embed_templates("templates/*")

Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_boilerplate_web/home/live.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule ElixirBoilerplateWeb.Home.Live do
use ElixirBoilerplateWeb, :live_view
use ElixirBoilerplateWeb.LiveView

def mount(_, _, socket) do
socket = assign(socket, :message, "Hello, world!")
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_boilerplate_web/home/templates/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="mx-auto max-w-2xl">
<div class="home">
<a target="_blank" href="https://github.com/mirego/elixir-boilerplate">
<.logo width={500} />
<Branding.logo width={500} />
</a>

<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="mx-auto max-w-2xl">
<div class="home">
<a target="_blank" href="https://github.com/mirego/elixir-boilerplate">
<.logo width={500} />
<Branding.logo width={500} />
</a>

<p>
Expand Down
14 changes: 14 additions & 0 deletions lib/elixir_boilerplate_web/html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ElixirBoilerplateWeb.HTML do
defmacro __using__(_opts) do
quote do
use Phoenix.Component

# Import convenience functions from controllers
import Phoenix.Controller,
only: [get_csrf_token: 0, view_module: 1, view_template: 1]

# Include general helpers for rendering HTML
unquote(ElixirBoilerplateWeb.html_helpers())
end
end
end
2 changes: 1 addition & 1 deletion lib/elixir_boilerplate_web/layouts/layouts.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule ElixirBoilerplateWeb.Layouts do
use ElixirBoilerplateWeb, :html
use ElixirBoilerplateWeb.HTML

embed_templates("templates/*")

Expand Down
9 changes: 9 additions & 0 deletions lib/elixir_boilerplate_web/live_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ElixirBoilerplateWeb.LiveView do
defmacro __using__(_opts) do
quote do
use Phoenix.LiveView, layout: {ElixirBoilerplateWeb.Layouts, :live}

unquote(ElixirBoilerplateWeb.html_helpers())
end
end
end
3 changes: 3 additions & 0 deletions lib/elixir_boilerplate_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ElixirBoilerplateWeb.Router do
use Phoenix.Router, helpers: false

# Import common connection and controller functions to use in pipelines
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router

pipeline :browser do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule ElixirBoilerplate.Mixfile do
{:excoveralls, "~> 0.16", only: :test},

# Dialyzer
{:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false}
]
end

Expand Down

0 comments on commit 19078f2

Please sign in to comment.