Module | ActsAsList::InstanceMethods |
In: |
lib/acts_as_list.rb
|
All the methods available to a record that has had acts_as_list specified. Each method works by assuming the object to be the item in the list, so chapter.move_lower would move that chapter lower in the list of all chapters. Likewise, chapter.first? would return true if that chapter is the first in the list of all chapters.
Decrease the position of this item without adjusting the rest of the list.
# File lib/acts_as_list.rb, line 136 136: def decrement_position 137: return unless in_list? 138: update_attribute position_column, self.send(position_column).to_i - 1 139: end
Return true if this object is the first in the list.
# File lib/acts_as_list.rb, line 142 142: def first? 143: return false unless in_list? 144: self.send(position_column) == 1 145: end
Return the next higher item in the list.
# File lib/acts_as_list.rb, line 154 154: def higher_item 155: return nil unless in_list? 156: acts_as_list_class.find(:first, :conditions => 157: "#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC" 158: ) 159: end
Test if this record is in a list
# File lib/acts_as_list.rb, line 170 170: def in_list? 171: !send(position_column).nil? 172: end
Increase the position of this item without adjusting the rest of the list.
# File lib/acts_as_list.rb, line 130 130: def increment_position 131: return unless in_list? 132: update_attribute position_column, self.send(position_column).to_i + 1 133: end
Insert the item at the given position (defaults to the top position of 1).
# File lib/acts_as_list.rb, line 77 77: def insert_at(position = 1) 78: insert_at_position(position) 79: end
Return true if this object is the last in the list.
# File lib/acts_as_list.rb, line 148 148: def last? 149: return false unless in_list? 150: self.send(position_column) == bottom_position_in_list 151: end
Return the next lower item in the list.
# File lib/acts_as_list.rb, line 162 162: def lower_item 163: return nil unless in_list? 164: acts_as_list_class.find(:first, :conditions => 165: "#{scope_condition} AND #{position_column} > #{send(position_column).to_s}", :order => "#{position_column} ASC" 166: ) 167: end
Swap positions with the next higher item, if one exists.
# File lib/acts_as_list.rb, line 92 92: def move_higher 93: higher = higher_item 94: return unless higher 95: acts_as_list_class.transaction do 96: self.update_attribute(position_column, higher.send(position_column)) 97: higher.increment_position 98: end 99: end
Swap positions with the next lower item, if one exists.
# File lib/acts_as_list.rb, line 82 82: def move_lower 83: lower = lower_item 84: return unless lower 85: acts_as_list_class.transaction do 86: self.update_attribute(position_column, lower.send(position_column)) 87: lower.decrement_position 88: end 89: end
Move to the bottom of the list. If the item is already in the list, the items below it have their position adjusted accordingly.
# File lib/acts_as_list.rb, line 103 103: def move_to_bottom 104: return unless in_list? 105: acts_as_list_class.transaction do 106: decrement_positions_on_lower_items 107: assume_bottom_position 108: end 109: end
Move to the top of the list. If the item is already in the list, the items above it have their position adjusted accordingly.
# File lib/acts_as_list.rb, line 113 113: def move_to_top 114: return unless in_list? 115: acts_as_list_class.transaction do 116: increment_positions_on_higher_items 117: assume_top_position 118: end 119: end