Module: ActsAsList::Mongoid::InstanceMethods

Defined in:
lib/mongoid/acts_as_list.rb

Instance Method Summary collapse

Instance Method Details

#decrement_positionObject

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



222
223
224
225
226
227
228
# File 'lib/mongoid/acts_as_list.rb', line 222

def decrement_position
  return unless in_list?

  # in_collection.where(:pos => my_position).
  adjust!(position_key => -1)
  save!
end

#do_decrement(conditions, options) ⇒ Object

conditions, { position_column => 1 }



130
131
132
# File 'lib/mongoid/acts_as_list.rb', line 130

def do_decrement( conditions, options)
  in_collection.where(conditions).adjust! position_key => -1
end

#do_increment(conditions, options) ⇒ Object



134
135
136
# File 'lib/mongoid/acts_as_list.rb', line 134

def do_increment( conditions, options)
  in_collection.where(conditions).adjust! position_key => 1
end

#first?Boolean

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

Returns:

  • (Boolean)


231
232
233
234
# File 'lib/mongoid/acts_as_list.rb', line 231

def first?
  return false unless in_list?
  my_position == 1
end

#greater_than_meObject



142
143
144
# File 'lib/mongoid/acts_as_list.rb', line 142

def greater_than_me
  { position_key.gt => my_position.to_i}
end

#higher_itemObject

Return the next higher item in the list.



244
245
246
247
248
# File 'lib/mongoid/acts_as_list.rb', line 244

def higher_item
  return nil unless in_list?
  conditions = scope_condition.merge!( less_than_me )
  order_by_position(conditions).last
end

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


259
260
261
# File 'lib/mongoid/acts_as_list.rb', line 259

def in_list?
  !my_position.nil?
end

#increment_positionObject

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



214
215
216
217
218
219
# File 'lib/mongoid/acts_as_list.rb', line 214

def increment_position
  return unless in_list?
  # in_collection.where(:pos => my_position).
  adjust!(position_key => 1) 
  save!
end

#insert_at(position = 1) ⇒ Object



146
147
148
# File 'lib/mongoid/acts_as_list.rb', line 146

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

#insert_in_list_at(position = 1) ⇒ Object

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



165
166
167
# File 'lib/mongoid/acts_as_list.rb', line 165

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

#last?Boolean

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

Returns:

  • (Boolean)


237
238
239
240
241
# File 'lib/mongoid/acts_as_list.rb', line 237

def last?
  return false unless in_list?
  bottom_pos = bottom_position_in_list
  my_position == bottom_pos
end

#less_than_meObject



138
139
140
# File 'lib/mongoid/acts_as_list.rb', line 138

def less_than_me
  { position_key.lt => my_position.to_i}
end

#lower_itemObject

Return the next lower item in the list.



251
252
253
254
255
256
# File 'lib/mongoid/acts_as_list.rb', line 251

def lower_item
  return nil unless in_list?

  conditions = scope_condition.merge!( greater_than_me )
  order_by_position(conditions).first
end

#move(command) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongoid/acts_as_list.rb', line 71

def move command
  if command.kind_of? Symbol
    case command
    when :highest, :top
      move_to_top
    when :lowest, :bottom
      move_to_bottom
    when :up, :higher
      move_higher
    when :down, :lower
      move_lower
    else
      raise ArgumentError, "unknown move command '#{command}', try one of #{self.class.move_commands_available}"
    end
  elsif command.kind_of? Hash
    other = command.values.first
    cmd = command.keys.first
    case cmd
    when :to
      move_to(other)
    when :above
      move_above(other)
    when :below
      move_below(other)
    else
      raise ArgumentError, "Hash command #{cmd.inspect} not valid, must be one of"
    end
  else
    raise ArgumentError, "move command takes either a Symbol or Hash as an argument, not a #{command.class}"
  end
end

#move_above(object) ⇒ Object



159
160
161
162
# File 'lib/mongoid/acts_as_list.rb', line 159

def move_above(object)
  new_pos = ( self == object ) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position - 1 : object.my_position)
  move_to(new_pos)
end

#move_below(object) ⇒ Object



154
155
156
157
# File 'lib/mongoid/acts_as_list.rb', line 154

def move_below(object)
  new_pos = (self == object) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position : object.my_position + 1)
  move_to(new_pos)
end

#move_higherObject

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



179
180
181
182
183
184
185
# File 'lib/mongoid/acts_as_list.rb', line 179

def move_higher
  high_item = higher_item
  return unless high_item

  high_item.increment_position
  decrement_position
end

#move_lowerObject

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



170
171
172
173
174
175
176
# File 'lib/mongoid/acts_as_list.rb', line 170

def move_lower
  low_item = lower_item
  return unless low_item

  low_item.decrement_position
  increment_position
end

#move_to(position = 1) ⇒ Object



150
151
152
# File 'lib/mongoid/acts_as_list.rb', line 150

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



189
190
191
192
193
194
# File 'lib/mongoid/acts_as_list.rb', line 189

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.



198
199
200
201
202
203
# File 'lib/mongoid/acts_as_list.rb', line 198

def move_to_top
  return unless in_list?

  increment_positions_on_higher_items
  assume_top_position
end

#order_by_position(conditions, extras = []) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mongoid/acts_as_list.rb', line 104

def order_by_position conditions, extras = []
  sub_collection = in_collection.where(conditions)
  sub_collection = if embedded?
    sub_collection.sort { |x,y| x.my_position <=> y.my_position }
  else
    sub_collection.order_by(position_key.to_sym.asc)
  end

  if !extras.empty?
    sub_collection = if embedded?
      sub_collection.sort do |x,y|
        if x.my_position == y.my_position
          x.created_at <=> y.created_at
        else
         x.my_position <=> y.my_position
        end
      end
    else
      sub_collection.order_by(extras)
    end
  end

  sub_collection
end

#remove_from_listObject

Removes the item from the list.



206
207
208
209
210
211
# File 'lib/mongoid/acts_as_list.rb', line 206

def remove_from_list
  if in_list?
    decrement_positions_on_lower_items
    set_my_position nil
  end
end

#sortObject

sorts all items in the list if two items have same position, the one created more recently goes first



265
266
267
268
269
270
271
272
# File 'lib/mongoid/acts_as_list.rb', line 265

def sort
  conditions = scope_condition
  list_items = order_by_position(conditions, :created_at.desc).to_a

  list_items.each_with_index do |list_item, index|
    list_item.set_my_position index + 1
  end
end