Skip to content

Commit

Permalink
Marks parent association as optional for Mongoid 6.0.0.beta and newer.
Browse files Browse the repository at this point in the history
…Closes #63
  • Loading branch information
benedikt committed Jul 21, 2016
1 parent 560377f commit 90b4da7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A tree structure for Mongoid documents using the materialized path pattern

## Requirements

* mongoid (>= 4.0, < 6.0)
* mongoid (>= 4.0, < 7.0)

For a mongoid 3.x compatible version, please use mongoid-tree 1.0.x,
for a mongoid 2.x compatible version, please use mongoid-tree 0.7.x.
Expand Down
12 changes: 11 additions & 1 deletion lib/mongoid/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ module Tree
included do
has_many :children, :class_name => self.name, :foreign_key => :parent_id, :inverse_of => :parent, :validate => false

belongs_to :parent, :class_name => self.name, :inverse_of => :children, :index => true, :validate => false
options = {
:class_name => self.name,
:inverse_of => :children,
:index => true,
:validate => false,
:optional => true
}

options.delete(:optional) if Gem::Version.new(Mongoid::VERSION) < Gem::Version.new('6.0.0.beta')

belongs_to :parent, options

field :parent_ids, :type => Array, :default => []
index :parent_ids => 1
Expand Down
2 changes: 1 addition & 1 deletion mongoid-tree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |s|

s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE README.md Rakefile Gemfile .rspec)

s.add_runtime_dependency('mongoid', ['< 6.0', '>= 4.0'])
s.add_runtime_dependency('mongoid', ['< 7.0', '>= 4.0'])
s.add_development_dependency('rake', ['>= 0.9.2'])
s.add_development_dependency('rspec', ['~> 3.0'])
s.add_development_dependency('yard', ['~> 0.8'])
Expand Down

0 comments on commit 90b4da7

Please sign in to comment.