All Projects → dmarkow → ecto_ranked

dmarkow / ecto_ranked

Licence: MIT license
Ranking models for Ecto

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to ecto ranked

PageRank
Page Rank library for Javascript
Stars: ✭ 23 (-37.84%)
Mutual labels:  ranking
ultra-sort
DSL for SIMD Sorting on AVX2 & AVX512
Stars: ✭ 29 (-21.62%)
Mutual labels:  sorting
crystal-web-framework-stars
⭐️ Web frameworks for Crystal, most starred on Github
Stars: ✭ 40 (+8.11%)
Mutual labels:  ranking
RankNet
Learning to Rank from Pair-wise data
Stars: ✭ 40 (+8.11%)
Mutual labels:  ranking
spring-boot-jpa-rest-demo-filter-paging-sorting
Spring Boot Data JPA with Filter, Pagination and Sorting
Stars: ✭ 70 (+89.19%)
Mutual labels:  sorting
softpool
SoftPoolNet: Shape Descriptor for Point Cloud Completion and Classification - ECCV 2020 oral
Stars: ✭ 62 (+67.57%)
Mutual labels:  sorting
AlgorithmVisualizer
A better visualization of different algorithms made with React
Stars: ✭ 123 (+232.43%)
Mutual labels:  sorting
algorithms
Algorithms in python and C
Stars: ✭ 71 (+91.89%)
Mutual labels:  sorting
cakephp-sequence
CakePHP plugin for maintaining a contiguous sequence of records
Stars: ✭ 41 (+10.81%)
Mutual labels:  sorting
ScoreboardStats
Bukkit plugin for customizing the sidebar of the scoreboard feature from minecraft
Stars: ✭ 29 (-21.62%)
Mutual labels:  ranking
svelte-datagrid
Svelte data grid spreadsheet best best features and performance from excel
Stars: ✭ 48 (+29.73%)
Mutual labels:  sorting
algos
A collection of algorithms in rust
Stars: ✭ 16 (-56.76%)
Mutual labels:  sorting
hidden-gems
Ranking of Steam games which favors "hidden gems". Featured in PC Gamer.
Stars: ✭ 37 (+0%)
Mutual labels:  ranking
vote-schulze
Vote calculation with Schulze method (Condorcet voting)
Stars: ✭ 23 (-37.84%)
Mutual labels:  ranking
Neural Bayesian Personalized Ranking
Representation Learning and Pairwise Ranking for Implicit Feedback in Top-N Item Recommendation
Stars: ✭ 23 (-37.84%)
Mutual labels:  ranking
cyberrating
🚥 S&P of Blockchains
Stars: ✭ 13 (-64.86%)
Mutual labels:  ranking
pysorter
A command line utility for organizing files and directories according to regex patterns.
Stars: ✭ 40 (+8.11%)
Mutual labels:  sorting
HallOfFame-Root-me.org
💀 Root-me Hall Of Fame dashboard 💀
Stars: ✭ 15 (-59.46%)
Mutual labels:  ranking
intergo
A package for interleaving / multileaving ranking generation in go
Stars: ✭ 30 (-18.92%)
Mutual labels:  ranking
image-sorter2
One-click image sorting/labelling script
Stars: ✭ 65 (+75.68%)
Mutual labels:  sorting

EctoRanked

This package adds automatic ranking to your Ecto models. It's heavily based on the Rails ranked-model gem.

Installation

The package can be installed by adding ecto_ranked to your list of dependencies in mix.exs:

def deps do
  [{:ecto_ranked, "~> 0.4.0"}]
end

Usage

To get started:

  • import EctoRanked
  • Add a :rank integer field to your model (NOTE: Setting a unique index on this column may cause issues depending on your database platform)
  • Call set_rank() in your changeset
  • Optionally, add a virtual :position field (with a type of :any) so you can move items in your list.
defmodule MyApp.Item do
  use MyApp.Web, :model
  import EctoRanked

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

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:position])
    |> set_rank()
  end
end

If you need to use field names other than :rank and :position, you can pass those as options to set_rank:

defmodule MyApp.Item do
  use MyApp.Web, :model
  import EctoRanked

  schema "items" do
    field :my_ranking_field, :integer
    field :my_position_field, :any, virtual: true
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:my_position_field])
    |> set_rank(rank: :my_ranking_field, position: :my_position_field)
  end
end

If you'd like to scope your ranking to a certain field (e.g. an association, string field, etc.), just add a :scope argument to set_rank:

defmodule MyApp.Item do
  use MyApp.Web, :model
  import EctoRanked

  schema "items" do
    field :rank, :integer
    field :position, :any, virtual: true
    belongs_to :parent, MyApp.Parent
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:position])
    |> set_rank(scope: :parent_id)
  end
end

You can scope across multiple fields:

struct
|> cast(params, [:position, :parent_id, :category])
|> set_rank(scope: [:parent_id, :category])

Scopes 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:

struct
|> cast(params, [:position, :parent_id, :category])
|> set_rank(scope: :parent_id, scope_required: true)

You can even have multiple rankings that sort independently of each other (e.g. a scoped one and a global one, or multiple global ones):

struct
|> cast(params, [:local_position, :global_position, :parent_id])
|> set_rank(rank: :scoped_rank, position: :scoped_position, scope: :parent_id)
|> set_rank(rank: :global_rank, position: :global_position)

Position is a write-only virtual attribute that's meant for placing an item at a specific rank. By default the position attribute will be nil but you can calculate it on demand:

def compute_positions(items \\ []) do
  for {item, i} <- Enum.with_index(items) do
    %{item | position: i}
  end
end

Item
|> order_by([:rank])
|> Repo.all()
|> Item.compute_positions()

Documentation

Documentation can be found at https://hexdocs.pm/ecto_ranked.

Thanks

  • Everyone who contributed to ranked-model, of which this package is a rough clone.
  • EctoOrdered, which provided a great starting point.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].