Module: Sequel::Plugins::List::InstanceMethods
- Defined in:
- lib/sequel/plugins/list.rb
Instance Method Summary collapse
-
#after_destroy ⇒ Object
When destroying an instance, move all entries after the instance down one position, so that there aren’t any gaps.
-
#at_position(p) ⇒ Object
The model object at the given position in the list containing this instance.
-
#before_validation ⇒ Object
Set the value of the position_field to the maximum value plus 1 unless the position field already has a value.
-
#last_position ⇒ Object
Find the last position in the list containing this instance.
-
#list_dataset ⇒ Object
A dataset that represents the list containing this instance.
-
#move_down(n = 1) ⇒ Object
Move this instance down the given number of places in the list, or 1 place if no argument is specified.
-
#move_to(target, lp = nil) ⇒ Object
Move this instance to the given place in the list.
-
#move_to_bottom ⇒ Object
Move this instance to the bottom (last position) of the list.
-
#move_to_top ⇒ Object
Move this instance to the top (first position, usually position 1) of the list.
-
#move_up(n = 1) ⇒ Object
Move this instance the given number of places up in the list, or 1 place if no argument is specified.
-
#next(n = 1) ⇒ Object
The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.
-
#position_value ⇒ Object
The value of the model’s position field for this instance.
-
#prev(n = 1) ⇒ Object
The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.
Instance Method Details
#after_destroy ⇒ Object
When destroying an instance, move all entries after the instance down one position, so that there aren’t any gaps
106 107 108 109 110 111 |
# File 'lib/sequel/plugins/list.rb', line 106 def after_destroy super f = Sequel[position_field] list_dataset.where(f > position_value).update(f => f - 1) end |
#at_position(p) ⇒ Object
The model object at the given position in the list containing this instance.
100 101 102 |
# File 'lib/sequel/plugins/list.rb', line 100 def at_position(p) list_dataset.first(position_field => p) end |
#before_validation ⇒ Object
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.
190 191 192 193 194 195 196 197 |
# File 'lib/sequel/plugins/list.rb', line 190 def before_validation unless get_column_value(position_field) current_max = list_dataset.max(position_field) value = current_max.nil? ? model.top_of_list : current_max.to_i + 1 set_column_value("#{position_field}=", value) end super end |
#last_position ⇒ Object
Find the last position in the list containing this instance.
114 115 116 |
# File 'lib/sequel/plugins/list.rb', line 114 def last_position list_dataset.max(position_field).to_i end |
#list_dataset ⇒ Object
A dataset that represents the list containing this instance.
119 120 121 |
# File 'lib/sequel/plugins/list.rb', line 119 def list_dataset model.scope_proc ? model.scope_proc.call(self) : model.dataset end |
#move_down(n = 1) ⇒ Object
Move this instance down the given number of places in the list, or 1 place if no argument is specified.
125 126 127 |
# File 'lib/sequel/plugins/list.rb', line 125 def move_down(n = 1) move_to(position_value + n) end |
#move_to(target, lp = nil) ⇒ Object
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.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/sequel/plugins/list.rb', line 133 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_to_bottom ⇒ Object
Move this instance to the bottom (last position) of the list.
154 155 156 157 |
# File 'lib/sequel/plugins/list.rb', line 154 def move_to_bottom lp = last_position move_to(lp, lp) end |
#move_to_top ⇒ Object
Move this instance to the top (first position, usually position 1) of the list.
160 161 162 |
# File 'lib/sequel/plugins/list.rb', line 160 def move_to_top move_to(model.top_of_list) end |
#move_up(n = 1) ⇒ Object
Move this instance the given number of places up in the list, or 1 place if no argument is specified.
166 167 168 |
# File 'lib/sequel/plugins/list.rb', line 166 def move_up(n = 1) move_to(position_value - n) end |
#next(n = 1) ⇒ Object
The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.
172 173 174 |
# File 'lib/sequel/plugins/list.rb', line 172 def next(n = 1) n == 0 ? self : at_position(position_value + n) end |
#position_value ⇒ Object
The value of the model’s position field for this instance.
177 178 179 |
# File 'lib/sequel/plugins/list.rb', line 177 def position_value get_column_value(position_field) end |
#prev(n = 1) ⇒ Object
The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.
183 184 185 |
# File 'lib/sequel/plugins/list.rb', line 183 def prev(n = 1) self.next(n * -1) end |