{"id":31545264,"url":"https://github.com/dmarkow/ecto_ranked","last_synced_at":"2025-10-04T14:43:34.470Z","repository":{"id":46150666,"uuid":"91149238","full_name":"dmarkow/ecto_ranked","owner":"dmarkow","description":"Ranking models for Ecto","archived":false,"fork":false,"pushed_at":"2025-07-29T19:05:15.000Z","size":58,"stargazers_count":44,"open_issues_count":3,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-10T18:38:09.817Z","etag":null,"topics":["elixir","ranking","sorting"],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/ecto_ranked","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmarkow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-05-13T04:29:12.000Z","updated_at":"2025-04-30T18:22:15.000Z","dependencies_parsed_at":"2025-07-29T21:05:56.023Z","dependency_job_id":"3b653415-b5d3-463e-acab-d58eaabf4bbe","html_url":"https://github.com/dmarkow/ecto_ranked","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/dmarkow/ecto_ranked","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmarkow%2Fecto_ranked","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmarkow%2Fecto_ranked/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmarkow%2Fecto_ranked/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmarkow%2Fecto_ranked/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmarkow","download_url":"https://codeload.github.com/dmarkow/ecto_ranked/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmarkow%2Fecto_ranked/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278327866,"owners_count":25968897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","ranking","sorting"],"created_at":"2025-10-04T14:43:33.425Z","updated_at":"2025-10-04T14:43:34.464Z","avatar_url":"https://github.com/dmarkow.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EctoRanked\n\nThis package adds automatic ranking to your Ecto models. It's heavily based on\nthe Rails [ranked-model](https://github.com/mixonic/ranked-model) gem.\n\n## Installation\n\nThe package can be installed by adding `ecto_ranked` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:ecto_ranked, \"~\u003e 0.6.0\"}]\nend\n```\n\n## Usage\n\nTo get started:\n\n- `import EctoRanked`\n- Add a `:rank` integer field to your model (NOTE: Setting a unique index on this column may cause issues depending on your database platform)\n- Call `set_rank()` in your changeset\n- Optionally, add a virtual `:position` field (with a type of `:any`) so you can move items in your list.\n\n```elixir\ndefmodule MyApp.Item do\n  use MyApp.Web, :model\n  import EctoRanked\n\n  schema \"items\" do\n    field :rank, :integer\n    field :position, :any, virtual: true\n  end\n\n  def changeset(struct, params \\\\ %{}) do\n    struct\n    |\u003e cast(params, [:position])\n    |\u003e set_rank()\n  end\nend\n```\n\nIf you need to use field names other than `:rank` and `:position`, you can pass those as options to `set_rank`:\n\n```elixir\ndefmodule MyApp.Item do\n  use MyApp.Web, :model\n  import EctoRanked\n\n  schema \"items\" do\n    field :my_ranking_field, :integer\n    field :my_position_field, :any, virtual: true\n  end\n\n  def changeset(struct, params \\\\ %{}) do\n    struct\n    |\u003e cast(params, [:my_position_field])\n    |\u003e set_rank(rank: :my_ranking_field, position: :my_position_field)\n  end\nend\n```\n\nIf you'd like to scope your ranking to a certain field (e.g. an association, string field, etc.),\njust add a `:scope` argument to `set_rank`:\n\n```elixir\ndefmodule MyApp.Item do\n  use MyApp.Web, :model\n  import EctoRanked\n\n  schema \"items\" do\n    field :rank, :integer\n    field :position, :any, virtual: true\n    belongs_to :parent, MyApp.Parent\n  end\n\n  def changeset(struct, params \\\\ %{}) do\n    struct\n    |\u003e cast(params, [:position])\n    |\u003e set_rank(scope: :parent_id)\n  end\nend\n```\n\nYou can scope across multiple fields:\n\n```elixir\nstruct\n|\u003e cast(params, [:position, :parent_id, :category])\n|\u003e set_rank(scope: [:parent_id, :category])\n```\n\nScopes are optional by default, meaning within a single table you might have some records scoped against a value, and some records not scoped at all (when the scoped value receives a `nil` value). Those unscoped records are treated as their own global scope. If you want to ensure a scope is always provided, you can use the `scope_required` option, which is effectively the same as adding your own `validate_required/3` call:\n\n```elixir\nstruct\n|\u003e cast(params, [:position, :parent_id, :category])\n|\u003e set_rank(scope: :parent_id, scope_required: true)\n```\n\nYou can even have multiple rankings that sort independently of each other (e.g. a scoped one and a global one, or multiple global ones):\n\n```elixir\nstruct\n|\u003e cast(params, [:local_position, :global_position, :parent_id])\n|\u003e set_rank(rank: :scoped_rank, position: :scoped_position, scope: :parent_id)\n|\u003e set_rank(rank: :global_rank, position: :global_position)\n```\n\nPosition is a write-only virtual attribute that's meant for placing an item at\na specific rank. By default the `position` attribute will be `nil` but you can\ncalculate it on demand:\n\n```elixir\ndef compute_positions(items \\\\ []) do\n  for {item, i} \u003c- Enum.with_index(items) do\n    %{item | position: i}\n  end\nend\n\nItem\n|\u003e order_by([:rank])\n|\u003e Repo.all()\n|\u003e Item.compute_positions()\n```\n\n## Documentation\n\nDocumentation can be found at [https://hexdocs.pm/ecto_ranked](https://hexdocs.pm/ecto_ranked).\n\n## Thanks\n\n- Everyone who contributed to [ranked-model](https://github.com/mixonic/ranked-model/graphs/contributors), of which this package is a rough clone.\n- [EctoOrdered](https://github.com/zovafit/ecto-ordered), which provided a great starting point.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmarkow%2Fecto_ranked","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmarkow%2Fecto_ranked","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmarkow%2Fecto_ranked/lists"}