Skip to content

Commit d27a323

Browse files
committed
Added ActsAsList to manage queue ordering
1 parent c55998f commit d27a323

File tree

8 files changed

+832
-1
lines changed

8 files changed

+832
-1
lines changed

app/models/token.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def self.generate_slug(name)
44
end
55

66
has_many :requests, :class_name => 'TokenRequest', :inverse_of => :token,
7-
:dependent => :destroy, :order => 'created_at ASC'
7+
:dependent => :destroy, :order => 'position ASC'
88

99
before_validation :set_slug, :on => :create
1010

app/models/token_request.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class TokenRequest < ActiveRecord::Base
22
belongs_to :user
33
belongs_to :token, :inverse_of => :requests
4+
acts_as_list :scope => :token
45

56
validates_presence_of :user
67
validates_presence_of :token
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class AddPositionToTokenRequest < ActiveRecord::Migration
2+
def self.up
3+
add_column :token_requests, :position, :integer
4+
execute 'UPDATE token_requests SET position = id'
5+
end
6+
7+
def self.down
8+
remove_column :token_requests, :position
9+
end
10+
end

vendor/plugins/acts_as_list/README

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ActsAsList
2+
==========
3+
4+
This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a +position+ column defined as an integer on the mapped database table.
5+
6+
7+
Example
8+
=======
9+
10+
class TodoList < ActiveRecord::Base
11+
has_many :todo_items, :order => "position"
12+
end
13+
14+
class TodoItem < ActiveRecord::Base
15+
belongs_to :todo_list
16+
acts_as_list :scope => :todo_list
17+
end
18+
19+
todo_list.first.move_to_bottom
20+
todo_list.last.move_higher
21+
22+
23+
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'rake'
2+
require 'rake/testtask'
3+
4+
desc 'Default: run acts_as_list unit tests.'
5+
task :default => :test
6+
7+
desc 'Test the acts_as_ordered_tree plugin.'
8+
Rake::TestTask.new(:test) do |t|
9+
t.libs << 'lib'
10+
t.pattern = 'test/**/*_test.rb'
11+
t.verbose = true
12+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$:.unshift "#{File.dirname(__FILE__)}/lib"
2+
require 'active_record/acts/list'
3+
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::List }
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
module ActiveRecord
2+
module Acts #:nodoc:
3+
module List #:nodoc:
4+
def self.included(base)
5+
base.extend(ClassMethods)
6+
end
7+
8+
# This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list.
9+
# The class that has this specified needs to have a +position+ column defined as an integer on
10+
# the mapped database table.
11+
#
12+
# Todo list example:
13+
#
14+
# class TodoList < ActiveRecord::Base
15+
# has_many :todo_items, :order => "position"
16+
# end
17+
#
18+
# class TodoItem < ActiveRecord::Base
19+
# belongs_to :todo_list
20+
# acts_as_list :scope => :todo_list
21+
# end
22+
#
23+
# todo_list.first.move_to_bottom
24+
# todo_list.last.move_higher
25+
module ClassMethods
26+
# Configuration options are:
27+
#
28+
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+)
29+
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
30+
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
31+
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
32+
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
33+
def acts_as_list(options = {})
34+
configuration = { :column => "position", :scope => "1 = 1" }
35+
configuration.update(options) if options.is_a?(Hash)
36+
37+
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
38+
39+
if configuration[:scope].is_a?(Symbol)
40+
scope_condition_method = %(
41+
def scope_condition
42+
self.class.send(:sanitize_sql_hash_for_conditions, { :#{configuration[:scope].to_s} => send(:#{configuration[:scope].to_s}) })
43+
end
44+
)
45+
elsif configuration[:scope].is_a?(Array)
46+
scope_condition_method = %(
47+
def scope_condition
48+
attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column|
49+
memo[column.intern] = send(column.intern); memo
50+
end
51+
self.class.send(:sanitize_sql_hash_for_conditions, attrs)
52+
end
53+
)
54+
else
55+
scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
56+
end
57+
58+
class_eval <<-EOV
59+
include ActiveRecord::Acts::List::InstanceMethods
60+
61+
def acts_as_list_class
62+
::#{self.name}
63+
end
64+
65+
def position_column
66+
'#{configuration[:column]}'
67+
end
68+
69+
#{scope_condition_method}
70+
71+
before_destroy :decrement_positions_on_lower_items
72+
before_create :add_to_list_bottom
73+
EOV
74+
end
75+
end
76+
77+
# All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works
78+
# by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter
79+
# lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is
80+
# the first in the list of all chapters.
81+
module InstanceMethods
82+
# Insert the item at the given position (defaults to the top position of 1).
83+
def insert_at(position = 1)
84+
insert_at_position(position)
85+
end
86+
87+
# Swap positions with the next lower item, if one exists.
88+
def move_lower
89+
return unless lower_item
90+
91+
acts_as_list_class.transaction do
92+
lower_item.decrement_position
93+
increment_position
94+
end
95+
end
96+
97+
# Swap positions with the next higher item, if one exists.
98+
def move_higher
99+
return unless higher_item
100+
101+
acts_as_list_class.transaction do
102+
higher_item.increment_position
103+
decrement_position
104+
end
105+
end
106+
107+
# Move to the bottom of the list. If the item is already in the list, the items below it have their
108+
# position adjusted accordingly.
109+
def move_to_bottom
110+
return unless in_list?
111+
acts_as_list_class.transaction do
112+
decrement_positions_on_lower_items
113+
assume_bottom_position
114+
end
115+
end
116+
117+
# Move to the top of the list. If the item is already in the list, the items above it have their
118+
# position adjusted accordingly.
119+
def move_to_top
120+
return unless in_list?
121+
acts_as_list_class.transaction do
122+
increment_positions_on_higher_items
123+
assume_top_position
124+
end
125+
end
126+
127+
# Removes the item from the list.
128+
def remove_from_list
129+
if in_list?
130+
decrement_positions_on_lower_items
131+
update_attribute position_column, nil
132+
end
133+
end
134+
135+
# Increase the position of this item without adjusting the rest of the list.
136+
def increment_position
137+
return unless in_list?
138+
update_attribute position_column, self.send(position_column).to_i + 1
139+
end
140+
141+
# Decrease the position of this item without adjusting the rest of the list.
142+
def decrement_position
143+
return unless in_list?
144+
update_attribute position_column, self.send(position_column).to_i - 1
145+
end
146+
147+
# Return +true+ if this object is the first in the list.
148+
def first?
149+
return false unless in_list?
150+
self.send(position_column) == 1
151+
end
152+
153+
# Return +true+ if this object is the last in the list.
154+
def last?
155+
return false unless in_list?
156+
self.send(position_column) == bottom_position_in_list
157+
end
158+
159+
# Return the next higher item in the list.
160+
def higher_item
161+
return nil unless in_list?
162+
acts_as_list_class.find(:first, :conditions =>
163+
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
164+
)
165+
end
166+
167+
# Return the next lower item in the list.
168+
def lower_item
169+
return nil unless in_list?
170+
acts_as_list_class.find(:first, :conditions =>
171+
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
172+
)
173+
end
174+
175+
# Test if this record is in a list
176+
def in_list?
177+
!send(position_column).nil?
178+
end
179+
180+
private
181+
def add_to_list_top
182+
increment_positions_on_all_items
183+
end
184+
185+
def add_to_list_bottom
186+
self[position_column] = bottom_position_in_list.to_i + 1
187+
end
188+
189+
# Overwrite this method to define the scope of the list changes
190+
def scope_condition() "1" end
191+
192+
# Returns the bottom position number in the list.
193+
# bottom_position_in_list # => 2
194+
def bottom_position_in_list(except = nil)
195+
item = bottom_item(except)
196+
item ? item.send(position_column) : 0
197+
end
198+
199+
# Returns the bottom item
200+
def bottom_item(except = nil)
201+
conditions = scope_condition
202+
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
203+
acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
204+
end
205+
206+
# Forces item to assume the bottom position in the list.
207+
def assume_bottom_position
208+
update_attribute(position_column, bottom_position_in_list(self).to_i + 1)
209+
end
210+
211+
# Forces item to assume the top position in the list.
212+
def assume_top_position
213+
update_attribute(position_column, 1)
214+
end
215+
216+
# This has the effect of moving all the higher items up one.
217+
def decrement_positions_on_higher_items(position)
218+
acts_as_list_class.update_all(
219+
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} <= #{position}"
220+
)
221+
end
222+
223+
# This has the effect of moving all the lower items up one.
224+
def decrement_positions_on_lower_items
225+
return unless in_list?
226+
acts_as_list_class.update_all(
227+
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
228+
)
229+
end
230+
231+
# This has the effect of moving all the higher items down one.
232+
def increment_positions_on_higher_items
233+
return unless in_list?
234+
acts_as_list_class.update_all(
235+
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
236+
)
237+
end
238+
239+
# This has the effect of moving all the lower items down one.
240+
def increment_positions_on_lower_items(position)
241+
acts_as_list_class.update_all(
242+
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} >= #{position}"
243+
)
244+
end
245+
246+
# Increments position (<tt>position_column</tt>) of all items in the list.
247+
def increment_positions_on_all_items
248+
acts_as_list_class.update_all(
249+
"#{position_column} = (#{position_column} + 1)", "#{scope_condition}"
250+
)
251+
end
252+
253+
def insert_at_position(position)
254+
remove_from_list
255+
increment_positions_on_lower_items(position)
256+
self.update_attribute(position_column, position)
257+
end
258+
end
259+
end
260+
end
261+
end

0 commit comments

Comments
 (0)