lib/mongoid/orderable.rb in mongoid_orderable-1.2.0 vs lib/mongoid/orderable.rb in mongoid_orderable-4.0.0

- old
+ new

@@ -11,11 +11,11 @@ } configuration.merge! options if options.is_a?(Hash) configuration[:scope] = "#{configuration[:scope]}_id".to_sym if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/ - field configuration[:column], :type => Integer + field configuration[:column], orderable_field_opts(configuration) if configuration[:index] if MongoidOrderable.mongoid2? index configuration[:column] else index(configuration[:column] => 1) @@ -38,13 +38,26 @@ define_method :orderable_base do configuration[:base] end + self_class = self + define_method :orderable_inherited_class do + self_class if configuration[:inherited] + end + before_save :add_to_list after_destroy :remove_from_list end + + private + + def orderable_field_opts(configuration) + field_opts = { :type => Integer } + field_opts.merge!(configuration.slice(:as)) + field_opts + end end def move_to! target_position @move_to = target_position save @@ -96,11 +109,11 @@ def add_to_list apply_position @move_to end def remove_from_list - orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column, -1) + MongoidOrderable.inc orderable_scoped.where(orderable_column.gt => orderable_position), orderable_column, -1 end private def orderable_position @@ -113,15 +126,25 @@ def orderable_scoped if embedded? send(metadata.inverse).send(metadata.name).orderable_scope(self) else - self.class.orderable_scope(self) + (orderable_inherited_class || self.class).orderable_scope(self) end end def orderable_scope_changed? + if Mongoid.respond_to?(:unit_of_work) + Mongoid.unit_of_work do + orderable_scope_changed_query + end + else + orderable_scope_changed_query + end + end + + def orderable_scope_changed_query !orderable_scoped.where(:_id => _id).exists? end def apply_position target_position if persisted? && !embedded? && orderable_scope_changed? @@ -132,13 +155,13 @@ return if !target_position && in_list? target_position = target_position_to_position target_position unless in_list? - orderable_scoped.where(orderable_column.gte => target_position).inc(orderable_column, 1) + MongoidOrderable.inc orderable_scoped.where(orderable_column.gte => target_position), orderable_column, 1 else - orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position).inc(orderable_column, 1) if target_position < orderable_position - orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position).inc(orderable_column, -1) if target_position > orderable_position + MongoidOrderable.inc(orderable_scoped.where(orderable_column.gte => target_position, orderable_column.lt => orderable_position), orderable_column, 1) if target_position < orderable_position + MongoidOrderable.inc(orderable_scoped.where(orderable_column.gt => orderable_position, orderable_column.lte => target_position), orderable_column, -1) if target_position > orderable_position end self.orderable_position = target_position end