| 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 94
94: def after_destroy
95: super
96:
97: f = Sequel[position_field]
98: list_dataset.where(f > position_value).update(f => f - 1)
99: end
The model object at the given position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 88
88: def at_position(p)
89: list_dataset.first(position_field => p)
90: 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 175
175: def before_validation
176: unless get_column_value(position_field)
177: set_column_value("#{position_field}=", list_dataset.max(position_field).to_i+1)
178: end
179: super
180: end
Find the last position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 102
102: def last_position
103: list_dataset.max(position_field).to_i
104: end
A dataset that represents the list containing this instance.
# File lib/sequel/plugins/list.rb, line 107
107: def list_dataset
108: model.scope_proc ? model.scope_proc.call(self) : model.dataset
109: 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 113
113: def move_down(n = 1)
114: move_to(position_value + n)
115: 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 119
119: def move_to(target, lp = nil)
120: current = position_value
121: if target != current
122: checked_transaction do
123: ds = list_dataset
124: op, ds = if target < current
125: target = 1 if target < 1
126: [:+, ds.where(position_field=>target...current)]
127: else
128: lp ||= last_position
129: target = lp if target > lp
130: [:-, ds.where(position_field=>(current + 1)..target)]
131: end
132: ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1))
133: update(position_field => target)
134: end
135: end
136: self
137: end
Move this instance to the bottom (last position) of the list.
# File lib/sequel/plugins/list.rb, line 140
140: def move_to_bottom
141: lp = last_position
142: move_to(lp, lp)
143: end
Move this instance to the top (first position, position 1) of the list.
# File lib/sequel/plugins/list.rb, line 146
146: def move_to_top
147: move_to(1)
148: 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 152
152: def move_up(n = 1)
153: move_to(position_value - n)
154: 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 158
158: def next(n = 1)
159: n == 0 ? self : at_position(position_value + n)
160: end
The value of the model‘s position field for this instance.
# File lib/sequel/plugins/list.rb, line 163
163: def position_value
164: get_column_value(position_field)
165: end