lib/sequel/plugins/list.rb in sequel-3.32.0 vs lib/sequel/plugins/list.rb in sequel-3.33.0

- old
+ new

@@ -37,13 +37,15 @@ # to have their own list: # # Item.plugin :list, :scope=>:user_id # # Note that using this plugin modifies the order of the model's dataset to - # sort by the position and scope fields. + # sort by the position and scope fields. Also note that this plugin is subject to + # race conditions, and is not safe when concurrent modifications are made + # to the same list. # - # Also note that unlike ruby arrays, the list plugin assumes that the + # Additionally, note that unlike ruby arrays, the list plugin assumes that the # first entry in the list has position 1, not position 0. # # Copyright (c) 2007-2010 Sharon Rosner, Wayne E. Seguin, Aman Gupta, Adrian Madrid, Jeremy Evans module List # Set the +position_field+ and +scope_proc+ attributes for the model, @@ -86,9 +88,17 @@ module InstanceMethods # The model object at the given position in the list containing this instance. 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. + def before_create + unless send(position_field) + send("#{position_field}=", list_dataset.max(position_field).to_i+1) + end end # Find the last position in the list containing this instance. def last_position list_dataset.max(position_field).to_i