module YeshuaCrm::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
Decrease the position of this item without adjusting the rest of the list.
# File lib/yeshua_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
Return true
if this object is the first in the list.
# File lib/yeshua_crm/acts_as_list/list.rb, line 168 def first? return false unless in_list? self.send(position_column) == 1 end
Return the next higher item in the list.
# File lib/yeshua_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
Test if this record is in a list
# File lib/yeshua_crm/acts_as_list/list.rb, line 196 def in_list? !send(position_column).nil? end
Increase the position of this item without adjusting the rest of the list.
# File lib/yeshua_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 the item at the given position (defaults to the top position of 1).
# File lib/yeshua_crm/acts_as_list/list.rb, line 79 def insert_at(position = 1) insert_at_position(position) end
Return true
if this object is the last in the list.
# File lib/yeshua_crm/acts_as_list/list.rb, line 174 def last? return false unless in_list? self.send(position_column) == bottom_position_in_list end
Return the next lower item in the list.
# File lib/yeshua_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
Swap positions with the next higher item, if one exists.
# File lib/yeshua_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
Swap positions with the next lower item, if one exists.
# File lib/yeshua_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 the given position
# File lib/yeshua_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 the bottom of the list. If the item is already in the list, the items below it have their position adjusted accordingly.
# File lib/yeshua_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 the top of the list. If the item is already in the list, the items above it have their position adjusted accordingly.
# File lib/yeshua_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
Removes the item from the list.
# File lib/yeshua_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
# File lib/yeshua_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
# File lib/yeshua_crm/acts_as_list/list.rb, line 206 def add_to_list_bottom self[position_column] = bottom_position_in_list.to_i + 1 end
# File lib/yeshua_crm/acts_as_list/list.rb, line 202 def add_to_list_top increment_positions_on_all_items end
Forces item to assume the bottom position in the list.
# File lib/yeshua_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
Forces item to assume the top position in the list.
# File lib/yeshua_crm/acts_as_list/list.rb, line 233 def assume_top_position update_attribute(position_column, 1) end
Returns the bottom item
# File lib/yeshua_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
Returns the bottom position number in the list.
bottom_position_in_list # => 2
# File lib/yeshua_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
This has the effect of moving all the higher items up one.
# File lib/yeshua_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
This has the effect of moving all the lower items up one.
# File lib/yeshua_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
Increments position (position_column
) of all items in the list.
# File lib/yeshua_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
This has the effect of moving all the higher items down one.
# File lib/yeshua_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
This has the effect of moving all the lower items down one.
# File lib/yeshua_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
# File lib/yeshua_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
Overwrite this method to define the scope of the list changes
# File lib/yeshua_crm/acts_as_list/list.rb, line 211 def scope_condition() "1" end