Method: Sequel::Plugins::List::InstanceMethods#move_to

Defined in:
lib/sequel/plugins/list.rb

#move_to(target, lp = nil) ⇒ Object

Move this instance to the given place in the list. If lp is not given or greater than the last list position, uses the last list position. If lp is less than the top list position, uses the top list position.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sequel/plugins/list.rb', line 133

def move_to(target, lp = nil)
  current = position_value
  if target != current
    checked_transaction do
      ds = list_dataset
      op, ds = if target < current
        target = model.top_of_list if target < model.top_of_list
        [:+, ds.where(position_field=>target...current)]
      else
        lp ||= last_position
        target = lp if target > lp
        [:-, ds.where(position_field=>(current + 1)..target)]
      end
      ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1))
      update(position_field => target)
    end
  end
  self
end