Module | ActiveRecord::Acts::List::InstanceMethods |
In: |
vendor/rails/activerecord/lib/active_record/acts/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 vendor/rails/activerecord/lib/active_record/acts/list.rb, line 135 135: def decrement_position 136: return unless in_list? 137: update_attribute position_column, self.send(position_column).to_i - 1 138: end
Return true if this object is the first in the list.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 141 141: def first? 142: return false unless in_list? 143: self.send(position_column) == 1 144: end
Return the next higher item in the list.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 153 153: def higher_item 154: return nil unless in_list? 155: acts_as_list_class.find(:first, :conditions => 156: "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}" 157: ) 158: end
Test if this record is in a list
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 169 169: def in_list? 170: !send(position_column).nil? 171: end
Increase the position of this item without adjusting the rest of the list.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 129 129: def increment_position 130: return unless in_list? 131: update_attribute position_column, self.send(position_column).to_i + 1 132: end
Insert the item at the given position (defaults to the top position of 1).
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 79 79: def insert_at(position = 1) 80: insert_at_position(position) 81: end
Return true if this object is the last in the list.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 147 147: def last? 148: return false unless in_list? 149: self.send(position_column) == bottom_position_in_list 150: end
Return the next lower item in the list.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 161 161: def lower_item 162: return nil unless in_list? 163: acts_as_list_class.find(:first, :conditions => 164: "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}" 165: ) 166: end
Swap positions with the next higher item, if one exists.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 94 94: def move_higher 95: return unless higher_item 96: 97: acts_as_list_class.transaction do 98: higher_item.increment_position 99: decrement_position 100: end 101: end
Swap positions with the next lower item, if one exists.
# File vendor/rails/activerecord/lib/active_record/acts/list.rb, line 84 84: def move_lower 85: return unless lower_item 86: 87: acts_as_list_class.transaction do 88: lower_item.decrement_position 89: increment_position 90: end 91: 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 vendor/rails/activerecord/lib/active_record/acts/list.rb, line 105 105: def move_to_bottom 106: return unless in_list? 107: acts_as_list_class.transaction do 108: decrement_positions_on_lower_items 109: assume_bottom_position 110: end 111: 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 vendor/rails/activerecord/lib/active_record/acts/list.rb, line 115 115: def move_to_top 116: return unless in_list? 117: acts_as_list_class.transaction do 118: increment_positions_on_higher_items 119: assume_top_position 120: end 121: end