Skip to content

Commit

Permalink
Merge pull request #8 from jbbarth/mongoid3-compat
Browse files Browse the repository at this point in the history
Mongoid 3.0 compatibility
  • Loading branch information
skyeagle committed Jun 9, 2012
2 parents ce0b00a + 6e9bcd4 commit 7ebe771
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 31 deletions.
19 changes: 8 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ PATH
remote: .
specs:
mongoid-ancestry (0.2.2)
bson_ext (>= 1.3)
mongoid (>= 2.0)
mongoid (= 3.0.0.rc)

GEM
remote: http://rubygems.org/
Expand All @@ -14,19 +13,17 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
bson (1.6.2)
bson_ext (1.6.2)
bson (~> 1.6.2)
builder (3.0.0)
diff-lcs (1.1.2)
i18n (0.6.0)
mongo (1.6.2)
bson (~> 1.6.2)
mongoid (2.4.7)
mongoid (3.0.0.rc)
activemodel (~> 3.1)
mongo (~> 1.3)
moped (~> 1.0.0.rc)
origin (~> 1.0.0.rc)
tzinfo (~> 0.3.22)
multi_json (1.2.0)
moped (1.0.0.rc)
multi_json (1.3.6)
origin (1.0.0.rc)
rake (0.9.2.2)
rspec (2.5.0)
rspec-core (~> 2.5.0)
Expand All @@ -36,7 +33,7 @@ GEM
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
tzinfo (0.3.32)
tzinfo (0.3.33)

PLATFORMS
ruby
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ To apply Mongoid-ancestry to any Mongoid model, follow these simple steps:

Your model is now a tree!

## Mongoid compatibility

This gem only supports Mongoid 3.x starting with version 0.3.0.

If you want to use Mongoid version 2.x, you should either use this gem in a 0.2.x version or checkout the "mongoid-2.4-stable" branch. You can ask bundler to stick with 0.2.x versions of this gem by adding this to your Gemfile: `gem 'mongoid-ancestry', '~> 0.2.2'`

## Organising records into a tree
You can use the parent attribute to organise your records into a tree. If you have the id of the record you want
to use as a parent and don't want to fetch it, you can also use `parent_id`. Like any virtual model attributes,
Expand Down Expand Up @@ -220,7 +226,7 @@ The materialised path pattern requires Mongoid-ancestry to use a `regexp` condit

## Contact and copyright

It's a fork of [original ancestry](https://github.com/stefankroes/ancestry) gem but adopted to work with Mongoid.
It's a fork of [original ancestry](https://github.com/stefankroes/ancestry) gem but adapted to work with Mongoid.

All thanks should goes to Stefan Kroes for his great work.

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid-ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def has_ancestry(opts = {})
self.ancestry_field = opts[:ancestry_field]

self.field ancestry_field, :type => String
self.index ancestry_field
self.index({ ancestry_field => 1 })

# Create orphan strategy accessor and set to option or default (writer comes from DynamicClassMethods)
cattr_reader :orphan_strategy
Expand Down Expand Up @@ -167,7 +167,7 @@ def restore_ancestry_integrity!
end
end
# ... save parent of this node in parents array if it exists
parents[node.id] = node.parent_id if exists? node.parent_id
parents[node.id] = node.parent_id if where(:_id => node.parent_id).first

# Reset parent id in array to nil if it introduces a cycle
parent = parents[node.id]
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid-ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def cast_primary_key(key)
if primary_key_type == Integer
key.to_i
elsif primary_key_type == BSON::ObjectId && key =~ /[a-z0-9]{24}/
BSON::ObjectId.convert(self, key)
BSON::ObjectId.from_string(key)
else
key
end
Expand Down
3 changes: 1 addition & 2 deletions mongoid-ancestry.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |s|
"README.md"
]

s.add_dependency('mongoid', ">= 2.0")
s.add_dependency('bson_ext', ">= 1.3")
s.add_dependency('mongoid', "= 3.0.0.rc")
end

2 changes: 1 addition & 1 deletion spec/lib/ancestry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
end

it "should have STI support" do
subject.with_model :extra_columns => {:type => :string} do |model|
subject.with_model :extra_columns => {:type => 'String'} do |model|
subclass1 = Object.const_set 'Subclass1', Class.new(model)
(class << subclass1; self; end).send(:define_method, :model_name) do
Struct.new(:human, :underscore).new 'Subclass1', 'subclass1'
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/mongoid-ancestry/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def assert_integrity_restoration model
end

it "should build ancestry from parent ids" do
subject.with_model :skip_ancestry => true, :extra_columns => {:parent_id => :integer} do |model|
subject.with_model :skip_ancestry => true, :extra_columns => {:parent_id => 'BSON::ObjectId'} do |model|
[model.create!].each do |parent1|
(Array.new(5) { model.create :parent_id => parent1.id }).each do |parent2|
(Array.new(5) { model.create :parent_id => parent2.id }).each do |parent3|
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/mongoid-ancestry/instance_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@
root1.parent = root2
root1.save!
root1.descendants.asc(:_id).map(&:to_param).should eql(descendants)
}.to change(root2.descendants, 'size').by(root1.subtree.size)
}.to change(root2.descendants, 'count').by(root1.subtree.count)

descendants = root2.descendants.asc(:_id).map(&:to_param)
expect {
root2.parent = root3
root2.save!
root2.descendants.asc(:_id).map(&:to_param).should eql(descendants)
}.to change(root3.descendants, 'size').by(root2.subtree.size)
}.to change(root3.descendants, 'count').by(root2.subtree.count)

descendants = root1.descendants.asc(:_id).map(&:to_param)
expect {
expect {
root1.parent = nil
root1.save!
root1.descendants.asc(:_id).map(&:to_param).should eql(descendants)
}.to change(root3.descendants, 'size').by(-root1.subtree.size)
}.to change(root2.descendants, 'size').by(-root1.subtree.size)
}.to change(root3.descendants, 'count').by(-root1.subtree.count)
}.to change(root2.descendants, 'count').by(-root1.subtree.count)
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/mongoid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test:
sessions:
default:
database: ancestry_test
hosts:
- localhost:27017
11 changes: 4 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@

require 'mongoid-ancestry'

Mongoid.configure do |config|
logger = Logger.new('log/test.log')
config.master = Mongo::Connection.new('localhost', 27017,
:logger => logger).db('ancestry_test')
config.logger = logger
end
Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
Mongoid.logger = Logger.new('log/test.log')

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|
config.after :each do
Mongoid.master.collections.reject { |c| c.name =~ /^system\./ }.each(&:drop)
# Drops all collections in the current environment
Mongoid::Sessions.default.drop
end
end
2 changes: 1 addition & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.with_model options = {}
TestNode.send(:include, Mongoid::Ancestry) unless skip_ancestry

extra_columns.each do |name, type|
TestNode.send :field, name, :type => type.to_s.capitalize.constantize
TestNode.send :field, name, :type => type.constantize
end unless extra_columns.nil?

TestNode.has_ancestry options unless skip_ancestry
Expand Down

0 comments on commit 7ebe771

Please sign in to comment.