Skip to content
Browse files

Added :index option, source refactoring, version bump

  • Loading branch information...
1 parent 2dc2f2a commit 43a4acb641af1028242426c25c4eaca7da0e3784 @pyromaniac committed
Showing with 33 additions and 13 deletions.
  1. +3 −2 README.md
  2. +3 −0 Rakefile
  3. +5 −8 lib/mongoid/orderable.rb
  4. +1 −1 lib/mongoid_orderable/version.rb
  5. +21 −2 spec/mongoid/orderable_spec.rb
View
5 README.md
@@ -14,8 +14,7 @@ Mongoid::Orderable is a ordered list implementation for your mongoid models.
gem 'mongoid_orderable'
```
-Gem has the same api as others. Just include Mongoid::Orderable into your model.
-Also you can initialize orderable manually and specify `:scope` or `:column` options:
+Gem has the same api as others. Just include Mongoid::Orderable into your model and call `orderable` method.
```
class Item
@@ -24,8 +23,10 @@ class Item
# belongs_to :group
+ # orderable
# orderable :scope => :group, :column => :pos
# orderable :scope => lambda { |document| where(:group_id => document.group_id) }
+ # orderable :index => false # this one if you want specify indexes manually
end
```
View
3 Rakefile
@@ -1 +1,4 @@
require "bundler/gem_tasks"
+require "rspec/core/rake_task"
+
+RSpec::Core::RakeTask.new(:spec)
View
13 lib/mongoid/orderable.rb
@@ -1,23 +1,20 @@
module Mongoid::Orderable
extend ActiveSupport::Concern
- included do
- orderable
- end
-
module ClassMethods
def orderable options = {}
configuration = {
:column => :position,
+ :index => true,
:scope => nil
}
- configuration.update options if options.is_a?(Hash)
- field configuration[:column], :type => Integer
- index configuration[:column]
-
+ 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]
+
case configuration[:scope]
when Symbol then
scope :orderable_scope, lambda { |document| where(configuration[:scope] => document.send(configuration[:scope])) }
View
2 lib/mongoid_orderable/version.rb
@@ -1,3 +1,3 @@
module MongoidOrderable
- VERSION = "0.9.1"
+ VERSION = "1.0.0"
end
View
23 spec/mongoid/orderable_spec.rb
@@ -4,6 +4,8 @@
class SimpleOrderable
include Mongoid::Document
include Mongoid::Orderable
+
+ orderable
end
class ScopedGroup
@@ -21,8 +23,6 @@ class ScopedOrderable
orderable :scope => :group
end
-
-
class EmbedsOrderable
include Mongoid::Document
@@ -34,6 +34,8 @@ class EmbeddedOrderable
include Mongoid::Orderable
embedded_in :embeds_orderable
+
+ orderable
end
class CustomizedOrderable
@@ -43,6 +45,13 @@ class CustomizedOrderable
orderable :column => :pos
end
+ class NoIndexOrderable
+ include Mongoid::Document
+ include Mongoid::Orderable
+
+ orderable :index => false
+ end
+
describe SimpleOrderable do
before :each do
SimpleOrderable.delete_all
@@ -60,6 +69,10 @@ def positions
SimpleOrderable.fields['position'].options[:type].should == Integer
end
+ it 'should have index on position column' do
+ SimpleOrderable.index_options[:position].should_not be_nil
+ end
+
it 'should set proper position while creation' do
positions.should == [1, 2, 3, 4, 5]
end
@@ -267,4 +280,10 @@ def positions
end
end
+ describe NoIndexOrderable do
+ it 'should not have index on position column' do
+ NoIndexOrderable.index_options[:position].should be_nil
+ end
+ end
+
end

0 comments on commit 43a4acb

Please sign in to comment.
Something went wrong with that request. Please try again.