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 91 91: def at_position(p) 92: list_dataset.first(position_field => p) 93: end
Set the value of the position_field to the maximum value plus 1 unless the position field already has a value.
# File lib/sequel/plugins/list.rb, line 97 97: def before_create 98: unless send(position_field) 99: send("#{position_field}=", list_dataset.max(position_field).to_i+1) 100: end 101: super 102: end
Find the last position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 105 105: def last_position 106: list_dataset.max(position_field).to_i 107: end
A dataset that represents the list containing this instance.
# File lib/sequel/plugins/list.rb, line 110 110: def list_dataset 111: model.scope_proc ? model.scope_proc.call(self) : model.dataset 112: 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 116 116: def move_down(n = 1) 117: move_to(position_value + n) 118: 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 122 122: def move_to(target, lp = nil) 123: current = position_value 124: if target != current 125: checked_transaction do 126: ds = list_dataset 127: op, ds = if target < current 128: raise(Sequel::Error, "Moving too far up (target = #{target})") if target < 1 129: [:+, ds.filter(position_field=>target...current)] 130: else 131: lp ||= last_position 132: raise(Sequel::Error, "Moving too far down (target = #{target}, last_position = #{lp})") if target > lp 133: [:-, ds.filter(position_field=>(current + 1)..target)] 134: end 135: ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1)) 136: update(position_field => target) 137: end 138: end 139: self 140: end
Move this instance to the bottom (last position) of the list.
# File lib/sequel/plugins/list.rb, line 143 143: def move_to_bottom 144: lp = last_position 145: move_to(lp, lp) 146: end
Move this instance to the top (first position, position 1) of the list.
# File lib/sequel/plugins/list.rb, line 149 149: def move_to_top 150: move_to(1) 151: 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 155 155: def move_up(n = 1) 156: move_to(position_value - n) 157: 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 161 161: def next(n = 1) 162: n == 0 ? self : at_position(position_value + n) 163: end
The value of the model‘s position field for this instance.
# File lib/sequel/plugins/list.rb, line 166 166: def position_value 167: send(position_field) 168: end