Module | Sequel::Plugins::List::InstanceMethods |
In: |
lib/sequel/plugins/list.rb
|
The model object at the given position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 89 89: def at_position(p) 90: list_dataset.first(position_field => p) 91: end
Find the last position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 94 94: def last_position 95: list_dataset.max(position_field).to_i 96: end
A dataset that represents the list containing this instance.
# File lib/sequel/plugins/list.rb, line 99 99: def list_dataset 100: model.scope_proc ? model.scope_proc.call(self) : model.dataset 101: end
Move this instance down the given number of places in the list, or 1 place if no argument is specified.
# File lib/sequel/plugins/list.rb, line 105 105: def move_down(n = 1) 106: move_to(position_value + n) 107: end
Move this instance to the given place in the list. Raises an exception if target is less than 1 or greater than the last position in the list.
# File lib/sequel/plugins/list.rb, line 111 111: def move_to(target, lp = nil) 112: current = position_value 113: if target != current 114: checked_transaction do 115: ds = list_dataset 116: op, ds = if target < current 117: raise(Sequel::Error, "Moving too far up (target = #{target})") if target < 1 118: [:+, ds.filter(position_field=>target...current)] 119: else 120: lp ||= last_position 121: raise(Sequel::Error, "Moving too far down (target = #{target}, last_position = #{lp})") if target > lp 122: [:-, ds.filter(position_field=>(current + 1)..target)] 123: end 124: ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1)) 125: update(position_field => target) 126: end 127: end 128: self 129: end
Move this instance to the bottom (last position) of the list.
# File lib/sequel/plugins/list.rb, line 132 132: def move_to_bottom 133: lp = last_position 134: move_to(lp, lp) 135: end
Move this instance to the top (first position, position 1) of the list.
# File lib/sequel/plugins/list.rb, line 138 138: def move_to_top 139: move_to(1) 140: end
Move this instance the given number of places up in the list, or 1 place if no argument is specified.
# File lib/sequel/plugins/list.rb, line 144 144: def move_up(n = 1) 145: move_to(position_value - n) 146: end
The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.
# File lib/sequel/plugins/list.rb, line 150 150: def next(n = 1) 151: n == 0 ? self : at_position(position_value + n) 152: end
The value of the model‘s position field for this instance.
# File lib/sequel/plugins/list.rb, line 155 155: def position_value 156: send(position_field) 157: end