de-tangling more view code

main
Ryan Rix 2020-08-09 21:33:32 -07:00
parent 401d049b02
commit b0188d935a
9 changed files with 104 additions and 94 deletions

View File

@ -1,44 +0,0 @@
defmodule LipuKasiWeb.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""
use Phoenix.HTML
@doc """
Generates tag for inlined form input errors.
"""
def error_tag(form, field) do
Enum.map(Keyword.get_values(form.errors, field), fn error ->
content_tag(:span, translate_error(error), class: "help-block")
end)
end
@doc """
Translates an error message using gettext.
"""
def translate_error({msg, opts}) do
# When using gettext, we typically pass the strings we want
# to translate as a static argument:
#
# # Translate "is invalid" in the "errors" domain
# dgettext("errors", "is invalid")
#
# # Translate the number of files with plural rules
# dngettext("errors", "1 file", "%{count} files", count)
#
# Because the error messages we show in our forms and APIs
# are defined inside Ecto, we need to translate them dynamically.
# This requires us to call the Gettext module passing our gettext
# backend as first argument.
#
# Note we use the "errors" domain, which means translations
# should be written to the errors.po file. The :count option is
# set by Ecto and indicates we should also apply plural rules.
if count = opts[:count] do
Gettext.dngettext(LipuKasiWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(LipuKasiWeb.Gettext, "errors", msg, opts)
end
end
end

View File

@ -1,16 +0,0 @@
defmodule LipuKasiWeb.ErrorView do
use LipuKasiWeb, :view
# If you want to customize a particular status code
# for a certain format, you may uncomment below.
# def render("500.html", _assigns) do
# "Internal Server Error"
# end
# By default, Phoenix returns the status message from
# the template name. For example, "404.html" becomes
# "Not Found".
def template_not_found(template, _assigns) do
Phoenix.Controller.status_message_from_template(template)
end
end

View File

@ -1,3 +0,0 @@
defmodule LipuKasiWeb.LayoutView do
use LipuKasiWeb, :view
end

View File

@ -34,6 +34,12 @@ defmodule LipuKasiWeb.PageView do
end
#+end_src
#+begin_src elixir :tangle test/lipu_kasi_web/views/page_view_test.exs
defmodule LipuKasiWeb.PageViewTest do
use LipuKasiWeb.ConnCase, async: true
end
#+end_src
** Template
This is the default template. Someday I'll have something more interesting to say!
@ -90,3 +96,4 @@ defmodule LipuKasiWeb.PageControllerTest do
end
end
#+end_src

View File

@ -51,9 +51,9 @@ Files which are provided from tangles will need to be managed here:
# Controllers, Views, Templates
/lib/lipu_kasi_web/controllers/
/lib/lipu_kasi_web/views/page_view.ex
/lib/lipu_kasi_web/templates/page/
/lib/lipu_kasi_web/templates/layout/*.html.eex
/lib/lipu_kasi_web/views/
/test/lipu_kasi_web/views/
/lib/lipu_kasi_web/templates/
/test/support/
@ -331,3 +331,97 @@ import_config "prod.secret.exs"
#+end_src
When deployed, we'll probably do SSL termination in =nginx=, it's not so hard.
* Error Views and other base views
These are default, I'll change them some day.
#+begin_src elixir :tangle lib/lipu_kasi_web/views/error_view.ex
defmodule LipuKasiWeb.ErrorView do
use LipuKasiWeb, :view
# If you want to customize a particular status code
# for a certain format, you may uncomment below.
# def render("500.html", _assigns) do
# "Internal Server Error"
# end
# By default, Phoenix returns the status message from
# the template name. For example, "404.html" becomes
# "Not Found".
def template_not_found(template, _assigns) do
Phoenix.Controller.status_message_from_template(template)
end
end
#+end_src
#+begin_src elixir :tangle lib/lipu_kasi_web/views/error_helpers.ex
defmodule LipuKasiWeb.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""
use Phoenix.HTML
@doc """
Generates tag for inlined form input errors.
"""
def error_tag(form, field) do
Enum.map(Keyword.get_values(form.errors, field), fn error ->
content_tag(:span, translate_error(error), class: "help-block")
end)
end
@doc """
Translates an error message using gettext.
"""
def translate_error({msg, opts}) do
# When using gettext, we typically pass the strings we want
# to translate as a static argument:
#
# # Translate "is invalid" in the "errors" domain
# dgettext("errors", "is invalid")
#
# # Translate the number of files with plural rules
# dngettext("errors", "1 file", "%{count} files", count)
#
# Because the error messages we show in our forms and APIs
# are defined inside Ecto, we need to translate them dynamically.
# This requires us to call the Gettext module passing our gettext
# backend as first argument.
#
# Note we use the "errors" domain, which means translations
# should be written to the errors.po file. The :count option is
# set by Ecto and indicates we should also apply plural rules.
if count = opts[:count] do
Gettext.dngettext(LipuKasiWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(LipuKasiWeb.Gettext, "errors", msg, opts)
end
end
end
#+end_src
#+begin_src elixir :tangle test/lipu_kasi_web/views/error_view_test.exs
defmodule LipuKasiWeb.ErrorViewTest do
use LipuKasiWeb.ConnCase, async: true
# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View
test "renders 404.html" do
assert render_to_string(LipuKasiWeb.ErrorView, "404.html", []) == "Not Found"
end
test "renders 500.html" do
assert render_to_string(LipuKasiWeb.ErrorView, "500.html", []) == "Internal Server Error"
end
end
#+end_src
IDK why I need to hold on to this one.
#+begin_src elixir :tangle lib/lipu_kasi_web/views/layout_view.ex :mkdirp yes
defmodule LipuKasiWeb.LayoutView do
use LipuKasiWeb, :view
end
#+end_src

View File

@ -1,8 +0,0 @@
defmodule LipuKasiWeb.PageControllerTest do
use LipuKasiWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end
end

View File

@ -1,14 +0,0 @@
defmodule LipuKasiWeb.ErrorViewTest do
use LipuKasiWeb.ConnCase, async: true
# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View
test "renders 404.html" do
assert render_to_string(LipuKasiWeb.ErrorView, "404.html", []) == "Not Found"
end
test "renders 500.html" do
assert render_to_string(LipuKasiWeb.ErrorView, "500.html", []) == "Internal Server Error"
end
end

View File

@ -1,3 +0,0 @@
defmodule LipuKasiWeb.LayoutViewTest do
use LipuKasiWeb.ConnCase, async: true
end

View File

@ -1,3 +0,0 @@
defmodule LipuKasiWeb.PageViewTest do
use LipuKasiWeb.ConnCase, async: true
end