Module: ActiveRecord::Acts::List::InstanceMethods
- Defined in:
- lib/acts_as_list/active_record/acts/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.
- #default_position ⇒ Object
- #default_position? ⇒ Boolean
- #first? ⇒ Boolean
-
#higher_item ⇒ Object
Return the next higher item in the list.
-
#higher_items(limit = nil) ⇒ Object
Return the next n higher items in the list selects all higher items by default.
-
#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 = acts_as_list_top) ⇒ Object
Insert the item at the given position (defaults to the top position of 1).
- #last? ⇒ Boolean
-
#lower_item ⇒ Object
Return the next lower item in the list.
-
#lower_items(limit = nil) ⇒ Object
Return the next n lower items in the list selects all lower items by default.
-
#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.
-
#move_within_scope(scope_id) ⇒ Object
Move the item within scope.
- #not_in_list? ⇒ Boolean
-
#remove_from_list ⇒ Object
Removes the item from the list.
-
#set_list_position(new_position) ⇒ Object
Sets the new position and saves it.
Instance Method Details
#decrement_position ⇒ Object
Decrease the position of this item without adjusting the rest of the list.
138 139 140 141 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 138 def decrement_position return unless in_list? set_list_position(self.send(position_column).to_i - 1) end |
#default_position ⇒ Object
198 199 200 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 198 def default_position acts_as_list_class.columns_hash[position_column.to_s].default end |
#default_position? ⇒ Boolean
202 203 204 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 202 def default_position? default_position && default_position.to_i == send(position_column) end |
#first? ⇒ Boolean
143 144 145 146 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 143 def first? return false unless in_list? !higher_items(1).exists? end |
#higher_item ⇒ Object
Return the next higher item in the list.
154 155 156 157 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 154 def higher_item return nil unless in_list? higher_items(1).first end |
#higher_items(limit = nil) ⇒ Object
Return the next n higher items in the list selects all higher items by default
161 162 163 164 165 166 167 168 169 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 161 def higher_items(limit=nil) limit ||= acts_as_list_list.count position_value = send(position_column) acts_as_list_list. where("#{quoted_position_column_with_table_name} <= ?", position_value). where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.send(self.class.primary_key)). order("#{quoted_position_column_with_table_name} DESC"). limit(limit) end |
#in_list? ⇒ Boolean
Test if this record is in a list
190 191 192 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 190 def in_list? !not_in_list? end |
#increment_position ⇒ Object
Increase the position of this item without adjusting the rest of the list.
132 133 134 135 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 132 def increment_position return unless in_list? set_list_position(self.send(position_column).to_i + 1) end |
#insert_at(position = acts_as_list_top) ⇒ Object
Insert the item at the given position (defaults to the top position of 1).
64 65 66 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 64 def insert_at(position = acts_as_list_top) insert_at_position(position) end |
#last? ⇒ Boolean
148 149 150 151 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 148 def last? return false unless in_list? !lower_items(1).exists? end |
#lower_item ⇒ Object
Return the next lower item in the list.
172 173 174 175 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 172 def lower_item return nil unless in_list? lower_items(1).first end |
#lower_items(limit = nil) ⇒ Object
Return the next n lower items in the list selects all lower items by default
179 180 181 182 183 184 185 186 187 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 179 def lower_items(limit=nil) limit ||= acts_as_list_list.count position_value = send(position_column) acts_as_list_list. where("#{quoted_position_column_with_table_name} >= ?", position_value). where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.send(self.class.primary_key)). order("#{quoted_position_column_with_table_name} ASC"). limit(limit) end |
#move_higher ⇒ Object
Swap positions with the next higher item, if one exists.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 83 def move_higher return unless higher_item acts_as_list_class.transaction do if higher_item.send(position_column) != self.send(position_column) swap_positions(higher_item, self) else higher_item.increment_position decrement_position end end end |
#move_lower ⇒ Object
Swap positions with the next lower item, if one exists.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 69 def move_lower return unless lower_item acts_as_list_class.transaction do if lower_item.send(position_column) != self.send(position_column) swap_positions(lower_item, self) else lower_item.decrement_position increment_position end 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.
98 99 100 101 102 103 104 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 98 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.
108 109 110 111 112 113 114 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 108 def move_to_top return unless in_list? acts_as_list_class.transaction do increment_positions_on_higher_items assume_top_position end end |
#move_within_scope(scope_id) ⇒ Object
Move the item within scope. If a position within the new scope isn’t supplied, the item will be appended to the end of the list.
126 127 128 129 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 126 def move_within_scope(scope_id) send("#{scope_name}=", scope_id) save! end |
#not_in_list? ⇒ Boolean
194 195 196 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 194 def not_in_list? send(position_column).nil? end |
#remove_from_list ⇒ Object
Removes the item from the list.
117 118 119 120 121 122 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 117 def remove_from_list if in_list? decrement_positions_on_lower_items set_list_position(nil) end end |
#set_list_position(new_position) ⇒ Object
Sets the new position and saves it
207 208 209 210 |
# File 'lib/acts_as_list/active_record/acts/list.rb', line 207 def set_list_position(new_position) write_attribute position_column, new_position save(validate: false) end |