4 def self.included(base)
5 base.extend(ClassMethods)
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.
14 # class TodoList < ActiveRecord::Base
15 # has_many :todo_items, :order => "position"
18 # class TodoItem < ActiveRecord::Base
19 # belongs_to :todo_list
20 # acts_as_list :scope => :todo_list
23 # todo_list.first.move_to_bottom
24 # todo_list.last.move_higher
26 # Configuration options are:
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)
37 configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
39 if configuration[:scope].is_a?(Symbol)
40 scope_condition_method = %(
42 if #{configuration[:scope].to_s}.nil?
43 "#{configuration[:scope].to_s} IS NULL"
45 "#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}"
50 scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
54 include ActiveRecord::Acts::List::InstanceMethods
56 def acts_as_list_class
61 '#{configuration[:column]}'
64 #{scope_condition_method}
66 before_destroy :remove_from_list
67 before_create :add_to_list_bottom
72 # All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works
73 # by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter
74 # lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is
75 # the first in the list of all chapters.
76 module InstanceMethods
77 # Insert the item at the given position (defaults to the top position of 1).
78 def insert_at(position = 1)
79 insert_at_position(position)
82 # Swap positions with the next lower item, if one exists.
84 return unless lower_item
86 acts_as_list_class.transaction do
87 lower_item.decrement_position
92 # Swap positions with the next higher item, if one exists.
94 return unless higher_item
96 acts_as_list_class.transaction do
97 higher_item.increment_position
102 # Move to the bottom of the list. If the item is already in the list, the items below it have their
103 # position adjusted accordingly.
105 return unless in_list?
106 acts_as_list_class.transaction do
107 decrement_positions_on_lower_items
108 assume_bottom_position
112 # Move to the top of the list. If the item is already in the list, the items above it have their
113 # position adjusted accordingly.
115 return unless in_list?
116 acts_as_list_class.transaction do
117 increment_positions_on_higher_items
122 # Removes the item from the list.
125 decrement_positions_on_lower_items
126 update_attribute position_column, nil
130 # Increase the position of this item without adjusting the rest of the list.
131 def increment_position
132 return unless in_list?
133 update_attribute position_column, self.send(position_column).to_i + 1
136 # Decrease the position of this item without adjusting the rest of the list.
137 def decrement_position
138 return unless in_list?
139 update_attribute position_column, self.send(position_column).to_i - 1
142 # Return +true+ if this object is the first in the list.
144 return false unless in_list?
145 self.send(position_column) == 1
148 # Return +true+ if this object is the last in the list.
150 return false unless in_list?
151 self.send(position_column) == bottom_position_in_list
154 # Return the next higher item in the list.
156 return nil unless in_list?
157 acts_as_list_class.find(:first, :conditions =>
158 "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
162 # Return the next lower item in the list.
164 return nil unless in_list?
165 acts_as_list_class.find(:first, :conditions =>
166 "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
170 # Test if this record is in a list
172 !send(position_column).nil?
177 increment_positions_on_all_items
180 def add_to_list_bottom
181 self[position_column] = bottom_position_in_list.to_i + 1
184 # Overwrite this method to define the scope of the list changes
185 def scope_condition() "1" end
187 # Returns the bottom position number in the list.
188 # bottom_position_in_list # => 2
189 def bottom_position_in_list(except = nil)
190 item = bottom_item(except)
191 item ? item.send(position_column) : 0
194 # Returns the bottom item
195 def bottom_item(except = nil)
196 conditions = scope_condition
197 conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
198 acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
201 # Forces item to assume the bottom position in the list.
202 def assume_bottom_position
203 update_attribute(position_column, bottom_position_in_list(self).to_i + 1)
206 # Forces item to assume the top position in the list.
207 def assume_top_position
208 update_attribute(position_column, 1)
211 # This has the effect of moving all the higher items up one.
212 def decrement_positions_on_higher_items(position)
213 acts_as_list_class.update_all(
214 "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} <= #{position}"
218 # This has the effect of moving all the lower items up one.
219 def decrement_positions_on_lower_items
220 return unless in_list?
221 acts_as_list_class.update_all(
222 "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
226 # This has the effect of moving all the higher items down one.
227 def increment_positions_on_higher_items
228 return unless in_list?
229 acts_as_list_class.update_all(
230 "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
234 # This has the effect of moving all the lower items down one.
235 def increment_positions_on_lower_items(position)
236 acts_as_list_class.update_all(
237 "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} >= #{position}"
241 # Increments position (<tt>position_column</tt>) of all items in the list.
242 def increment_positions_on_all_items
243 acts_as_list_class.update_all(
244 "#{position_column} = (#{position_column} + 1)", "#{scope_condition}"
248 def insert_at_position(position)
250 increment_positions_on_lower_items(position)
251 self.update_attribute(position_column, position)