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

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



162
163
164
165
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 162

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)


168
169
170
171
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 168

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

#higher_itemObject

Return the next higher item in the list.



180
181
182
183
184
185
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 180

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

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


196
197
198
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 196

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

#increment_positionObject

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



156
157
158
159
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 156

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



79
80
81
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 79

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)


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

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.



188
189
190
191
192
193
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 188

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

#move_higherObject

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



94
95
96
97
98
99
100
101
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 94

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.



84
85
86
87
88
89
90
91
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 84

def move_lower
  return unless lower_item

  acts_as_list_class.transaction do
    lower_item.decrement_position
    increment_position
  end
end

#move_to=(pos) ⇒ Object

Move to the given position



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 124

def move_to=(pos)
  case pos.to_s
  when 'highest'
    move_to_top
  when 'higher'
    move_higher
  when 'lower'
    move_lower
  when 'lowest'
    move_to_bottom
  end
  reset_positions_in_list
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.



105
106
107
108
109
110
111
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 105

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.



115
116
117
118
119
120
121
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 115

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

Removes the item from the list.



148
149
150
151
152
153
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 148

def remove_from_list
  if in_list?
    decrement_positions_on_lower_items
    update_attribute position_column, nil
  end
end

#reset_positions_in_listObject



138
139
140
141
142
143
144
145
# File 'lib/plugins/acts_as_list/lib/active_record/acts/list.rb', line 138

def reset_positions_in_list
  acts_as_list_class.where(scope_condition).reorder("#{position_column} ASC, id ASC").each_with_index do |item, i|
    unless item.send(position_column) == (i + 1)
      acts_as_list_class.where({:id => item.id}).
        update_all({position_column => (i + 1)})
    end
  end
end