module RedmineCrm::ActsAsList::List::InstanceMethods

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.

Public Instance Methods

decrement_position() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 162
def decrement_position
  return unless in_list?
  update_attribute position_column, self.send(position_column).to_i - 1
end
first?() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 168
def first?
  return false unless in_list?
  self.send(position_column) == 1
end
higher_item() click to toggle source

Return the next higher item in the list.

# File lib/redmine_crm/acts_as_list/list.rb, line 180
def higher_item
  return nil unless in_list?
  rcrm_acts_as_list_class.where(
    "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
  ).first
end
in_list?() click to toggle source

Test if this record is in a list

# File lib/redmine_crm/acts_as_list/list.rb, line 196
def in_list?
  !send(position_column).nil?
end
increment_position() click to toggle source

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

# File lib/redmine_crm/acts_as_list/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) click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 79
def insert_at(position = 1)
  insert_at_position(position)
end
last?() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 174
def last?
  return false unless in_list?
  self.send(position_column) == bottom_position_in_list
end
lower_item() click to toggle source

Return the next lower item in the list.

# File lib/redmine_crm/acts_as_list/list.rb, line 188
def lower_item
  return nil unless in_list?
  rcrm_acts_as_list_class.where(
    "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
  ).first
end
move_higher() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 94
def move_higher
  return unless higher_item

  rcrm_acts_as_list_class.transaction do
    higher_item.increment_position
    decrement_position
  end
end
move_lower() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 84
def move_lower
  return unless lower_item

  rcrm_acts_as_list_class.transaction do
    lower_item.decrement_position
    increment_position
  end
end
move_to=(pos) click to toggle source

Move to the given position

# File lib/redmine_crm/acts_as_list/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_bottom() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 105
def move_to_bottom
  return unless in_list?
  rcrm_acts_as_list_class.transaction do
    decrement_positions_on_lower_items
    assume_bottom_position
  end
end
move_to_top() click to toggle source

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

# File lib/redmine_crm/acts_as_list/list.rb, line 115
def move_to_top
  return unless in_list?
  rcrm_acts_as_list_class.transaction do
    increment_positions_on_higher_items
    assume_top_position
  end
end
remove_from_list() click to toggle source

Removes the item from the list.

# File lib/redmine_crm/acts_as_list/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_list() click to toggle source
# File lib/redmine_crm/acts_as_list/list.rb, line 138
def reset_positions_in_list
  rcrm_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)
      rcrm_acts_as_list_class.where({:id => item.id}).
        update_all({position_column => (i + 1)})
    end
  end
end

Private Instance Methods

add_to_list_bottom() click to toggle source
# File lib/redmine_crm/acts_as_list/list.rb, line 206
def add_to_list_bottom
  self[position_column] = bottom_position_in_list.to_i + 1
end
add_to_list_top() click to toggle source
# File lib/redmine_crm/acts_as_list/list.rb, line 202
def add_to_list_top
  increment_positions_on_all_items
end
assume_bottom_position() click to toggle source

Forces item to assume the bottom position in the list.

# File lib/redmine_crm/acts_as_list/list.rb, line 228
def assume_bottom_position
  update_attribute(position_column, bottom_position_in_list(self).to_i + 1)
end
assume_top_position() click to toggle source

Forces item to assume the top position in the list.

# File lib/redmine_crm/acts_as_list/list.rb, line 233
def assume_top_position
  update_attribute(position_column, 1)
end
bottom_item(except = nil) click to toggle source

Returns the bottom item

# File lib/redmine_crm/acts_as_list/list.rb, line 221
def bottom_item(except = nil)
  conditions = scope_condition
  conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
  rcrm_acts_as_list_class.where(conditions).reorder("#{position_column} DESC").first
end
bottom_position_in_list(except = nil) click to toggle source

Returns the bottom position number in the list.

bottom_position_in_list    # => 2
# File lib/redmine_crm/acts_as_list/list.rb, line 215
def bottom_position_in_list(except = nil)
  item = bottom_item(except)
  item ? item.send(position_column) : 0
end
decrement_positions_on_higher_items(position) click to toggle source

This has the effect of moving all the higher items up one.

# File lib/redmine_crm/acts_as_list/list.rb, line 238
def decrement_positions_on_higher_items(position)
  rcrm_acts_as_list_class.
    where("#{scope_condition} AND #{position_column} <= #{position}").
    update_all("#{position_column} = (#{position_column} - 1)")
end
decrement_positions_on_lower_items() click to toggle source

This has the effect of moving all the lower items up one.

# File lib/redmine_crm/acts_as_list/list.rb, line 245
def decrement_positions_on_lower_items
  return unless in_list?
  rcrm_acts_as_list_class.
    where("#{scope_condition} AND #{position_column} > #{send(position_column).to_i}").
    update_all("#{position_column} = (#{position_column} - 1)")
end
increment_positions_on_all_items() click to toggle source

Increments position (position_column) of all items in the list.

# File lib/redmine_crm/acts_as_list/list.rb, line 268
def increment_positions_on_all_items
  rcrm_acts_as_list_class.
    where("#{scope_condition}").
    update_all("#{position_column} = (#{position_column} + 1)")
end
increment_positions_on_higher_items() click to toggle source

This has the effect of moving all the higher items down one.

# File lib/redmine_crm/acts_as_list/list.rb, line 253
def increment_positions_on_higher_items
  return unless in_list?
  rcrm_acts_as_list_class.
    where("#{scope_condition} AND #{position_column} < #{send(position_column).to_i}").
    update_all("#{position_column} = (#{position_column} + 1)")
end
increment_positions_on_lower_items(position) click to toggle source

This has the effect of moving all the lower items down one.

# File lib/redmine_crm/acts_as_list/list.rb, line 261
def increment_positions_on_lower_items(position)
  rcrm_acts_as_list_class.
    where("#{scope_condition} AND #{position_column} >= #{position}").
    update_all("#{position_column} = (#{position_column} + 1)")
end
insert_at_position(position) click to toggle source
# File lib/redmine_crm/acts_as_list/list.rb, line 274
def insert_at_position(position)
  remove_from_list
  increment_positions_on_lower_items(position)
  self.update_attribute(position_column, position)
end
scope_condition() click to toggle source

Overwrite this method to define the scope of the list changes

# File lib/redmine_crm/acts_as_list/list.rb, line 211
def scope_condition() "1" end