Module | Sequel::Plugins::List::InstanceMethods |
In: |
lib/sequel/plugins/list.rb
|
When destroying an instance, move all entries after the instance down one position, so that there aren‘t any gaps
# File lib/sequel/plugins/list.rb, line 101 101: def after_destroy 102: super 103: 104: f = Sequel.expr(position_field) 105: list_dataset.where(f > position_value).update(f => f - 1) 106: end
The model object at the given position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 86 86: def at_position(p) 87: list_dataset.first(position_field => p) 88: 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 92 92: def before_create 93: unless get_column_value(position_field) 94: set_column_value("#{position_field}=", list_dataset.max(position_field).to_i+1) 95: end 96: super 97: end
Find the last position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 109 109: def last_position 110: list_dataset.max(position_field).to_i 111: end
A dataset that represents the list containing this instance.
# File lib/sequel/plugins/list.rb, line 114 114: def list_dataset 115: model.scope_proc ? model.scope_proc.call(self) : model.dataset 116: 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 120 120: def move_down(n = 1) 121: move_to(position_value + n) 122: 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 126 126: def move_to(target, lp = nil) 127: current = position_value 128: if target != current 129: checked_transaction do 130: ds = list_dataset 131: op, ds = if target < current 132: raise(Sequel::Error, "Moving too far up (target = #{target})") if target < 1 133: [:+, ds.filter(position_field=>target...current)] 134: else 135: lp ||= last_position 136: raise(Sequel::Error, "Moving too far down (target = #{target}, last_position = #{lp})") if target > lp 137: [:-, ds.filter(position_field=>(current + 1)..target)] 138: end 139: ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1)) 140: update(position_field => target) 141: end 142: end 143: self 144: end
Move this instance to the bottom (last position) of the list.
# File lib/sequel/plugins/list.rb, line 147 147: def move_to_bottom 148: lp = last_position 149: move_to(lp, lp) 150: end
Move this instance to the top (first position, position 1) of the list.
# File lib/sequel/plugins/list.rb, line 153 153: def move_to_top 154: move_to(1) 155: 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 159 159: def move_up(n = 1) 160: move_to(position_value - n) 161: 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 165 165: def next(n = 1) 166: n == 0 ? self : at_position(position_value + n) 167: end
The value of the model‘s position field for this instance.
# File lib/sequel/plugins/list.rb, line 170 170: def position_value 171: get_column_value(position_field) 172: end