From 0a665d4a61d6a59a280665df56a3a381fb61ff76 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 16 Oct 2022 19:34:20 -0500 Subject: [PATCH] improvement: use new flow features for search --- .../search/preparations/load_search_data.ex | 20 ++-- lib/ash_hq/docs/flows/search/search.ex | 104 +++++------------- .../docs/flows/search/steps/build_results.ex | 5 +- .../docs/flows/search/steps/should_search.ex | 13 +++ lib/ash_hq_web/schema.ex | 6 - mix.lock | 11 +- test/tailwind_test.exs | 5 - 7 files changed, 59 insertions(+), 105 deletions(-) create mode 100644 lib/ash_hq/docs/flows/search/steps/should_search.ex delete mode 100644 test/tailwind_test.exs diff --git a/lib/ash_hq/docs/extensions/search/preparations/load_search_data.ex b/lib/ash_hq/docs/extensions/search/preparations/load_search_data.ex index 5b034ae..f2b53c1 100644 --- a/lib/ash_hq/docs/extensions/search/preparations/load_search_data.ex +++ b/lib/ash_hq/docs/extensions/search/preparations/load_search_data.ex @@ -8,15 +8,15 @@ defmodule AshHq.Extensions.Search.Preparations.LoadSearchData do query_string = Ash.Query.get_argument(query, :query) to_load = AshHq.Docs.Extensions.Search.load_for_search(query.resource) - if query_string do - query - |> Ash.Query.load(search_headline: [query: query_string]) - |> Ash.Query.load(match_rank: [query: query_string]) - |> Ash.Query.load(name_matches: [query: query_string, similarity: 0.7]) - |> Ash.Query.load(to_load) - |> Ash.Query.sort(match_rank: {:asc, %{query: query_string}}) - else - query - end + query.resource + |> AshHq.Docs.Extensions.RenderMarkdown.render_attributes() + |> Enum.reduce(query, fn {source, target}, query -> + Ash.Query.deselect(query, [source, target]) + end) + |> Ash.Query.load(search_headline: [query: query_string]) + |> Ash.Query.load(match_rank: [query: query_string]) + |> Ash.Query.load(name_matches: [query: query_string, similarity: 0.7]) + |> Ash.Query.load(to_load) + |> Ash.Query.sort(match_rank: {:asc, %{query: query_string}}) end end diff --git a/lib/ash_hq/docs/flows/search/search.ex b/lib/ash_hq/docs/flows/search/search.ex index 03c963f..4fc9d8c 100644 --- a/lib/ash_hq/docs/flows/search/search.ex +++ b/lib/ash_hq/docs/flows/search/search.ex @@ -1,6 +1,17 @@ defmodule AshHq.Docs.Search do @moduledoc false + @search [ + AshHq.Docs.Option, + AshHq.Docs.MixTask, + AshHq.Docs.Module, + AshHq.Docs.Function, + AshHq.Docs.Extension, + AshHq.Docs.LibraryVersion, + AshHq.Docs.Guide, + AshHq.Docs.Dsl + ] + use Ash.Flow, otp_app: :ash_hq flow do @@ -25,88 +36,29 @@ defmodule AshHq.Docs.Search do end steps do - custom :options, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.Option - } - end + map :search_results, @search do + custom :should_search, AshHq.Docs.Search.Steps.ShouldSearch do + input %{ + resource: element(:search_results), + types: arg(:types) + } + end - custom :dsls, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.Dsl - } - end + branch :maybe_search, result(:should_search) do + output :search - custom :guides, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.Guide - } - end - - custom :library_versions, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.LibraryVersion - } - end - - custom :extensions, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.Extension - } - end - - custom :functions, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.Function - } - end - - custom :modules, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.Module - } - end - - custom :mix_tasks, AshHq.Docs.Search.Steps.SearchResource do - input %{ - query: arg(:query), - library_versions: arg(:library_versions), - types: arg(:types), - resource: AshHq.Docs.MixTask - } + read :search, element(:search_results), :search do + input %{ + library_versions: arg(:library_versions), + query: arg(:query) + } + end + end end custom :build_results, AshHq.Docs.Search.Steps.BuildResults do input %{ - dsls: result(:dsls), - options: result(:options), - guides: result(:guides), - library_versions: result(:library_versions), - extensions: result(:extensions), - functions: result(:functions), - modules: result(:modules), - mix_tasks: result(:mix_tasks) + results: result(:search_results) } end end diff --git a/lib/ash_hq/docs/flows/search/steps/build_results.ex b/lib/ash_hq/docs/flows/search/steps/build_results.ex index 80239fc..f5985f5 100644 --- a/lib/ash_hq/docs/flows/search/steps/build_results.ex +++ b/lib/ash_hq/docs/flows/search/steps/build_results.ex @@ -5,10 +5,9 @@ defmodule AshHq.Docs.Search.Steps.BuildResults do use Ash.Flow.Step def run(input, _opts, _context) do - # search_results = {:ok, - input - |> Map.values() + input[:results] + |> Kernel.||([]) |> List.flatten() |> Enum.sort_by( # false comes first, and we want all things where the name matches to go first diff --git a/lib/ash_hq/docs/flows/search/steps/should_search.ex b/lib/ash_hq/docs/flows/search/steps/should_search.ex new file mode 100644 index 0000000..0aae008 --- /dev/null +++ b/lib/ash_hq/docs/flows/search/steps/should_search.ex @@ -0,0 +1,13 @@ +defmodule AshHq.Docs.Search.Steps.ShouldSearch do + @moduledoc """ + Determines if the type of the resource is in the types being searched + """ + use Ash.Flow.Step + + def run(input, _opts, _context) do + resource = input[:resource] + types = input[:types] + + {:ok, is_nil(types) || AshHq.Docs.Extensions.Search.type(resource) in types} + end +end diff --git a/lib/ash_hq_web/schema.ex b/lib/ash_hq_web/schema.ex index 6050582..2ae26dc 100644 --- a/lib/ash_hq_web/schema.ex +++ b/lib/ash_hq_web/schema.ex @@ -6,12 +6,6 @@ defmodule AshHqWeb.Schema do use AshGraphql, apis: @apis - query do - end - - # mutation do - # end - def context(ctx) do AshGraphql.add_context(ctx, @apis) end diff --git a/mix.lock b/mix.lock index b857520..616d1be 100644 --- a/mix.lock +++ b/mix.lock @@ -1,10 +1,11 @@ %{ "absinthe": {:hex, :absinthe, "1.7.0", "36819e7b1fd5046c9c734f27fe7e564aed3bda59f0354c37cd2df88fd32dd014", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0 or ~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "566a5b5519afc9b29c4d367f0c6768162de3ec03e9bf9916f9dc2bcbe7c09643"}, "absinthe_plug": {:hex, :absinthe_plug, "1.5.8", "38d230641ba9dca8f72f1fed2dfc8abd53b3907d1996363da32434ab6ee5d6ab", [:mix], [{:absinthe, "~> 1.5", [hex: :absinthe, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "bbb04176647b735828861e7b2705465e53e2cf54ccf5a73ddd1ebd855f996e5a"}, - "ash": {:git, "https://github.com/ash-project/ash.git", "0efad5ae4efa90dbe6e7b65ffe48b875386f6374", []}, - "ash_graphql": {:git, "https://github.com/ash-project/ash_graphql.git", "1d31f7a20c2ee9545245f2539f1628d0f5a1afe9", []}, + "ash": {:git, "https://github.com/ash-project/ash.git", "6751afc6837dea7311f9cda82293a2af37a2ea10", []}, + "ash_graphql": {:git, "https://github.com/ash-project/ash_graphql.git", "75ba47215df6dcc2dea13bf7dd945063f08464a3", []}, + "ash_paper_trail": {:git, "https://github.com/ash-project/ash_paper_trail.git", "e6928944b51fd768d27cbb2ef80fb7864aaab46d", []}, "ash_phoenix": {:git, "https://github.com/ash-project/ash_phoenix.git", "bc0680439310b88b8dad05bbcdfca2230e33f465", []}, - "ash_postgres": {:git, "https://github.com/ash-project/ash_postgres.git", "12cf97d89e3fab29575b45baf5a5e6fb9d839e1a", []}, + "ash_postgres": {:git, "https://github.com/ash-project/ash_postgres.git", "fb8a13f33d36d84b58559b4345e3a01606c00fd2", []}, "bcrypt_elixir": {:hex, :bcrypt_elixir, "3.0.1", "9be815469e6bfefec40fa74658ecbbe6897acfb57614df1416eeccd4903f602c", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "486bb95efb645d1efc6794c1ddd776a186a9a713abf06f45708a6ce324fb96cf"}, "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, "castore": {:hex, :castore, "0.1.18", "deb5b9ab02400561b6f5708f3e7660fc35ca2d51bfc6a940d2f513f89c2975fc", [:mix], [], "hexpm", "61bbaf6452b782ef80b33cdb45701afbcf0a918a45ebe7e73f1130d661e66a06"}, @@ -24,7 +25,7 @@ "docsh": {:hex, :docsh, "0.7.2", "f893d5317a0e14269dd7fe79cf95fb6b9ba23513da0480ec6e77c73221cae4f2", [:rebar3], [{:providers, "1.8.1", [hex: :providers, repo: "hexpm", optional: false]}], "hexpm", "4e7db461bb07540d2bc3d366b8513f0197712d0495bb85744f367d3815076134"}, "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.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"}, - "ecto": {:hex, :ecto, "3.9.0", "7c74fc0d950a700eb7019057ff32d047ed7f19b57c1b2ca260cf0e565829101d", [:mix], [{:decimal, "~> 1.6 or ~> 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", "fed5ebc5831378b916afd0b5852a0c5bb3e7390665cc2b0ec8ab0c712495b73d"}, + "ecto": {:hex, :ecto, "3.9.1", "67173b1687afeb68ce805ee7420b4261649d5e2deed8fe5550df23bab0bc4396", [:mix], [{:decimal, "~> 1.6 or ~> 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", "c80bb3d736648df790f7f92f81b36c922d9dd3203ca65be4ff01d067f54eb304"}, "ecto_psql_extras": {:hex, :ecto_psql_extras, "0.7.4", "5d43fd088d39a158c860b17e8d210669587f63ec89ea122a4654861c8c6e2db4", [:mix], [{:ecto_sql, "~> 3.4", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, ">= 0.15.7", [hex: :postgrex, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1", [hex: :table_rex, repo: "hexpm", optional: false]}], "hexpm", "311db02f1b772e3d0dc7f56a05044b5e1499d78ed6abf38885e1ca70059449e5"}, "ecto_sql": {:hex, :ecto_sql, "3.9.0", "2bb21210a2a13317e098a420a8c1cc58b0c3421ab8e3acfa96417dab7817918c", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a8f3f720073b8b1ac4c978be25fa7960ed7fd44997420c304a4a2e200b596453"}, "eflambe": {:git, "https://github.com/DockYard/flame_on.git", "3f4e3bfa2e5546d95fb94d7ecb4b459bc4398c41", []}, @@ -87,7 +88,7 @@ "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "sobelow": {:hex, :sobelow, "0.11.1", "23438964486f8112b41e743bbfd402da3e5b296fdc9eacab29914b79c48916dd", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "9897363a7eff96f4809304a90aad819e2ad5e5d24db547af502885146746a53c"}, "sourceror": {:hex, :sourceror, "0.11.2", "549ce48be666421ac60cfb7f59c8752e0d393baa0b14d06271d3f6a8c1b027ab", [:mix], [], "hexpm", "9ab659118896a36be6eec68ff7b0674cba372fc8e210b1e9dc8cf2b55bb70dfb"}, - "spark": {:hex, :spark, "0.1.27", "b8c956b784db4a90a2bc0f43820efbaa16ac54fbd8bc6e01460a93dd0540d470", [:mix], [{:nimble_options, "~> 0.4.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.1", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "7412778f8408cb4b0742aa1af08fa5daa7d14e8416a4a78ae01fb5af7edef778"}, + "spark": {:hex, :spark, "0.1.28", "8ce732daa56ad0dc11190b28461f85e71b67c5b61ce4818841bc8fcdbf799676", [:mix], [{:nimble_options, "~> 0.4.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.1", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "11b2d52b473345e2ecb4fe70c76ca8400b2fa9417acb629a6bd92db9d3ff953b"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, "stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"}, "surface": {:hex, :surface, "0.9.1", "6a343564b1d6c17c619ac933cec5680ffe8c68f0cd2d85f780b70f6607750a96", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.18.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.11", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "131312d35d190739d0e0f1681acb9fce6962d3f81439011a3331bad2976ca372"}, diff --git a/test/tailwind_test.exs b/test/tailwind_test.exs deleted file mode 100644 index 5c5f98c..0000000 --- a/test/tailwind_test.exs +++ /dev/null @@ -1,5 +0,0 @@ -defmodule AshHq.Test.TailwindTest do - use ExUnit.Case, async: true - - doctest AshHq.Tailwind, import: true -end