Skip to content

Commit

Permalink
adding tests and bumping version for release
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaugher committed Aug 11, 2023
1 parent 3316153 commit 6185328
Show file tree
Hide file tree
Showing 7 changed files with 585 additions and 253 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It also allows you to specify various contexts

```elixir
def deps do
[{:tags_multi_tenant, "~> 0.1.8"}]
[{:tags_multi_tenant, "~> 0.1.16"}]
end
```

Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use Mix.Config

config :logger, level: :info
config :logger, level: :debug

config :tags_multi_tenant, ecto_repos: [TagsMultiTenant.Repo]

Expand Down
23 changes: 17 additions & 6 deletions lib/tags_multi_tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,18 @@ defmodule TagsMultiTenant do
@spec tagged_with(tags, module, context, opts) :: list
def tagged_with(tags, model, context \\ "tags", opts \\ [])

def tagged_with(tag, model, context, opts) when is_bitstring(tag),
do: tagged_with([tag], model, context, opts)
def tagged_with(tag, model, context, opts) when is_bitstring(tag) do
tagged_with([tag], model, context, opts)
end

def tagged_with(tag, model, context, _opts) when is_list(context),
do: tagged_with(tag, model, "tags", context)
def tagged_with(tag, model, context, _opts) when is_list(context) do
tagged_with(tag, model, "tags", context)
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 @@ -235,14 +240,20 @@ defmodule TagsMultiTenant do
"""
def tagged_with_query(query, tags, context \\ "tags")

def tagged_with_query(query, tag, context) when is_bitstring(tag),
do: tagged_with_query(query, [tag], context)
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
129 changes: 116 additions & 13 deletions lib/tags_multi_tenant/tags_multi_tenant_query.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule TagsMultiTenant.TagsMultiTenantQuery do
import Ecto.{Query}
alias Makeup.Styles.HTML.IgorStyle
alias TagsMultiTenant.{Tagging, Tag}

@moduledoc """
Expand Down Expand Up @@ -43,37 +44,139 @@ defmodule TagsMultiTenant.TagsMultiTenantQuery do
)
end

defp build_where(query, tags = [_head | _tail = []]) do
Enum.reduce(tags, query, fn value, query ->
query |> where([tags: tags], tags.name == ^value)
end)
@tag_format ~r/^(begins|ends|contains|equals):(\w+)$/

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
split = String.split(Enum.at(tag, 0, []), ":", trim: true)
action = Enum.at(split, 0, nil)
value = Enum.at(split, 1, nil)

{:ok, action, value}
else
_ ->
{:error, "incorrect format passed"}
end
end

defp build_where(query, [head | tail]) do
query = query |> build_where([head])
defp create_where(action, value)

Enum.reduce(tail, query, fn value, query ->
query
|> or_where([tags: tags], tags.name == ^value)
defp create_where("begins", value) when byte_size(value) > 0 do
like = "#{value}%"
dynamic([tags: tags], ilike(tags.name, ^like))
end

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

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

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

defp create_where(_, _) do
true
end

defp build_single(tag = [_head | _tail = []]) do
with {:ok, action, value} <- split_action_value(tag) do
create_where(action, value)
else
{:error, message} ->
IO.inspect(message)
true

_ ->
true
end
end

defp build_where([head | tail]) do
condition1 = build_single([head])

Enum.reduce(tail, condition1, fn tag, conditions ->
build_or_where([tag], conditions)
end)
end

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

@doc """
Build the query to search tagged resources
"""
def search_tagged_with(query, tags, context, taggable_type) do
tags_length = length(tags)
# tags_length = length(tags)

conditions = build_where(tags)

IO.inspect("SEARCH")

IO.inspect(conditions)

query
|> join_taggings_from_model(context, taggable_type)
|> join_tags
# |> where([tags: tags], tags.name in ^tags)
|> build_where(tags)
# |> IO.inspect()
# |> build_where(tags)
|> where(^conditions)
|> group_by([m], m.id)
|> having([taggings: taggings], count(taggings.taggable_id) == ^tags_length)
# |> 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
27 changes: 14 additions & 13 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ defmodule TagsMultiTenant.Mixfile do
use Mix.Project

def project do
[app: :tags_multi_tenant,
name: "TagsMultiTenant",
version: "0.1.15",
elixir: "~> 1.10",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
elixirc_paths: elixirc_paths(Mix.env),
package: package(),
deps: deps(),
description: description(),
source_url: "https://github.com/augustwenty/tags_multi_tenant",
docs: [main: "TagsMultiTenant", extras: ["README.md"]]
[
app: :tags_multi_tenant,
name: "TagsMultiTenant",
version: "0.1.16",
elixir: "~> 1.10",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
package: package(),
deps: deps(),
description: description(),
source_url: "https://github.com/augustwenty/tags_multi_tenant",
docs: [main: "TagsMultiTenant", extras: ["README.md"]]
]
end

Expand All @@ -39,7 +40,7 @@ defmodule TagsMultiTenant.Mixfile do
{:ecto_sql, "~> 3.0"},
{:postgrex, ">= 0.16.2"},
{:inflex, "~> 1.8.1"},
{:ex_doc, "~> 0.14", only: :dev, runtime: false},
{:ex_doc, "~> 0.14", only: :dev, runtime: false}
]
end

Expand Down
Loading

0 comments on commit 6185328

Please sign in to comment.