Module: MongoMapper::Plugins::ActsAsList::InstanceMethods

Defined in:
lib/mongo_mapper/plugins/acts_as_list.rb

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



86
87
88
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 86

def <=>(other)
  self[position_column] <=> other[position_column]
end

#decrement_positionObject

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



146
147
148
149
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 146

def decrement_position
  return unless in_list?
  update_position( self.send(position_column).to_i-1 )
end

#first?Boolean

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

Returns:

  • (Boolean)


152
153
154
155
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 152

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

#higher_itemObject

Return the next higher item in the list.



164
165
166
167
168
169
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 164

def higher_item
  return nil unless in_list?
  conditions = scope_condition
  conditions.merge!( { position_column.to_sym => send(position_column).to_i-1 } )
  acts_as_list_class.where(conditions).first
end

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


180
181
182
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 180

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

#increment_positionObject

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



140
141
142
143
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 140

def increment_position
  return unless in_list?
  update_position( 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).



91
92
93
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 91

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

#last?Boolean

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

Returns:

  • (Boolean)


158
159
160
161
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 158

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.



172
173
174
175
176
177
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 172

def lower_item
  return nil unless in_list?
  conditions = scope_condition
  conditions.merge!( { position_column.to_sym => send(position_column).to_i+1 } )
  acts_as_list_class.where(conditions).first
end

#move_higherObject

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



103
104
105
106
107
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 103

def move_higher
  return unless higher_item
  higher_item.increment_position
  decrement_position
end

#move_lowerObject

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



96
97
98
99
100
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 96

def move_lower
  return unless lower_item
  lower_item.decrement_position
  increment_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.



111
112
113
114
115
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 111

def move_to_bottom
  return unless in_list?
  decrement_positions_on_lower_items
  assume_bottom_position
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.



119
120
121
122
123
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 119

def move_to_top
  return unless in_list?
  increment_positions_on_higher_items
  assume_top_position
end

#remove_from_listObject

Removes the item from the list.



132
133
134
135
136
137
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 132

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

#update_position(value = nil) ⇒ Object



125
126
127
128
129
# File 'lib/mongo_mapper/plugins/acts_as_list.rb', line 125

def update_position(value=nil)
  # update_attribute position_column, value
  self.set( { position_column.to_sym => value } )
  self[position_column] = value
end