ecto_ordered v0.1.2 EctoOrdered

EctoOrdered provides changeset methods for updating ordering an ordering column

It should be added to your schema like so:

defmodule OrderedListItem do
  use Ecto.Schema
  import Ecto.Changeset

  schema "ordered_list_item" do
    field :title,            :string
    field :position,         :integer
  end

  def changeset(model, params) do
    model
    |> cast(params, [:position, :title])
    |> set_order(:position)
  end

  def delete(model) do
    model
    |> cast(%{}, [])
    |> Map.put(:action, :delete)
    |> set_order(:position)
  end
end

Note the delete function used to ensure that the remaining items are repositioned on deletion.

Summary

Functions

Returns a changeset which will include updates to the other ordered rows within the same transaction as the insertion, deletion or update of this row

Functions

set_order(changeset, field, scope \\ nil)

Returns a changeset which will include updates to the other ordered rows within the same transaction as the insertion, deletion or update of this row.

The arguments are as follows:

  • changeset the changeset which is part of the ordered list
  • field the field in which the order should be stored
  • scope the field in which the scope for the order should be stored (optional)