Module: ActsAsListAR::InstanceMethods

Defined in:
lib/acts_as_list_ar.rb,
lib/acts_as_list_ar/rails2.rb,
lib/acts_as_list_ar/rails3.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.



151
152
153
154
# File 'lib/acts_as_list_ar.rb', line 151

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)


157
158
159
160
# File 'lib/acts_as_list_ar.rb', line 157

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

#higher_itemObject

Return the next higher item in the list.



169
170
171
172
173
174
# File 'lib/acts_as_list_ar.rb', line 169

def higher_item
  # return nil unless in_list?  # http://github.com/brightspark3/acts_as_list/commit/8e55352aaa437d23a1ebdeabd5276c6dd5aad6a1      
  acts_as_list_class.find(:first, :conditions =>
    "#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC"
  )                           
end

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


185
186
187
# File 'lib/acts_as_list_ar.rb', line 185

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

#increment_positionObject

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



145
146
147
148
# File 'lib/acts_as_list_ar.rb', line 145

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



61
62
63
# File 'lib/acts_as_list_ar.rb', line 61

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

#insert_at_bottomObject

Inserts the item at the bottom of the list



66
67
68
# File 'lib/acts_as_list_ar.rb', line 66

def insert_at_bottom
  assume_bottom_position
end

#insert_into(list, options = {}) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/acts_as_list_ar.rb', line 70

def insert_into(list, options = {})
  position = options.delete(:position) || list.size
  options.each { |attr, value| self.send "#{attr}=", value }

  list.insert(position, self)
  list.each_with_index { |item, index| item.update_attribute(:position, index + 1) }
end

#last?Boolean

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

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/acts_as_list_ar.rb', line 163

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.



177
178
179
180
181
182
# File 'lib/acts_as_list_ar.rb', line 177

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

#move_higherObject

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



103
104
105
106
107
108
109
110
# File 'lib/acts_as_list_ar.rb', line 103

def move_higher
  higher = higher_item
  return unless higher
  acts_as_list_class.transaction do
    self.update_attribute(position_column, higher.send(position_column))
    higher.increment_position
  end
end

#move_lowerObject

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



93
94
95
96
97
98
99
100
# File 'lib/acts_as_list_ar.rb', line 93

def move_lower
  lower = lower_item
  return unless lower
  acts_as_list_class.transaction do
    self.update_attribute(position_column, lower.send(position_column))
    lower.decrement_position
  end
end

#move_to(position = 1) ⇒ Object



78
79
80
# File 'lib/acts_as_list_ar.rb', line 78

def move_to(position = 1)
  move_to_position(position)
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.



114
115
116
117
118
119
120
# File 'lib/acts_as_list_ar.rb', line 114

def move_to_bottom
  # return unless in_list?
  acts_as_list_class.transaction do
    decrement_positions_on_lower_items if in_list?
    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.



124
125
126
127
128
129
130
131
# File 'lib/acts_as_list_ar.rb', line 124

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

#remove_from(list, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/acts_as_list_ar.rb', line 82

def remove_from(list, options = {})
  if in_list?
    decrement_positions_on_lower_items

    self.send "#{position_column}=", nil
    options.each { |attr, value| self.send "#{attr}=", value }
    list.delete self
  end
end

#remove_from_listObject

Removes the item from the list.



134
135
136
137
138
139
140
141
142
# File 'lib/acts_as_list_ar.rb', line 134

def remove_from_list
  # if in_list?
  #   decrement_positions_on_lower_items
  #   update_attribute position_column, nil
  # end            
  return unless in_list?
  decrement_positions_on_lower_items
  update_attribute position_column, nil        
end