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

- (Object) decrement_position

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



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

def decrement_position
  return unless in_list?
  update_attributes! position_column => self.send(position_column).to_i - 1
end

- (Object) default_position



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

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

- (Boolean) default_position?

Returns:

  • (Boolean)


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

def default_position?
  default_position == send(position_column)
end

- (Boolean) first?

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

Returns:

  • (Boolean)


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

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

- (Object) higher_item

Return the next higher item in the list.



169
170
171
172
173
174
# File 'lib/acts_as_list/active_record/acts/list.rb', line 169

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

- (Boolean) in_list?

Test if this record is in a list

Returns:

  • (Boolean)


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

def in_list?
  !not_in_list?
end

- (Object) increment_position

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



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

def increment_position
  return unless in_list?
  update_attributes! position_column => self.send(position_column).to_i + 1
end

- (Object) insert_at(position = acts_as_list_top)

Insert the item at the given position (defaults to the top position of 1).



92
93
94
# File 'lib/acts_as_list/active_record/acts/list.rb', line 92

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

- (Boolean) last?

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

Returns:

  • (Boolean)


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

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

- (Object) lower_item

Return the next lower item in the list.



177
178
179
180
181
182
# File 'lib/acts_as_list/active_record/acts/list.rb', line 177

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

- (Object) move_higher

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



107
108
109
110
111
112
113
114
# File 'lib/acts_as_list/active_record/acts/list.rb', line 107

def move_higher
  return unless higher_item

  acts_as_list_class.transaction do
    higher_item.increment_position
    decrement_position
  end
end

- (Object) move_lower

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



97
98
99
100
101
102
103
104
# File 'lib/acts_as_list/active_record/acts/list.rb', line 97

def move_lower
  return unless lower_item

  acts_as_list_class.transaction do
    lower_item.decrement_position
    increment_position
  end
end

- (Object) move_to_bottom

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



118
119
120
121
122
123
124
# File 'lib/acts_as_list/active_record/acts/list.rb', line 118

def move_to_bottom
  return unless in_list?
  acts_as_list_class.transaction do
    decrement_positions_on_lower_items
    assume_bottom_position
  end
end

- (Object) move_to_top

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



128
129
130
131
132
133
134
# File 'lib/acts_as_list/active_record/acts/list.rb', line 128

def move_to_top
  return unless in_list?
  acts_as_list_class.transaction do
    increment_positions_on_higher_items
    assume_top_position
  end
end

- (Boolean) not_in_list?

Returns:

  • (Boolean)


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

def not_in_list?
  send(position_column).nil?
end

- (Object) remove_from_list

Removes the item from the list.



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

def remove_from_list
  if in_list?
    decrement_positions_on_lower_items
    update_attributes! position_column => nil
  end
end