lib/mongoid/orderable.rb in mongoid_orderable-1.0.0 vs lib/mongoid/orderable.rb in mongoid_orderable-1.1.0

- old
+ new

@@ -11,19 +11,26 @@ 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 - index configuration[:column] if configuration[:index] + if configuration[:index] + if MongoidOrderable.mongoid2? + index configuration[:column] + else + index(configuration[:column] => 1) + end + end case configuration[:scope] when Symbol then - scope :orderable_scope, lambda { |document| where(configuration[:scope] => document.send(configuration[:scope])) } + scope :orderable_scope, lambda { |document| + where(configuration[:scope] => document.send(configuration[:scope])) } when Proc then scope :orderable_scope, configuration[:scope] else - scope :orderable_scope, lambda { |document| where } + scope :orderable_scope, lambda { |document| where({}) } end define_method :orderable_column do configuration[:column] end @@ -84,11 +91,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) + orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column, -1) end private def orderable_position @@ -111,23 +118,23 @@ !orderable_scoped.where(:_id => _id).exists? end def apply_position target_position if persisted? && !embedded? && orderable_scope_changed? - self.class.find(_id).remove_from_list + self.class.unscoped.find(_id).remove_from_list self.orderable_position = nil end 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) + orderable_scoped.where(orderable_column.gte => target_position).inc(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 + 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 end self.orderable_position = target_position end @@ -146,10 +153,10 @@ target_position end def bottom_orderable_position @bottom_orderable_position = begin - max = orderable_scoped.max(orderable_column).to_i + max = orderable_scoped.distinct(orderable_column).map(&:to_i).max.to_i in_list? ? max : max.next end end end