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

Instance Method Details

#decrement_positionObject

Decrease the position of this item without adjusting the rest of the list.



204
205
206
207
# File 'lib/acts_as_list/active_record/acts/list.rb', line 204

def decrement_position
  return unless in_list?
  set_list_position(self.send(position_column).to_i - 1)
end

#default_positionObject



266
267
268
# File 'lib/acts_as_list/active_record/acts/list.rb', line 266

def default_position
  acts_as_list_class.columns_hash[position_column.to_s].default
end

#default_position?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/acts_as_list/active_record/acts/list.rb', line 270

def default_position?
  default_position && default_position.to_i == send(position_column)
end

#first?Boolean

Return true if this object is the first in the list.

Returns:

  • (Boolean)


210
211
212
213
# File 'lib/acts_as_list/active_record/acts/list.rb', line 210

def first?
  return false unless in_list?
  self.send(position_column) == acts_as_list_top
end

#higher_itemObject

Return the next higher item in the list.



222
223
224
225
# File 'lib/acts_as_list/active_record/acts/list.rb', line 222

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



229
230
231
232
233
234
235
236
237
# File 'lib/acts_as_list/active_record/acts/list.rb', line 229

def higher_items(limit=nil)
  limit ||= acts_as_list_list.count
  position_value = send(position_column)
  acts_as_list_list.
    where("#{position_column} < ?", position_value).
    where("#{position_column} >= ?", position_value - limit).
    limit(limit).
    order("#{acts_as_list_class.table_name}.#{position_column} ASC")
end

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


258
259
260
# File 'lib/acts_as_list/active_record/acts/list.rb', line 258

def in_list?
  !not_in_list?
end

#increment_positionObject

Increase the position of this item without adjusting the rest of the list.



198
199
200
201
# File 'lib/acts_as_list/active_record/acts/list.rb', line 198

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).



138
139
140
# File 'lib/acts_as_list/active_record/acts/list.rb', line 138

def insert_at(position = acts_as_list_top)
  insert_at_position(position)
end

#last?Boolean

Return true if this object is the last in the list.

Returns:

  • (Boolean)


216
217
218
219
# File 'lib/acts_as_list/active_record/acts/list.rb', line 216

def last?
  return false unless in_list?
  self.send(position_column) == bottom_position_in_list
end

#lower_itemObject

Return the next lower item in the list.



240
241
242
243
# File 'lib/acts_as_list/active_record/acts/list.rb', line 240

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



247
248
249
250
251
252
253
254
255
# File 'lib/acts_as_list/active_record/acts/list.rb', line 247

def lower_items(limit=nil)
  limit ||= acts_as_list_list.count
  position_value = send(position_column)
  acts_as_list_list.
    where("#{position_column} > ?", position_value).
    where("#{position_column} <= ?", position_value + limit).
    limit(limit).
    order("#{acts_as_list_class.table_name}.#{position_column} ASC")
end

#move_higherObject

Swap positions with the next higher item, if one exists.



153
154
155
156
157
158
159
160
# File 'lib/acts_as_list/active_record/acts/list.rb', line 153

def move_higher
  return unless higher_item

  acts_as_list_class.transaction do
    higher_item.increment_position
    decrement_position
  end
end

#move_lowerObject

Swap positions with the next lower item, if one exists.



143
144
145
146
147
148
149
150
# File 'lib/acts_as_list/active_record/acts/list.rb', line 143

def move_lower
  return unless lower_item

  acts_as_list_class.transaction do
    lower_item.decrement_position
    increment_position
  end
end

#move_to_bottomObject

Move to the bottom of the list. If the item is already in the list, the items below it have their position adjusted accordingly.



164
165
166
167
168
169
170
# File 'lib/acts_as_list/active_record/acts/list.rb', line 164

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_topObject

Move to the top of the list. If the item is already in the list, the items above it have their position adjusted accordingly.



174
175
176
177
178
179
180
# File 'lib/acts_as_list/active_record/acts/list.rb', line 174

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.



192
193
194
195
# File 'lib/acts_as_list/active_record/acts/list.rb', line 192

def move_within_scope(scope_id)
  send("#{scope_name}=", scope_id)
  save!
end

#not_in_list?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/acts_as_list/active_record/acts/list.rb', line 262

def not_in_list?
  send(position_column).nil?
end

#remove_from_listObject

Removes the item from the list.



183
184
185
186
187
188
# File 'lib/acts_as_list/active_record/acts/list.rb', line 183

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



275
276
277
278
# File 'lib/acts_as_list/active_record/acts/list.rb', line 275

def set_list_position(new_position)
  write_attribute position_column, new_position
  save(validate: false)
end