Skip to content

Commit 060ae5c

Browse files
authored
feat(elixir/phoenix): update phoenix to 1.7 (#8004)
1 parent 4a9476d commit 060ae5c

File tree

14 files changed

+163
-59
lines changed

14 files changed

+163
-59
lines changed

frameworks/Elixir/phoenix/lib/hello_web.ex

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,104 @@
11
defmodule HelloWeb do
22
@moduledoc """
3-
A module that keeps using definitions for controllers,
4-
views and so on.
3+
The entrypoint for defining your web interface, such
4+
as controllers, views, channels and so on.
55
66
This can be used in your application as:
77
88
use HelloWeb, :controller
9-
use HelloWeb, :view
9+
use HelloWeb, :html
1010
11-
The definitions below will be executed for every view,
12-
controller, etc, so keep them short and clean, focused
11+
The definitions below will be executed for every controller,
12+
component, etc, so keep them short and clean, focused
1313
on imports, uses and aliases.
1414
1515
Do NOT define functions inside the quoted expressions
16-
below. Instead, define any helper function in modules
17-
and import those modules here.
16+
below. Instead, define additional modules and import
17+
those modules here.
1818
"""
1919

20+
def static_paths,
21+
do: ~w(assets favicon.svg apple-touch-icon.png robots.txt font-files mask-icon.svg)
22+
2023
def controller do
2124
quote do
22-
use Phoenix.Controller, namespace: HelloWeb, log: false
25+
use Phoenix.Controller,
26+
namespace: HelloWeb,
27+
formats: [:html, :json],
28+
layouts: [html: HelloWeb.Layouts],
29+
log: false
30+
31+
import Plug.Conn
32+
import HelloWeb.Gettext
33+
34+
unquote(verified_routes())
35+
end
36+
end
37+
38+
def component do
39+
quote do
40+
use Phoenix.Component
2341

24-
# Alias the data repository and import query/model functions
25-
alias Hello.Repo
26-
import Ecto
27-
import Ecto.Query
42+
import HelloWeb.Gettext
2843

29-
# Import URL helpers from the router
30-
import HelloWeb.Router.Helpers
44+
# Routes generation with the ~p sigil
45+
unquote(verified_routes())
3146
end
3247
end
3348

34-
def view do
49+
def html do
3550
quote do
36-
use Phoenix.View,
37-
root: "lib/hello_web/templates",
38-
namespace: HelloWeb
51+
use Phoenix.Component
52+
53+
# Import convenience functions from controllers
54+
import Phoenix.Controller,
55+
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
3956

40-
alias HelloWeb.Router.Helpers, as: Routes
57+
# Include general helpers for rendering HTML
58+
unquote(html_helpers())
59+
end
60+
end
61+
62+
defp html_helpers do
63+
quote do
64+
# Use all HTML functionality (forms, tags, etc)
65+
use Phoenix.HTML
66+
# Core UI Components and translation
67+
import HelloWeb.Gettext
68+
69+
# Routes generation with the ~p sigil
70+
unquote(verified_routes())
71+
end
72+
end
73+
74+
def verified_routes do
75+
quote do
76+
use Phoenix.VerifiedRoutes,
77+
endpoint: HelloWeb.Endpoint,
78+
router: HelloWeb.Router,
79+
statics: HelloWeb.static_paths()
4180
end
4281
end
4382

4483
def router do
4584
quote do
46-
use Phoenix.Router
85+
use Phoenix.Router, helpers: false
86+
87+
# Import common connection and controller functions to use in pipelines
88+
import Plug.Conn
89+
import Phoenix.Controller
4790
end
4891
end
4992

5093
def channel do
5194
quote do
5295
use Phoenix.Channel
53-
# Alias the data repository and import query/model functions
54-
alias Hello.Repo
55-
import Ecto
56-
import Ecto.Query
5796
end
5897
end
5998

6099
@doc """
61-
When used, dispatch to the appropriate controller/view/etc.
62-
"""
100+
When used, dispatch to the appropriate controller/view/etc.
101+
"""
63102
defmacro __using__(which) when is_atom(which) do
64103
apply(__MODULE__, which, [])
65104
end

frameworks/Elixir/phoenix/lib/hello_web/views/error_view.ex renamed to frameworks/Elixir/phoenix/lib/hello_web/controllers/error_html.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule HelloWeb.ErrorView do
2-
use HelloWeb, :view
1+
defmodule HelloWeb.ErrorHTML do
2+
use HelloWeb, :html
33

44
def render("404.html", _assigns) do
55
"Page not found - 404"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule HelloWeb.ErrorJSON do
2+
@moduledoc """
3+
JSON error pages
4+
"""
5+
6+
# If you want to customize a particular status code,
7+
# you may add your own clauses, such as:
8+
#
9+
# def render("500.json", _assigns) do
10+
# %{errors: %{detail: "Internal Server Error"}}
11+
# end
12+
13+
# By default, Phoenix returns the status message from
14+
# the template name. For example, "404.json" becomes
15+
# "Not Found".
16+
def render(template, _assigns) do
17+
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
18+
end
19+
end

frameworks/Elixir/phoenix/lib/hello_web/controllers/page_controller.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule HelloWeb.PageController do
33

44
use HelloWeb, :controller
55

6+
alias Hello.Repo
67
alias Hello.Cache
78

89
@json "application/json"
@@ -59,7 +60,7 @@ defmodule HelloWeb.PageController do
5960

6061
fortunes = [additional_fortune | Repo.all(Fortune)]
6162

62-
render(conn, "fortunes.html",
63+
render(conn, :fortunes,
6364
fortunes: Enum.sort(fortunes, fn f1, f2 -> f1.message < f2.message end)
6465
)
6566
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule HelloWeb.PageHTML do
2+
use HelloWeb, :html
3+
4+
@moduledoc """
5+
Standard Page HTML Helper
6+
"""
7+
8+
embed_templates "page_html/*"
9+
end
File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule HelloWeb.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 HelloWeb.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: :hello
24+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule HelloWeb.Layouts do
2+
use HelloWeb, :html
3+
4+
@moduledoc false
5+
6+
embed_templates "layouts/*"
7+
end
File renamed without changes.
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
defmodule HelloWeb.Router do
22
use HelloWeb, :router
33

4+
pipeline :browser do
5+
plug :accepts, ~w(html json)
6+
end
7+
48
scope "/", HelloWeb do
5-
get("/json", PageController, :_json)
6-
get("/db", PageController, :db)
7-
get("/queries", PageController, :queries)
8-
get("/fortunes", PageController, :fortunes)
9-
get("/updates", PageController, :updates)
10-
get("/plaintext", PageController, :plaintext)
11-
get("/cached-queries", PageController, :cached)
12-
get("/", PageController, :index)
9+
pipe_through [:browser]
10+
11+
get "/json", PageController, :_json
12+
get "/db", PageController, :db
13+
get "/queries", PageController, :queries
14+
get "/fortunes", PageController, :fortunes
15+
get "/updates", PageController, :updates
16+
get "/plaintext", PageController, :plaintext
17+
get "/cached-queries", PageController, :cached
18+
get "/", PageController, :index
1319
end
1420
end

0 commit comments

Comments
 (0)