Skip to content

Commit

Permalink
fix: updating to handle special characters in the tag name
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaugher committed Sep 18, 2023
1 parent 6185328 commit ee973a0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
8 changes: 0 additions & 8 deletions lib/tags_multi_tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ defmodule TagsMultiTenant do
end

def tagged_with(tags, model, context, opts) do
IO.inspect("TAGGED")
IO.inspect(tags)
IO.inspect(model)
do_tags_search(model, tags, context) |> repo().all(opts)
end

Expand All @@ -241,19 +238,14 @@ defmodule TagsMultiTenant do
def tagged_with_query(query, tags, context \\ "tags")

def tagged_with_query(query, tag, context) when is_bitstring(tag) do
IO.inspect("1")
tagged_with_query(query, [tag], context)
end

def tagged_with_query(query, tags, context) do
IO.inspect("2")
do_tags_search(query, tags, context)
end

defp do_tags_search(queryable, tags, context) do
IO.inspect(queryable)
IO.inspect(tags)
IO.inspect(context)
%{from: %{source: {_source, schema}}} = Ecto.Queryable.to_query(queryable)

queryable
Expand Down
20 changes: 1 addition & 19 deletions lib/tags_multi_tenant/tags_multi_tenant_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do
)
end

@tag_format ~r/^(begins|ends|contains|equals):(\w+)$/
@tag_format ~r/^(begins|ends|contains|equals):(.*)$/

defp split_action_value(tag = [_head | _tail = []]) do
with tag when not is_nil(tag) <- Regex.run(@tag_format, Enum.at(tag, 0, []), capture: :first) do
Expand Down Expand Up @@ -77,7 +77,6 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do
end

defp create_where("equals", value) when byte_size(value) > 0 do
IO.inspect("HERE")
dynamic([tags: tags], tags.name == ^value)
end

Expand All @@ -90,7 +89,6 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do
create_where(action, value)
else
{:error, message} ->
IO.inspect(message)
true

_ ->
Expand All @@ -109,47 +107,36 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do
defp create_or_where(action, value, conditions)

defp create_or_where("begins", value, conditions) when byte_size(value) > 0 do
IO.inspect("1")
like = "#{value}%"
dynamic([tags: tags], ^conditions or ilike(tags.name, ^like))
end

defp create_or_where("ends", value, conditions) when byte_size(value) > 0 do
IO.inspect("2")
like = "%#{value}"
dynamic([tags: tags], ^conditions or ilike(tags.name, ^like))
end

defp create_or_where("contains", value, conditions) when byte_size(value) > 0 do
IO.inspect("3")
like = "%#{value}%"
dynamic([tags: tags], ^conditions or ilike(tags.name, ^like))
end

defp create_or_where("equals", value, conditions) when byte_size(value) > 0 do
IO.inspect("4")
dynamic([tags: tags], ^conditions or tags.name == ^value)
end

defp create_or_where(_, _, _) do
IO.inspect("5")
true
end

defp build_or_where(tag = [_head | _tail = []], conditions) do
with {:ok, action, value} <- split_action_value(tag) do
IO.inspect("INSIDE")
IO.inspect(action)
IO.inspect(value)
create_or_where(action, value, conditions)
else
{:error, message} ->
IO.inspect("ERROR1")
IO.inspect(message)
conditions

_ ->
IO.inspect("ERROR2")
conditions
end
end
Expand All @@ -162,10 +149,6 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do

conditions = build_where(tags)

IO.inspect("SEARCH")

IO.inspect(conditions)

query
|> join_taggings_from_model(context, taggable_type)
|> join_tags
Expand All @@ -176,7 +159,6 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do
# |> having([taggings: taggings], count(taggings.taggable_id) <= ^tags_length)
|> order_by([m], asc: m.inserted_at)
|> select([m], m)
|> IO.inspect()
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TagsMultiTenant.Mixfile do
[
app: :tags_multi_tenant,
name: "TagsMultiTenant",
version: "0.1.16",
version: "0.1.17",
elixir: "~> 1.10",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down
14 changes: 14 additions & 0 deletions test/tags_multi_tenant_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ defmodule TagsMultiTenantTest do
assert Enum.sort(result) == Enum.sort([post1, post2, post3])
end

test "should return filtered posts, begins action, including dash" do
post1 = @repo.insert!(%Post{title: "hello world1"})
post2 = @repo.insert!(%Post{title: "hello world2"})
post3 = @repo.insert!(%Post{title: "hello world3"})
TagsMultiTenant.add(post1, "dash-dash")
TagsMultiTenant.add(post2, "tagged1")
TagsMultiTenant.add(post3, "tagged2")

result = TagsMultiTenant.tagged_with("begins:dash-dash", Post)

assert Enum.count(result) == 1
assert Enum.sort(result) == Enum.sort([post1])
end

test "should return all posts, ignoring invalid tag, ends action" do
post1 = @repo.insert!(%Post{title: "hello world1"})
post2 = @repo.insert!(%Post{title: "hello world2"})
Expand Down

0 comments on commit ee973a0

Please sign in to comment.