Skip to content

Commit

Permalink
fix: cast fields to string for simple search
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewar authored and staylorwr committed May 11, 2021
1 parent f16975f commit 2e6637d
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions lib/ex_teal/search/simple_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,10 @@ defmodule ExTeal.Search.SimpleSearch do
@spec build(Ecto.Query.t(), module(), map()) :: Ecto.Query.t()
def build(query, resource, %{"search" => term}) do
dynamic =
Enum.reduce(resource.search(), false, fn field_name, dynamic ->
dynamic([q], ilike(field(q, ^field_name), ^"%#{term}%") or ^dynamic)
Enum.reduce([:id | resource.search()], false, fn field_name, dynamic ->
dynamic([q], ilike(type(field(q, ^field_name), :string), ^"%#{term}%") or ^dynamic)
end)

dynamic =
case Integer.parse(term) do
{id, ""} ->
dynamic([q], q.id == ^id or ^dynamic)

{_integer, _remainder} ->
dynamic

:error ->
dynamic
end

from(query, where: ^dynamic)
end
end

0 comments on commit 2e6637d

Please sign in to comment.