Skip to content

Commit

Permalink
chore: redirect all docs to hex
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Sep 5, 2024
1 parent 0d70f37 commit aff6a7b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 64 deletions.
138 changes: 76 additions & 62 deletions lib/ash_hq_web/redirect_to_hex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,96 @@ defmodule AshHqWeb.RedirectToHex do
@moduledoc "Sets the platform being used with liveview"
import Phoenix.LiveView

Check warning on line 3 in lib/ash_hq_web/redirect_to_hex.ex

View workflow job for this annotation

GitHub Actions / OTP 26.0.2 / Elixir 1.15.4 / Ash

unused import Phoenix.LiveView
require Ash.Query
@behaviour Plug

def on_mount(:default, %{"dsl_target" => dsl_target}, _session, socket) do
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
def init(opts), do: opts

AshHq.Docs.Module
|> Ash.Query.filter(name == ^dsl_target or sanitized_name == ^dsl_target)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
{:halt, redirect(socket, external: AshHqWeb.DocRoutes.doc_link(module))}
def call(conn, _) do
case conn.params do
%{"dsl_target" => dsl_target} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)

_ ->
{:cont, socket}
end
end
AshHq.Docs.Module
|> Ash.Query.filter(name == ^dsl_target or sanitized_name == ^dsl_target)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))

def on_mount(
:default,
%{"library" => library, "version" => version, "mix_task" => mix_task},
session,
socket
) do
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.MixTask)

AshHq.Docs.MixTask
|> Ash.Query.filter(name == ^mix_task or sanitized_name == ^mix_task)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
{:halt, redirect(socket, external: AshHqWeb.DocRoutes.doc_link(module))}
_ ->
Phoenix.Controller.redirect(conn, to: "/")
end

_ ->
on_mount(
:default,
%{"module" => mix_task, "library" => library, "version" => version},
session,
socket
)
end
end
%{"mix_task" => mix_task} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.MixTask)

def on_mount(:default, %{"module" => module}, _session, socket) do
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
AshHq.Docs.MixTask
|> Ash.Query.filter(name == ^mix_task or sanitized_name == ^mix_task)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))

AshHq.Docs.Module
|> Ash.Query.filter(name == ^module or sanitized_name == ^module)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
{:halt, redirect(socket, external: AshHqWeb.DocRoutes.doc_link(module))}
_ ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)

_ ->
{:cont, socket}
end
end
AshHq.Docs.Module
|> Ash.Query.filter(name == ^mix_task or sanitized_name == ^mix_task)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))

_ ->
Phoenix.Controller.redirect(conn, to: "/")
end
end

%{"module" => module} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)

AshHq.Docs.Module
|> Ash.Query.filter(name == ^module or sanitized_name == ^module)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))

_ ->
Phoenix.Controller.redirect(conn, to: "/")
end

%{"extension" => module} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)

AshHq.Docs.Module
|> Ash.Query.filter(name == ^module or sanitized_name == ^module)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))

def on_mount(:default, %{"extension" => module}, _session, socket) do
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
_ ->
Phoenix.Controller.redirect(conn, to: "/")
end

AshHq.Docs.Module
|> Ash.Query.filter(name == ^module or sanitized_name == ^module)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
{:halt, redirect(socket, external: AshHqWeb.DocRoutes.doc_link(module))}
%{"guide" => path, "library" => library} ->
redirect_to_hex(conn, "https://hexdocs.pm/#{library}/#{List.last(path)}.html")

_ ->
{:cont, socket}
conn
end
end

def on_mount(:default, _params, _session, socket) do
{:cont, socket}
defp redirect_to_hex(conn, to) do
conn
|> Plug.Conn.put_status(302)
|> Phoenix.Controller.redirect(external: to)
|> Plug.Conn.halt()
end
end
4 changes: 2 additions & 2 deletions lib/ash_hq_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule AshHqWeb.Router do
plug(:put_root_layout, {AshHqWeb.LayoutView, :root})
plug(:protect_from_forgery)
plug(AshHqWeb.SessionPlug)
plug(AshHqWeb.RedirectToHex)
end

pipeline :api do
Expand All @@ -26,8 +27,7 @@ defmodule AshHqWeb.Router do
live_session :main,
on_mount: [
{AshHqWeb.LiveUserAuth, :live_user_optional},
{AshHqWeb.InitAssigns, :default},
{AshHqWeb.RedirectToHex, :default}
{AshHqWeb.InitAssigns, :default}
],
root_layout: {AshHqWeb.LayoutView, :root} do
live("/", AppViewLive, :home)
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule AshHq.MixProject do
{:ash_blog, github: "ash-project/ash_blog"},
{:ash_csv, "~> 0.9"},
{:ash_oban, "~> 0.2"},
{:earmark, "== 1.5.0-pre1"},
# Jobs
{:oban, "~> 2.16"},
{:flame, "~> 0.4.2"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"decorator": {:hex, :decorator, "1.4.0", "a57ac32c823ea7e4e67f5af56412d12b33274661bb7640ec7fc882f8d23ac419", [:mix], [], "hexpm", "0a07cedd9083da875c7418dea95b78361197cf2bf3211d743f6f7ce39656597f"},
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"},
"earmark": {:hex, :earmark, "1.5.0-pre1", "e04aca73692bc3cda3429d6df99c8dae2bf76411e5e76d006a4bc04ac81ef1c1", [:mix], [{:earmark_parser, "~> 1.4.21", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "26ec0473ad2ef995b9672f89309a7a4952887f69b78cfc7af14e320bc6546bfa"},
"earmark_parser": {:hex, :earmark_parser, "1.4.41", "ab34711c9dc6212dda44fcd20ecb87ac3f3fce6f0ca2f28d4a00e4154f8cd599", [:mix], [], "hexpm", "a81a04c7e34b6617c2792e291b5a2e57ab316365c2644ddc553bb9ed863ebefa"},
"ecto": {:hex, :ecto, "3.12.2", "bae2094f038e9664ce5f089e5f3b6132a535d8b018bd280a485c2f33df5c0ce1", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "492e67c70f3a71c6afe80d946d3ced52ecc57c53c9829791bfff1830ff5a1f0c"},
"ecto_psql_extras": {:hex, :ecto_psql_extras, "0.8.1", "cdfee5cb21d51af37d14233e4aa3ef38189bf42c499d6a8160a943c990cc74e2", [:mix], [{:ecto_sql, "~> 3.7", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, "> 0.16.0 and < 0.20.0", [hex: :postgrex, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1 or ~> 4.0.0", [hex: :table_rex, repo: "hexpm", optional: false]}], "hexpm", "c0a1df8181c87a0419afa881452329a9dfd2c1fb2a737bf147f862dfa44a1af0"},
Expand Down

0 comments on commit aff6a7b

Please sign in to comment.