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

Defined in:
lib/acts_as_list/active_record/acts/list.rb

Instance Method Summary collapse

Instance Method Details

#current_positionObject

Get the current position of the item in the list



71
72
73
74
# File 'lib/acts_as_list/active_record/acts/list.rb', line 71

def current_position
  position = send(position_column)
  position ? position.to_i : nil
end

#decrement_positionObject

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



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

def decrement_position
  return unless in_list?
  set_list_position(current_position - 1)
end

#default_positionObject



207
208
209
# File 'lib/acts_as_list/active_record/acts/list.rb', line 207

def default_position
  acts_as_list_class.column_defaults[position_column.to_s]
end

#default_position?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/acts_as_list/active_record/acts/list.rb', line 211

def default_position?
  default_position && default_position == current_position
end

#first?Boolean

Returns:

  • (Boolean)


154
155
156
157
# File 'lib/acts_as_list/active_record/acts/list.rb', line 154

def first?
  return false unless in_list?
  !higher_items(1).exists?
end

#higher_itemObject

Return the next higher item in the list.



165
166
167
168
# File 'lib/acts_as_list/active_record/acts/list.rb', line 165

def higher_item
  return nil unless in_list?
  higher_items(1).first
end

#higher_items(limit = nil) ⇒ Object

Return the next n higher items in the list selects all higher items by default



172
173
174
175
176
177
178
179
# File 'lib/acts_as_list/active_record/acts/list.rb', line 172

def higher_items(limit=nil)
  limit ||= acts_as_list_list.count
  acts_as_list_list.
    where("#{quoted_position_column_with_table_name} <= ?", current_position).
    where.not(primary_key_condition).
    reorder(acts_as_list_order_argument(:desc)).
    limit(limit)
end

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


199
200
201
# File 'lib/acts_as_list/active_record/acts/list.rb', line 199

def in_list?
  !not_in_list?
end

#increment_positionObject

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



143
144
145
146
# File 'lib/acts_as_list/active_record/acts/list.rb', line 143

def increment_position
  return unless in_list?
  set_list_position(current_position + 1)
end

#insert_at(position = acts_as_list_top) ⇒ Object

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



77
78
79
# File 'lib/acts_as_list/active_record/acts/list.rb', line 77

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

#insert_at!(position = acts_as_list_top) ⇒ Object



81
82
83
# File 'lib/acts_as_list/active_record/acts/list.rb', line 81

def insert_at!(position = acts_as_list_top)
  insert_at_position(position, true)
end

#last?Boolean

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/acts_as_list/active_record/acts/list.rb', line 159

def last?
  return false unless in_list?
  !lower_items(1).exists?
end

#lower_itemObject

Return the next lower item in the list.



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

def lower_item
  return nil unless in_list?
  lower_items(1).first
end

#lower_items(limit = nil) ⇒ Object

Return the next n lower items in the list selects all lower items by default



189
190
191
192
193
194
195
196
# File 'lib/acts_as_list/active_record/acts/list.rb', line 189

def lower_items(limit=nil)
  limit ||= acts_as_list_list.count
  acts_as_list_list.
    where("#{quoted_position_column_with_table_name} >= ?", current_position).
    where.not(primary_key_condition).
    reorder(acts_as_list_order_argument(:asc)).
    limit(limit)
end

#move_higherObject

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



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/acts_as_list/active_record/acts/list.rb', line 100

def move_higher
  return unless higher_item

  acts_as_list_class.transaction do
    if higher_item.current_position != current_position
      swap_positions_with(higher_item)
    else
      higher_item.increment_position
      decrement_position
    end
  end
end

#move_lowerObject

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



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/acts_as_list/active_record/acts/list.rb', line 86

def move_lower
  return unless lower_item

  acts_as_list_class.transaction do
    if lower_item.current_position != current_position
      swap_positions_with(lower_item)
    else
      lower_item.decrement_position
      increment_position
    end
  end
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.



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

def move_to_bottom
  return unless in_list?
  insert_at_position bottom_position_in_list.to_i
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.



122
123
124
125
# File 'lib/acts_as_list/active_record/acts/list.rb', line 122

def move_to_top
  return unless in_list?
  insert_at_position acts_as_list_top
end

#move_within_scope(scope_id) ⇒ Object

Move the item within scope. If a position within the new scope isn’t supplied, the item will be appended to the end of the list.



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

def move_within_scope(scope_id)
  send("#{scope_name}=", scope_id)
  save!
end

#not_in_list?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/acts_as_list/active_record/acts/list.rb', line 203

def not_in_list?
  current_position.nil?
end

#remove_from_listObject

Removes the item from the list.



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

def remove_from_list
  if in_list?
    decrement_positions_on_lower_items
    set_list_position(nil)
  end
end

#set_list_position(new_position, raise_exception_if_save_fails = false) ⇒ Object

Sets the new position and saves it



216
217
218
219
# File 'lib/acts_as_list/active_record/acts/list.rb', line 216

def set_list_position(new_position, raise_exception_if_save_fails=false)
  self[position_column] = new_position
  raise_exception_if_save_fails ? save! : save
end