module Resort

# Resort

A tool that allows any ActiveRecord model to be sorted.

Unlike most Rails sorting plugins (acts_as_list, etc), Resort is based on linked lists rather than absolute position fields.

@example Using Resort in an ActiveRecord model

# In the migration
create_table :products do |t|
  t.text       :name
  t.references :next
  t.boolean    :first
end

# Model
class Product < ActiveRecord::Base
  resort!

  # A sortable model must implement #siblings method, which should
  # return and ActiveRecord::Relation with all the models to be
  # considered as `peers` in the list representing the sorted
  # products, i.e. its siblings.
  def siblings
    self.class.scoped
  end
end

product = Product.create(:name => 'Bread')
product.first? # => true

another_product = Product.create(:name => 'Milk')
yet_another_product = Product.create(:name => 'Salami')

yet_another_product.append_to(product)

Product.ordered.map(&:name)
# => ['Bread', 'Salami', 'Milk']

Constants

VERSION

Resort’s version number