Module: ActsAsList::InstanceMethods
- Defined in:
- lib/acts_as_list.rb
Overview
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.
Instance Method Summary collapse
-
#decrement_position ⇒ Object
Decrease the position of this item without adjusting the rest of the list.
-
#first? ⇒ Boolean
Return
true
if this object is the first in the list. -
#higher_item ⇒ Object
Return the next higher item in the list.
-
#in_list? ⇒ Boolean
Test if this record is in a list.
-
#increment_position ⇒ Object
Increase the position of this item without adjusting the rest of the list.
-
#insert_at(position = 1) ⇒ Object
Insert the item at the given position (defaults to the top position of 1).
-
#last? ⇒ Boolean
Return
true
if this object is the last in the list. -
#lower_item ⇒ Object
Return the next lower item in the list.
-
#move_higher ⇒ Object
Swap positions with the next higher item, if one exists.
-
#move_lower ⇒ Object
Swap positions with the next lower item, if one exists.
-
#move_to_bottom ⇒ Object
Move to the bottom of the list.
-
#move_to_top ⇒ Object
Move to the top of the list.
-
#remove_from_list(save = true) ⇒ Object
Removes the item from the list.
- #remove_from_list_without_saving ⇒ Object
Instance Method Details
#decrement_position ⇒ Object
Decrease the position of this item without adjusting the rest of the list.
139 140 141 142 |
# File 'lib/acts_as_list.rb', line 139 def decrement_position return unless in_list? update_attribute position_column, self.send(position_column).to_i - 1 end |
#first? ⇒ Boolean
Return true
if this object is the first in the list.
145 146 147 148 |
# File 'lib/acts_as_list.rb', line 145 def first? return false unless in_list? self.send(position_column) == 1 end |
#higher_item ⇒ Object
Return the next higher item in the list.
157 158 159 160 161 162 |
# File 'lib/acts_as_list.rb', line 157 def higher_item return nil unless in_list? acts_as_list_class.find(:first, :conditions => "#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC" ) end |
#in_list? ⇒ Boolean
Test if this record is in a list
173 174 175 |
# File 'lib/acts_as_list.rb', line 173 def in_list? !send(position_column).nil? end |
#increment_position ⇒ Object
Increase the position of this item without adjusting the rest of the list.
133 134 135 136 |
# File 'lib/acts_as_list.rb', line 133 def increment_position return unless in_list? update_attribute position_column, self.send(position_column).to_i + 1 end |
#insert_at(position = 1) ⇒ Object
Insert the item at the given position (defaults to the top position of 1).
76 77 78 |
# File 'lib/acts_as_list.rb', line 76 def insert_at(position = 1) insert_at_position(position) end |
#last? ⇒ Boolean
Return true
if this object is the last in the list.
151 152 153 154 |
# File 'lib/acts_as_list.rb', line 151 def last? return false unless in_list? self.send(position_column) == bottom_position_in_list end |
#lower_item ⇒ Object
Return the next lower item in the list.
165 166 167 168 169 170 |
# File 'lib/acts_as_list.rb', line 165 def lower_item return nil unless in_list? acts_as_list_class.find(:first, :conditions => "#{scope_condition} AND #{position_column} > #{send(position_column).to_s}", :order => "#{position_column} ASC" ) end |
#move_higher ⇒ Object
Swap positions with the next higher item, if one exists.
91 92 93 94 95 96 97 98 |
# File 'lib/acts_as_list.rb', line 91 def move_higher higher = higher_item return unless higher acts_as_list_class.transaction do self.update_attribute(position_column, higher.send(position_column)) higher.increment_position end end |
#move_lower ⇒ Object
Swap positions with the next lower item, if one exists.
81 82 83 84 85 86 87 88 |
# File 'lib/acts_as_list.rb', line 81 def move_lower lower = lower_item return unless lower acts_as_list_class.transaction do self.update_attribute(position_column, lower.send(position_column)) lower.decrement_position end end |
#move_to_bottom ⇒ Object
Move to the bottom of the list. If the item is already in the list, the items below it have their position adjusted accordingly.
102 103 104 105 106 107 108 |
# File 'lib/acts_as_list.rb', line 102 def move_to_bottom return unless in_list? acts_as_list_class.transaction do decrement_positions_on_lower_items assume_bottom_position end end |
#move_to_top ⇒ Object
Move to the top of the list. If the item is already in the list, the items above it have their position adjusted accordingly.
112 113 114 115 116 117 118 |
# File 'lib/acts_as_list.rb', line 112 def move_to_top return unless in_list? acts_as_list_class.transaction do increment_positions_on_higher_items assume_top_position end end |
#remove_from_list(save = true) ⇒ Object
Removes the item from the list.
121 122 123 124 125 126 |
# File 'lib/acts_as_list.rb', line 121 def remove_from_list(save = true) if in_list? decrement_positions_on_lower_items update_attribute(position_column, nil) if save end end |
#remove_from_list_without_saving ⇒ Object
128 129 130 |
# File 'lib/acts_as_list.rb', line 128 def remove_from_list_without_saving self.remove_from_list(false) end |