Skip to content

Commit

Permalink
fix: Actions should use all index filtering tools when building a query
Browse files Browse the repository at this point in the history
  • Loading branch information
staylorwr committed Oct 9, 2020
1 parent 6ecf7d7 commit 9c55bcf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
13 changes: 7 additions & 6 deletions lib/ex_teal/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ defmodule ExTeal.Action do
query =
conn
|> resource.handle_index(params)
|> Index.field_filters(conn.params, resource)
|> Index.filter_via_relationships(params)
|> Index.field_filters(params, resource)
|> Index.search(params, resource)

case query do
[] -> {:error, :not_found}
query -> {:ok, query}
end
{:ok, query}
end

def actionable_query(resource, %Conn{params: %{"resources" => ids} = params} = conn) do
Expand All @@ -112,7 +111,9 @@ defmodule ExTeal.Action do
query =
conn
|> resource.handle_index(params)
|> Index.field_filters(conn.params, resource)
|> Index.filter_via_relationships(params)
|> Index.field_filters(params, resource)
|> Index.search(params, resource)
|> where([r], r.id in ^ids)

{:ok, query}
Expand Down
13 changes: 8 additions & 5 deletions lib/ex_teal/resource/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,14 @@ defmodule ExTeal.Resource.Index do
query
end

def filter_via_relationships(query, %{
"via_resource" => resource_name,
"via_resource_id" => resource_id,
"via_relationship" => rel_name
}) do
def filter_via_relationships(
query,
%{
"via_resource" => resource_name,
"via_resource_id" => resource_id,
"via_relationship" => rel_name
}
) do
with {:ok, resource} <- ExTeal.resource_for(resource_name),
{:ok, relationship} <- schema_assoc_for(resource, rel_name) do
from(query, where: ^[{relationship.related_key, resource_id}])
Expand Down
41 changes: 41 additions & 0 deletions test/ex_teal/action_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,46 @@ defmodule ExTeal.ActionTest do
[found_post] = Repo.all(query)
assert found_post.user.id == p.user.id
end

test "filters based on filter fields" do
p = insert(:post, name: "Foo")
insert(:post)
filters = [%{"field" => "name", "operator" => "=", "operand" => "Foo"}]
encoded_filters = filters |> Jason.encode!() |> :base64.encode()

conn =
build_conn(:get, "/posts/", %{
"field_filters" => encoded_filters,
"resources" => "all",
"action" => "publish-post"
})
|> fetch_query_params()

{:ok, query} = Action.actionable_query(PostResource, conn)

[found_post] = Repo.all(query)
assert found_post.id == p.id
end

@tag manifest: TestExTeal.DefaultManifest
test "filters based on relationships" do
u = insert(:user)
p = insert(:post, user: u)
insert(:post)

conn =
build_conn(:get, "posts", %{
"via_resource" => "users",
"via_resource_id" => "#{u.id}",
"via_relationship" => "posts",
"relationship_type" => "hasMany",
"resources" => "all"
})
|> fetch_query_params()

{:ok, query} = Action.actionable_query(PostResource, conn)
[found_post] = Repo.all(query)
assert found_post.id == p.id
end
end
end
1 change: 1 addition & 0 deletions test/support/schema.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule TestExTeal.User do
schema "users" do
field(:email, :string)
field(:name, :string)
has_many(:posts, TestExTeal.Post)

many_to_many(:preferred_tags, TestExTeal.Tag, join_through: TestExTeal.PreferredTag)
timestamps()
Expand Down

0 comments on commit 9c55bcf

Please sign in to comment.