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
104 def after_destroy
105   super
106 
107   f = Sequel[position_field]
108   list_dataset.where(f > position_value).update(f => f - 1)
109 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
 98 def at_position(p)
 99   list_dataset.first(position_field => p)
100 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
187 def before_validation
188   unless get_column_value(position_field)
189     set_column_value("#{position_field}=", list_dataset.max(position_field).to_i+1)
190   end
191   super
192 end
last_position() click to toggle source

Find the last position in the list containing this instance.

    # File lib/sequel/plugins/list.rb
112 def last_position
113   list_dataset.max(position_field).to_i
114 end
list_dataset() click to toggle source

A dataset that represents the list containing this instance.

    # File lib/sequel/plugins/list.rb
117 def list_dataset
118   model.scope_proc ? model.scope_proc.call(self) : model.dataset
119 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
123 def move_down(n = 1)
124   move_to(position_value + n)
125 end
move_to(target, lp = nil) click to toggle source

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
131 def move_to(target, lp = nil)
132   current = position_value
133   if target != current
134     checked_transaction do
135       ds = list_dataset
136       op, ds = if target < current
137         target = model.top_of_list if target < model.top_of_list
138         [:+, ds.where(position_field=>target...current)]
139       else
140         lp ||= last_position
141         target = lp if target > lp
142         [:-, ds.where(position_field=>(current + 1)..target)]
143       end
144       ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1))
145       update(position_field => target)
146     end
147   end
148   self
149 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
152 def move_to_bottom
153   lp = last_position 
154   move_to(lp, lp)
155 end
move_to_top() click to toggle source

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

    # File lib/sequel/plugins/list.rb
158 def move_to_top
159   move_to(model.top_of_list)
160 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
164 def move_up(n = 1)
165   move_to(position_value - n) 
166 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
170 def next(n = 1)
171   n == 0 ? self : at_position(position_value + n)
172 end
position_value() click to toggle source

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

    # File lib/sequel/plugins/list.rb
175 def position_value
176   get_column_value(position_field)
177 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
181 def prev(n = 1)
182   self.next(n * -1)
183 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
197 def position_field
198   model.position_field
199 end