module Sequel::Plugins::List::InstanceMethods

Public Instance Methods

after_destroy() click to toggle source

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

Calls superclass method
   # File lib/sequel/plugins/list.rb
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
at_position(p) click to toggle source

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

   # File lib/sequel/plugins/list.rb
88 def at_position(p)
89   list_dataset.first(position_field => p)
90 end
before_validation() click to toggle source

Set the value of the position_field to the maximum value plus 1 unless the position field already has a value.

Calls superclass method
    # File lib/sequel/plugins/list.rb
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
last_position() click to toggle source

Find the last position in the list containing this instance.

    # File lib/sequel/plugins/list.rb
102 def last_position
103   list_dataset.max(position_field).to_i
104 end
list_dataset() click to toggle source

A dataset that represents the list containing this instance.

    # File lib/sequel/plugins/list.rb
107 def list_dataset
108   model.scope_proc ? model.scope_proc.call(self) : model.dataset
109 end
move_down(n = 1) click to toggle source

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
113 def move_down(n = 1)
114   move_to(position_value + n)
115 end
move_to(target, lp = nil) click to toggle source

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
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_to_bottom() click to toggle source

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

    # File lib/sequel/plugins/list.rb
140 def move_to_bottom
141   lp = last_position 
142   move_to(lp, lp)
143 end
move_to_top() click to toggle source

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

    # File lib/sequel/plugins/list.rb
146 def move_to_top
147   move_to(1)
148 end
move_up(n = 1) click to toggle source

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
152 def move_up(n = 1)
153   move_to(position_value - n) 
154 end
next(n = 1) click to toggle source

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
158 def next(n = 1)
159   n == 0 ? self : at_position(position_value + n)
160 end
position_value() click to toggle source

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

    # File lib/sequel/plugins/list.rb
163 def position_value
164   get_column_value(position_field)
165 end
prev(n = 1) click to toggle source

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
169 def prev(n = 1)
170   self.next(n * -1)
171 end

Private Instance Methods

position_field() click to toggle source

The model's position field, an instance method for ease of use.

    # File lib/sequel/plugins/list.rb
185 def position_field
186   model.position_field
187 end