Module: ActiveRecord::Acts::List::InstanceMethods

Defined in:
lib/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.



132
133
134
135
# File 'lib/active_record/acts/list.rb', line 132

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.

Returns:

  • (Boolean)


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

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

#higher_itemObject

Return the next higher item in the list.



150
151
152
153
154
155
# File 'lib/active_record/acts/list.rb', line 150

def higher_item
  return nil unless in_list?
  acts_as_list_class.find(:first, :conditions =>
    "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
  )
end

#in_list?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/active_record/acts/list.rb', line 165

def in_list?
  !send(position_column).nil?
end

#increment_positionObject

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



126
127
128
129
# File 'lib/active_record/acts/list.rb', line 126

def increment_position
  return unless in_list?
  update_attribute position_column, self.send(position_column).to_i + 1
end

#insert_at(position = 1) ⇒ Object



77
78
79
# File 'lib/active_record/acts/list.rb', line 77

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

#last?Boolean

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

Returns:

  • (Boolean)


144
145
146
147
# File 'lib/active_record/acts/list.rb', line 144

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.



158
159
160
161
162
163
# File 'lib/active_record/acts/list.rb', line 158

def lower_item
  return nil unless in_list?
  acts_as_list_class.find(:first, :conditions =>
    "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
  )
end

#move_higherObject

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



92
93
94
95
96
97
98
99
# File 'lib/active_record/acts/list.rb', line 92

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.



82
83
84
85
86
87
88
89
# File 'lib/active_record/acts/list.rb', line 82

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.



103
104
105
106
107
108
109
# File 'lib/active_record/acts/list.rb', line 103

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.



113
114
115
116
117
118
119
# File 'lib/active_record/acts/list.rb', line 113

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_listObject



121
122
123
# File 'lib/active_record/acts/list.rb', line 121

def remove_from_list
  decrement_positions_on_lower_items if in_list?
end