Skip to content

Commit a74c25c

Browse files
committed
code quality
1 parent 1dd467a commit a74c25c

2 files changed

Lines changed: 25 additions & 34 deletions

File tree

lib/nested_sets.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,15 @@ defmodule NestedSets do
118118
defp maybe_set_tree_id(_repo, node, %{tree: false}), do: node
119119

120120
defp maybe_set_tree_id(repo, node, cfg) do
121-
schema = node.__struct__
122121
pk = get_primary_key(node)
123122

124123
{1, _} =
125124
repo.update_all(
126-
from(n in schema, where: n.id == ^pk),
125+
from(n in node.__struct__, where: n.id == ^pk),
127126
set: [{cfg.tree, pk}]
128127
)
129128

130-
repo.get!(schema, pk)
129+
repo.get!(node.__struct__, pk)
131130
end
132131

133132
@doc """
@@ -183,7 +182,6 @@ defmodule NestedSets do
183182
@spec delete_with_children(Ecto.Repo.t(), ns_node()) :: {:ok, integer()} | {:error, term()}
184183
def delete_with_children(repo, node) do
185184
cfg = config(node)
186-
schema = node.__struct__
187185

188186
repo.transact(fn ->
189187
refreshed = repo.reload!(node)
@@ -192,7 +190,7 @@ defmodule NestedSets do
192190
width = rgt - lft + 1
193191

194192
query =
195-
from(n in schema,
193+
from(n in node.__struct__,
196194
where: field(n, ^cfg.lft) >= ^lft,
197195
where: field(n, ^cfg.rgt) <= ^rgt
198196
)
@@ -595,7 +593,6 @@ defmodule NestedSets do
595593
end
596594

597595
defp move_node_as_root(repo, node, cfg) do
598-
schema = node.__struct__
599596
old_tree = Map.get(node, cfg.tree)
600597
pk = get_primary_key(node)
601598

@@ -607,7 +604,7 @@ defmodule NestedSets do
607604
width = rgt - lft + 1
608605

609606
subtree_query =
610-
from(n in schema,
607+
from(n in node.__struct__,
611608
where: field(n, ^cfg.lft) >= ^lft,
612609
where: field(n, ^cfg.rgt) <= ^rgt,
613610
where: field(n, ^cfg.tree) == ^old_tree
@@ -624,7 +621,7 @@ defmodule NestedSets do
624621

625622
for attr <- [cfg.lft, cfg.rgt] do
626623
close_query =
627-
from(n in schema,
624+
from(n in node.__struct__,
628625
where: field(n, ^attr) > ^rgt,
629626
where: field(n, ^cfg.tree) == ^old_tree
630627
)

lib/nested_sets/query.ex

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule NestedSets.Query do
4141
## Options
4242
* `:depth` - limit to ancestors within N levels (optional)
4343
"""
44-
@spec ancestors(Ecto.Queryable.t(), ns_node(), keyword()) :: Ecto.Query.t() | nil
44+
@spec ancestors(Ecto.Queryable.t(), ns_node(), keyword()) :: Ecto.Query.t()
4545
def ancestors(queryable, node, opts \\ []) do
4646
schema = get_schema(queryable)
4747
cfg = config(schema)
@@ -72,10 +72,7 @@ defmodule NestedSets.Query do
7272
apply_tree_filter(query, node, cfg)
7373
end
7474

75-
@doc """
76-
Alias for `ancestors/3`.
77-
"""
78-
@spec parents(Ecto.Queryable.t(), ns_node(), keyword()) :: Ecto.Query.t() | nil
75+
@doc "Alias for `ancestors/3`."
7976
def parents(queryable, node, opts \\ []), do: ancestors(queryable, node, opts)
8077

8178
@doc """
@@ -84,7 +81,7 @@ defmodule NestedSets.Query do
8481
## Options
8582
* `:depth` - limit to descendants within N levels (optional)
8683
"""
87-
@spec descendants(Ecto.Queryable.t(), ns_node(), keyword()) :: Ecto.Query.t() | nil
84+
@spec descendants(Ecto.Queryable.t(), ns_node(), keyword()) :: Ecto.Query.t()
8885
def descendants(queryable, node, opts \\ []) do
8986
schema = get_schema(queryable)
9087
cfg = config(schema)
@@ -115,22 +112,19 @@ defmodule NestedSets.Query do
115112
apply_tree_filter(query, node, cfg)
116113
end
117114

118-
@doc """
119-
Alias for `descendants/3`.
120-
"""
121-
@spec children(Ecto.Queryable.t(), ns_node(), keyword()) :: Ecto.Query.t() | nil
115+
@doc "Alias for `descendants/3`."
122116
def children(queryable, node, opts \\ []), do: descendants(queryable, node, opts)
123117

124118
@doc """
125119
Finds only direct children of a node (depth = 1).
126120
"""
127-
@spec direct_children(Ecto.Queryable.t(), struct()) :: Ecto.Query.t() | nil
121+
@spec direct_children(Ecto.Queryable.t(), struct()) :: Ecto.Query.t()
128122
def direct_children(queryable, node), do: descendants(queryable, node, depth: 1)
129123

130124
@doc """
131125
Finds all leaf nodes (nodes without children) under a node.
132126
"""
133-
@spec leaves(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
127+
@spec leaves(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
134128
def leaves(queryable, node) do
135129
schema = get_schema(queryable)
136130
cfg = config(schema)
@@ -152,7 +146,7 @@ defmodule NestedSets.Query do
152146
@doc """
153147
Finds the previous sibling of a node.
154148
"""
155-
@spec prev_sibling(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
149+
@spec prev_sibling(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
156150
def prev_sibling(queryable, node) do
157151
schema = get_schema(queryable)
158152
cfg = config(schema)
@@ -170,13 +164,13 @@ defmodule NestedSets.Query do
170164
@doc """
171165
Alias for `prev_sibling/2`.
172166
"""
173-
@spec prev(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
167+
@spec prev(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
174168
def prev(queryable, node), do: prev_sibling(queryable, node)
175169

176170
@doc """
177171
Finds the next sibling of a node.
178172
"""
179-
@spec next_sibling(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
173+
@spec next_sibling(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
180174
def next_sibling(queryable, node) do
181175
schema = get_schema(queryable)
182176
cfg = config(schema)
@@ -191,16 +185,13 @@ defmodule NestedSets.Query do
191185
apply_tree_filter(query, node, cfg)
192186
end
193187

194-
@doc """
195-
Alias for `next_sibling/2`.
196-
"""
197-
@spec next(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
188+
@doc "Alias for `next_sibling/2`."
198189
def next(queryable, node), do: next_sibling(queryable, node)
199190

200191
@doc """
201192
Finds all siblings of a node (nodes with the same parent).
202193
"""
203-
@spec siblings(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
194+
@spec siblings(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
204195
def siblings(queryable, node) do
205196
schema = get_schema(queryable)
206197
cfg = config(schema)
@@ -247,7 +238,7 @@ defmodule NestedSets.Query do
247238
@doc """
248239
Finds the root node for a specific tree (when using tree).
249240
"""
250-
@spec root(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
241+
@spec root(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
251242
def root(queryable, node) do
252243
schema = get_schema(queryable)
253244
cfg = config(schema)
@@ -264,7 +255,7 @@ defmodule NestedSets.Query do
264255
@doc """
265256
Gets a node and all its descendants (the full subtree including the node itself).
266257
"""
267-
@spec subtree(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t() | nil
258+
@spec subtree(Ecto.Queryable.t(), ns_node()) :: Ecto.Query.t()
268259
def subtree(queryable, node) do
269260
schema = get_schema(queryable)
270261
cfg = config(schema)
@@ -300,13 +291,13 @@ defmodule NestedSets.Query do
300291
Filters by a specific tree (when using tree).
301292
Accepts either a tree_id integer or a node.
302293
"""
303-
@spec in_tree(Ecto.Queryable.t(), pos_integer() | ns_node()) :: Ecto.Query.t() | nil
294+
@spec in_tree(Ecto.Queryable.t(), pos_integer() | ns_node()) :: Ecto.Query.t()
304295
def in_tree(queryable, tree_id) when is_integer(tree_id) do
305296
schema = get_schema(queryable)
306297
cfg = config(schema)
307298

308299
if cfg.tree == false do
309-
queryable
300+
to_query(queryable)
310301
else
311302
from(n in queryable,
312303
where: field(n, ^cfg.tree) == ^tree_id
@@ -320,9 +311,12 @@ defmodule NestedSets.Query do
320311
apply_tree_filter(queryable, node, cfg)
321312
end
322313

323-
# Private section
314+
# Helpers
315+
316+
# ensure we always return a Query struct, even if passing through an atom
317+
defp to_query(queryable), do: Ecto.Queryable.to_query(queryable)
324318

325-
defp apply_tree_filter(query, _node, %{tree: false} = _cfg), do: query
319+
defp apply_tree_filter(query, _node, %{tree: false} = _cfg), do: to_query(query)
326320

327321
defp apply_tree_filter(query, node, %{tree: tree_attr} = _cfg) do
328322
tree_value = Map.get(node, tree_attr)

0 commit comments

Comments
 (0)