Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bumped version

  • Loading branch information...
commit 2dc2f2a9c2149b4555c7ffbeefc90128868f7135 1 parent 58f4de0
Arkadiy Zabazhanov authored
Showing with 88 additions and 92 deletions.
  1. +87 −91 lib/mongoid/orderable.rb
  2. +1 −1  lib/mongoid_orderable/version.rb
178 lib/mongoid/orderable.rb
View
@@ -36,128 +36,124 @@ def orderable options = {}
end
end
- module InstanceMethods
+ def move_to! target_position
+ @move_to = target_position
+ save
+ end
+ alias_method :insert_at!, :move_to!
- def move_to! target_position
- @move_to = target_position
- save
- end
- alias_method :insert_at!, :move_to!
+ def move_to target_position
+ @move_to = target_position
+ end
+ alias_method :insert_at, :move_to
- def move_to target_position
- @move_to = target_position
- end
- alias_method :insert_at, :move_to
+ def move_to= target_position
+ @move_to = target_position
+ end
+ alias_method :insert_at=, :move_to=
- def move_to= target_position
- @move_to = target_position
+ [:top, :bottom].each do |symbol|
+ define_method "move_to_#{symbol}" do
+ move_to symbol
end
- alias_method :insert_at=, :move_to=
- [:top, :bottom].each do |symbol|
- define_method "move_to_#{symbol}" do
- move_to symbol
- end
-
- define_method "move_to_#{symbol}!" do
- move_to! symbol
- end
+ define_method "move_to_#{symbol}!" do
+ move_to! symbol
end
+ end
- [:higher, :lower].each do |symbol|
- define_method "move_#{symbol}" do
- move_to symbol
- end
-
- define_method "move_#{symbol}!" do
- move_to! symbol
- end
+ [:higher, :lower].each do |symbol|
+ define_method "move_#{symbol}" do
+ move_to symbol
end
- def first?
- in_list? && orderable_position == 1
+ define_method "move_#{symbol}!" do
+ move_to! symbol
end
+ end
- def last?
- in_list? && orderable_position == bottom_orderable_position
- end
+ def first?
+ in_list? && orderable_position == 1
+ end
- def in_list?
- !orderable_position.nil?
- end
+ def last?
+ in_list? && orderable_position == bottom_orderable_position
+ end
- def add_to_list
- apply_position @move_to
- end
+ def in_list?
+ !orderable_position.nil?
+ end
- def remove_from_list
- orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column => -1)
- end
+ def add_to_list
+ apply_position @move_to
+ end
- private
+ def remove_from_list
+ orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column => -1)
+ end
- def orderable_position
- send orderable_column
- end
+private
- def orderable_position= value
- send "#{orderable_column}=", value
- end
+ def orderable_position
+ send orderable_column
+ end
- def orderable_scoped
- if embedded?
- send(metadata.inverse).send(metadata.name).orderable_scope(self)
- else
- self.class.orderable_scope(self)
- end
- end
+ def orderable_position= value
+ send "#{orderable_column}=", value
+ end
- def orderable_scope_changed?
- !orderable_scoped.where(:_id => _id).exists?
+ def orderable_scoped
+ if embedded?
+ send(metadata.inverse).send(metadata.name).orderable_scope(self)
+ else
+ self.class.orderable_scope(self)
end
+ end
- def apply_position target_position
- if persisted? && !embedded? && orderable_scope_changed?
- self.class.find(_id).remove_from_list
- self.orderable_position = nil
- end
+ def orderable_scope_changed?
+ !orderable_scoped.where(:_id => _id).exists?
+ end
- return if !target_position && in_list?
+ def apply_position target_position
+ if persisted? && !embedded? && orderable_scope_changed?
+ self.class.find(_id).remove_from_list
+ self.orderable_position = nil
+ end
- target_position = target_position_to_position target_position
+ return if !target_position && in_list?
- unless in_list?
- 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
- end
+ target_position = target_position_to_position target_position
- self.orderable_position = target_position
+ unless in_list?
+ 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
end
- def target_position_to_position target_position
- target_position = :bottom unless target_position
+ self.orderable_position = target_position
+ end
+
+ def target_position_to_position target_position
+ target_position = :bottom unless target_position
- target_position = case target_position.to_sym
- when :top then 1
- when :bottom then bottom_orderable_position
- when :higher then orderable_position.pred
- when :lower then orderable_position.next
- end unless target_position.is_a? Numeric
+ target_position = case target_position.to_sym
+ when :top then 1
+ when :bottom then bottom_orderable_position
+ when :higher then orderable_position.pred
+ when :lower then orderable_position.next
+ end unless target_position.is_a? Numeric
- target_position = 1 if target_position < 1
- target_position = bottom_orderable_position if target_position > bottom_orderable_position
- target_position
- end
+ target_position = 1 if target_position < 1
+ target_position = bottom_orderable_position if target_position > bottom_orderable_position
+ target_position
+ end
- def bottom_orderable_position
- @bottom_orderable_position = begin
- max = orderable_scoped.max(orderable_column).to_i
- in_list? ? max : max.next
- end
+ def bottom_orderable_position
+ @bottom_orderable_position = begin
+ max = orderable_scoped.max(orderable_column).to_i
+ in_list? ? max : max.next
end
-
end
end
2  lib/mongoid_orderable/version.rb
View
@@ -1,3 +1,3 @@
module MongoidOrderable
- VERSION = "0.9.0"
+ VERSION = "0.9.1"
end
Please sign in to comment.
Something went wrong with that request. Please try again.