module Sequel::Plugins::List::InstanceMethods

  1. lib/sequel/plugins/list.rb

Public Instance methods

after_destroy()

When destroying an instance, move all entries after the instance down one position, so that there aren’t any gaps

[show source]
    # File lib/sequel/plugins/list.rb
106 def after_destroy
107   super
108 
109   f = Sequel[position_field]
110   list_dataset.where(f > position_value).update(f => f - 1)
111 end
at_position(p)

The model object at the given position in the list containing this instance.

[show source]
    # File lib/sequel/plugins/list.rb
100 def at_position(p)
101   list_dataset.first(position_field => p)
102 end
before_validation()

Set the value of the position_field to the maximum value plus 1 unless the position field already has a value. If the list is empty, the position will be set to the model’s top_of_list value.

[show source]
    # File lib/sequel/plugins/list.rb
190 def before_validation
191   unless get_column_value(position_field)
192     current_max = list_dataset.max(position_field)
193     value = current_max.nil? ? model.top_of_list : current_max.to_i + 1
194     set_column_value("#{position_field}=", value)
195   end
196   super
197 end
last_position()

Find the last position in the list containing this instance.

[show source]
    # File lib/sequel/plugins/list.rb
114 def last_position
115   list_dataset.max(position_field).to_i
116 end
list_dataset()

A dataset that represents the list containing this instance.

[show source]
    # File lib/sequel/plugins/list.rb
119 def list_dataset
120   model.scope_proc ? model.scope_proc.call(self) : model.dataset
121 end
move_down(n = 1)

Move this instance down the given number of places in the list, or 1 place if no argument is specified.

[show source]
    # File lib/sequel/plugins/list.rb
125 def move_down(n = 1)
126   move_to(position_value + n)
127 end
move_to(target, lp = nil)

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.

[show source]
    # File lib/sequel/plugins/list.rb
133 def move_to(target, lp = nil)
134   current = position_value
135   if target != current
136     checked_transaction do
137       ds = list_dataset
138       op, ds = if target < current
139         target = model.top_of_list if target < model.top_of_list
140         [:+, ds.where(position_field=>target...current)]
141       else
142         lp ||= last_position
143         target = lp if target > lp
144         [:-, ds.where(position_field=>(current + 1)..target)]
145       end
146       ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1))
147       update(position_field => target)
148     end
149   end
150   self
151 end
move_to_bottom()

Move this instance to the bottom (last position) of the list.

[show source]
    # File lib/sequel/plugins/list.rb
154 def move_to_bottom
155   lp = last_position 
156   move_to(lp, lp)
157 end
move_to_top()

Move this instance to the top (first position, usually position 1) of the list.

[show source]
    # File lib/sequel/plugins/list.rb
160 def move_to_top
161   move_to(model.top_of_list)
162 end
move_up(n = 1)

Move this instance the given number of places up in the list, or 1 place if no argument is specified.

[show source]
    # File lib/sequel/plugins/list.rb
166 def move_up(n = 1)
167   move_to(position_value - n) 
168 end
next(n = 1)

The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.

[show source]
    # File lib/sequel/plugins/list.rb
172 def next(n = 1)
173   n == 0 ? self : at_position(position_value + n)
174 end
position_value()

The value of the model’s position field for this instance.

[show source]
    # File lib/sequel/plugins/list.rb
177 def position_value
178   get_column_value(position_field)
179 end
prev(n = 1)

The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.

[show source]
    # File lib/sequel/plugins/list.rb
183 def prev(n = 1)
184   self.next(n * -1)
185 end