Hoping someone can help me understand one bit of code that appears in the To Do Trek example by @chrismccord
QUESTION 1:
The delete_todo
function calls multi_decrement_positions
as follows:
multi_decrement_positions(:dec_rest_in_list, todo, list_id: todo.list_id)
and the that function is defined as follows:
defp multi_decrement_positions(%Ecto.Multi{} = multi, name, %type{} = struct, where_query) do
multi_update_all(multi, name, fn _ ->
from(t in type,
where: ^where_query,
where:
t.position > subquery(from og in type, where: og.id == ^struct.id, select: og.position),
update: [inc: [position: -1]]
)
end)
end
My question is with the subquery that is basically establishing the āold_positionā of the todo that is being removed. I donāt understand why that subquery needs to be done. The ātodoā that is being deleted is passed as a parameter and already knows its position. Couldnāt multi_decrement be defined as follows?
Note: The reason Iām asking is because Iām passing in a many-to-many struct that doesnāt have a primary key id so og.id == ^struct.id
is going to fail. But it knows its position. And the ^where_query
has already identified the subset of structs that make up āthe list.ā
defp multi_decrement_positions(%Ecto.Multi{} = multi, name, %type{} = struct, where_query) do
multi_update_all(multi, name, fn _ ->
from(t in type,
where: ^where_query,
where:
t.position > struct.position,
update: [inc: [position: -1]]
)
end)
end
QUESTION 2:
Totally stupid question. What does the āogā in the subquery stand for? That acronym is driving me crazy. Itās like a personalized license plate that I canāt figure out.
QUESTION 3:
Iām studying this To Do example because it shows sortable lists that are also streamable. Items can be repositioned.
When a user has repositioned an item in a stream that has multiple items already displayed, does it make sense to try to update the stream (versus regenerating it)? Updating it would require identifying all the items in the stream that need to be deleted and then inserting the items with the new positions in the correct place. It seems like it is better to just regenerate the stream after an item has been moved (since there is a domino effect on the entire stream). Or is there a reason why itās better to make changes on the stream versus regenerating in this case?