• Stars
    star
    147
  • Rank 250,543 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 15 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Allows you to sort ActiveRecord items in multiple lists with multiple scopes

sortableΒΆ ↑

Allows you to sort ActiveRecord items similar to github.com/rails/acts_as_list but with added support for multiple scopes and lists

Requires ActiveRecord >= 2.0.0

InstallationΒΆ ↑

gem install shuber-sortable --source http://gems.github.com
OR
script/plugin install git://github.com/shuber/sortable.git

ExamplesΒΆ ↑

SimpleΒΆ ↑

Works just like github.com/rails/acts_as_list

class Todo < ActiveRecord::Base
  # schema
  #   id           :integer
  #   project_id   :integer
  #   description  :string
  #   position     :integer
  sortable :scope => :project_id
end

@todo = Todo.create(:description => 'do something', :project_id => 1)
@todo_2 = Todo.create(:description => 'do something else', :project_id => 1)
@todo_3 = Todo.create(:description => 'some other task', :project_id => 2)

@todo.position # 1
@todo_2.position # 2
@todo_3.position # 1

@todo.move_down!
@todo_2.reload

@todo.position # 2
@todo_2.position # 1
@todo_3.position # 1

Multiple scopesΒΆ ↑

Stories may or may not be in a sprint, but if we scoped just by :sprint_id, all stories with a nil :sprint_id would be sorted in one giant list instead of being sorted in each of their respective projects. Specifying an array of scopes fixes this problem.

class Story < ActiveRecord::Base
  # schema
  #   id           :integer
  #   project_id   :integer
  #   sprint_id    :integer
  #   description  :string
  #   position     :integer
  sortable :scope => [:project_id, :sprint_id]
end

Multiple listsΒΆ ↑

Your project management software needs to allow both clients and developers to prioritize todo items separately so that they can be discussed and reviewed during their next meeting. Multiple lists solves this problem.

class Todo < ActiveRecord::Base
  # schema
  #   id                  :integer
  #   project_id          :integer
  #   description         :string
  #   client_priority     :integer
  #   developer_priority  :integer
  sortable :scope => :project_id, :column => :client_priority, :list_name => :client
  sortable :scope => :project_id, :column => :developer_priority, :list_name => :developer
end

@todo = Todo.create(:description => 'do something', :project_id => 1)
@todo_2 = Todo.create(:description => 'do something else', :project_id => 1)

@todo.client_priority # 1
@todo.developer_priority # 1
@todo_2.client_priority # 2
@todo_2.developer_priority # 2

@todo.move_down!(:client)
@todo_2.reload

@todo.client_priority # 2
@todo.developer_priority # 1
@todo_2.client_priority # 1
@todo_2.developer_priority # 2

Switching scopeΒΆ ↑

Any attributes specified as a :scope that are changed on an item cause the item to automatically switch lists when it is saved

class Todo < ActiveRecord::Base
  # schema
  #   id           :integer
  #   project_id   :integer
  #   description  :string
  #   position     :integer
  sortable :scope => :project_id
end

@todo = Todo.create(:description => 'do something', :project_id => 1)
@todo_2 = Todo.create(:description => 'do something else', :project_id => 1)

@todo.position # 1
@todo_2.position # 2

@todo.project_id = 2
@todo.save
@todo_2.reload

@todo.position # 1
@todo_2.position # 1

Instance methodsΒΆ ↑

# Adds the current item to the end of the specified list and saves
#
# If the current item is already in the list, it will remove it before adding it
add_to_list!(list_name = nil)

# Returns the first item in a list associated with the current item
first_item(list_name = nil)

# Returns a boolean after determining if the current item is the first item in the specified list
first_item?(list_name = nil)

# Returns an array of items higher than the current item in the specified list
higher_items(list_name = nil)

# Returns a boolean after determining if the current item is in the specified list
in_list?(list_name = nil)

# Inserts the current item at a certain position in the specified list and saves
#
# If the current item is already in the list, it will remove it before adding it
#
# Aliased as insert_at_position!
insert_at!(position = 1, list_name = nil)

# Returns the item with a position at a certain offset to the current item's position in the specified list
#
# Example
#
#   @todo = Todo.create
#   @todo_2 = Todo.create
#   @todo.item_at_offset(1) # returns @todo_2
#
# Returns nil if an item at the specified offset could not be found
item_at_offset(offset, list_name = nil)

# Returns the last item in a list associated with the current item
last_item(list_name = nil)

# Returns a boolean after determining if the current item is the last item in the specified list
last_item?(list_name = nil)

# Returns the position of the last item in a specified list
#
# Returns 0 if there are no items in the specified list
last_position(list_name = nil)

# Returns an array of items lower than the current item in the specified list
lower_items(list_name = nil)

# Moves the current item down one position in the specified list and saves
move_down!(list_name = nil)

# Moves the current item up one position in the specified list and saves
move_up!(list_name = nil)

# Moves the current item down to the bottom of the specified list and saves
move_to_bottom!(list_name = nil)

# Moves the current item up to the top of the specified list and saves
move_to_top!(list_name = nil)

# Returns the next lower item in the specified list
next_item(list_name = nil)

# Returns the previous higher item in the specified list
previous_item(list_name = nil)

# Removes the current item from the specified list and saves
#
# This will set the :position to nil
remove_from_list!(list_name = nil)

# Returns a boolean after determining if this item has changed any attributes specified in the :scope options
sortable_scope_changed?

# Stores an array of attributes specified as a :scope that have been changed
sortable_scope_changes

ContactΒΆ ↑

Problems, comments, and suggestions all welcome: [email protected]

More Repositories

1

curl

A basic CURL wrapper for PHP
PHP
448
star
2

proxy

Allows rails applications to respond to multiple hosts/domains and proxied requests
Ruby
100
star
3

vim-promiscuous

Instant context switching using git and vim sessions.
Vim Script
73
star
4

queryable_array

Provides a simplified DSL allowing arrays of objects to be searched by their attributes
Ruby
70
star
5

subdomain_account

A rails plugin that handles subdomain accounts
Ruby
59
star
6

postgres-twitter

Experimental build of a simple "twitter" app in Postgres
PLpgSQL
57
star
7

validates_as_hostname_label

Checks for valid hostname labels (i.e. subdomains)
Ruby
49
star
8

svnignore

git style ignores with subversion
Ruby
45
star
9

interface

Implementable interfaces in ruby
Ruby
31
star
10

event_calendar

Generates html event calendars with ruby
Ruby
30
star
11

authorization

authorization for rails
Ruby
26
star
12

hattr_accessor

Allows you to define attr_accessors that reference members of a hash
Ruby
26
star
13

variables

`Variable` objects for class and instance variables
Ruby
22
star
14

abstract_class

Abstract classes in Ruby
Ruby
20
star
15

eigenclass

Eigenclasses (metaclasses) in Ruby
Ruby
17
star
16

owners

Take ownership of your code
Ruby
16
star
17

defined

Calls Module#defined after a ruby class or module is (re)defined
Ruby
15
star
18

monolith

Monolithic git repository generator
Ruby
13
star
19

sub_diff

Inspect the changes of your `String#sub` and `String#gsub` replacements
Ruby
13
star
20

phuby

A port of ruby 2.0 to native php 5.4+
PHP
11
star
21

authentication

authentication for rails
Ruby
10
star
22

tmux-git

Display git information in your tmux status line
Shell
9
star
23

kalimba

JavaScript
8
star
24

restful_rails

An extendable PHP library to communicate with RESTful rails applications
PHP
5
star
25

nestable

Allows you to nest ActiveRecord records with various strategies like tree, set, path, etc.
Ruby
4
star
26

shuber.github.com

CSS
3
star
27

cache

A simple php caching library with swappable backends
PHP
3
star
28

woot

Scrapes woot.com sites with ruby
Ruby
3
star
29

pg_brainfuck

plpgsql brainfuck implementation
2
star
30

sudoku

Solves traditional square Sudoku puzzles
Ruby
2
star
31

gitserver

node gitserver
CoffeeScript
2
star
32

respond_to_missing

Defines Object#respond_to_missing? and patches Object#respond_to?
Ruby
2
star
33

goldberg

messing around with game physics in rubygame
Ruby
1
star
34

weeports

A tiny reporting application for php >= 5.3
PHP
1
star
35

jquery-favicons

Adds favicons to links
JavaScript
1
star
36

philt

PHP
1
star
37

myspace-google_analytics

Adds Google Analytics to your profile
1
star
38

smart_home

Ruby
1
star
39

taggie

The tiniest little HTML/XML parser...using regex
Ruby
1
star