Module: Resort

Defined in:
lib/resort.rb,
lib/resort/version.rb,
lib/generators/active_record/resort_generator.rb

Overview

# 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.

Examples:

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']

Defined Under Namespace

Modules: ClassMethods, Generators, Sortable

Constant Summary collapse

VERSION =

Resort’s version number

"0.2.3"