module Sequel::Plugins::List::InstanceMethods
Public Instance Methods
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 104 def after_destroy super f = Sequel[position_field] list_dataset.where(f > position_value).update(f => f - 1) end
The model object at the given position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 98 def at_position(p) list_dataset.first(position_field => p) 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 187 def before_validation unless get_column_value(position_field) set_column_value("#{position_field}=", list_dataset.max(position_field).to_i+1) end super end
Find the last position in the list containing this instance.
# File lib/sequel/plugins/list.rb, line 112 def last_position list_dataset.max(position_field).to_i end
A dataset that represents the list containing this instance.
# File lib/sequel/plugins/list.rb, line 117 def list_dataset model.scope_proc ? model.scope_proc.call(self) : model.dataset 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 123 def move_down(n = 1) move_to(position_value + n) end
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.
# File lib/sequel/plugins/list.rb, line 131 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
Move this instance to the bottom (last position) of the list.
# File lib/sequel/plugins/list.rb, line 152 def move_to_bottom lp = last_position move_to(lp, lp) end
Move this instance to the top (first position, usually position 1) of the list.
# File lib/sequel/plugins/list.rb, line 158 def move_to_top move_to(model.top_of_list) 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 164 def move_up(n = 1) move_to(position_value - n) 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 170 def next(n = 1) n == 0 ? self : at_position(position_value + n) end
The value of the model's position field for this instance.
# File lib/sequel/plugins/list.rb, line 175 def position_value get_column_value(position_field) 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 181 def prev(n = 1) self.next(n * -1) end
Private Instance Methods
The model's position field, an instance method for ease of use.
# File lib/sequel/plugins/list.rb, line 197 def position_field model.position_field end