Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zovafit/ecto-ordered

Ecto extension for ordered models
https://github.com/zovafit/ecto-ordered

Last synced: 7 days ago
JSON representation

Ecto extension for ordered models

Awesome Lists containing this project

README

        

EctoOrdered
===========

Ecto extension to support ordered list items. Similar to [acts_as_list](https://github.com/swanandp/acts_as_list), but
for [Ecto](https://github.com/elixir-lang/ecto)

Examples
--------

```elixir
# Global positioning
defmodule MyModel do
use Ecto.Schema
import EctoOrdered

schema "models" do
field :position, :integer, virtual: true
field, :rank, :integer
end

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

# Scoped positioning
defmodule MyModel do
use Ecto.Model
use EctoOrdered, scope: :reference_id

schema "models" do
field :reference_id, :integer
field :position, :integer, virtual: true
end

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