Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first try

  • Loading branch information...
commit d70bcf1f6cf298b588364a01d48eae96319dd0fd 0 parents
@scottmessinger authored
5 .document
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
21 .gitignore
@@ -0,0 +1,21 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Jakob Vidmar
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 README.rdoc
@@ -0,0 +1,47 @@
+= mongo_mapper-acts_as_tree
+
+This is an implementation of a tree structure for MongoMapper.
+
+== Installation
+
+Install as gem
+
+ gem install ramdiv-mongo_mapper_acts_as_tree
+
+== Usage
+
+Enable the tree functionality by declaring acts_as_tree on your model
+
+ class Category
+ include MongoMapper::Document
+ include MongoMapper::Acts::Tree
+
+ key :name, String
+
+ acts_as_tree
+ end
+
+The method accepts :parent_id_field, :path_field, :depth_field, :order as a hash.
+
+ :parent_id_field, :path_field, :depth_field => override the default field names
+ :order => control the order (format "_field-name_ _[asc|desc]_")
+
+Check the test_tree.rb for examples.
+
+== About bugs
+
+Use it. If you find any bugs, contact me (if possible with a test case) or patch it yourself (see next section).
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2009 Jakob Vidmar. See LICENSE for details.
54 Rakefile
@@ -0,0 +1,54 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "ramdiv-mongo_mapper_acts_as_tree"
+ gem.summary = %Q{ActsAsTree plugin for MongoMapper}
+ gem.description = %Q{Port of the old, venerable ActsAsTree with a bit of a twist}
+ gem.email = "jakob.vidmar@gmail.com"
+ gem.homepage = "http://github.com/ramdiv/mongo_mapper_acts_as_tree"
+ gem.authors = ["Jakob Vidmar"]
+ gem.add_dependency("mongo_mapper", ">= 0.6.8")
+
+ gem.add_development_dependency "shoulda", ">=2.10.2"
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+task :test => :check_dependencies
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "mongo_mapper_acts_as_tree #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  VERSION
@@ -0,0 +1 @@
+0.0.5
182 lib/mongo_mapper_acts_as_tree.rb
@@ -0,0 +1,182 @@
+require "mongo_mapper"
+
+module MongoMapper
+ module Acts
+ module Tree
+ def self.included(model)
+ model.class_eval do
+ extend InitializerMethods
+ end
+ end
+
+ module InitializerMethods
+ def acts_as_tree(options = {})
+ options = {
+ :parent_id_field => "parent_id",
+ :path_field => "path",
+ :depth_field => "depth"
+ }.merge(options)
+
+ write_inheritable_attribute :acts_as_tree_options, options
+ class_inheritable_reader :acts_as_tree_options
+
+ include InstanceMethods
+ include Fields
+ extend Fields
+ extend ClassMethods
+
+ key parent_id_field, ObjectId
+ key path_field, Array, :default => [], :index => true
+ key depth_field, Integer, :default => 0
+
+ after_save :move_children
+ validate :will_save_tree
+ before_destroy :destroy_descendants
+ end
+ end
+
+ module ClassMethods
+ def roots
+ self.all(parent_id_field => nil, :order => tree_order)
+ end
+ end
+
+ module InstanceMethods
+ def ==(other)
+ return true if other.equal?(self)
+ return true if other.instance_of?(self.class) and other._id == self._id
+ false
+ end
+
+ def parent=(var)
+ var = self.class.find(var) if var.is_a? String
+
+ if self.descendants.include? var
+ @_cyclic = true
+ else
+ @_parent = var
+ fix_position
+ @_will_move = true
+ end
+ end
+
+ def will_save_tree
+ if @_cyclic
+ errors.add(:base, "Can't be children of a descendant")
+ end
+ end
+
+ def fix_position
+ if parent.nil?
+ self[parent_id_field] = nil
+ self[path_field] = []
+ self[depth_field] = 0
+ else
+ self[parent_id_field] = parent._id
+ self[path_field] = parent[path_field] + [parent._id]
+ self[depth_field] = parent[depth_field] + 1
+ end
+ end
+
+ def parent
+ @_parent or (self[parent_id_field].nil? ? nil : self.class.find(self[parent_id_field]))
+ end
+
+ def root?
+ self[parent_id_field].nil?
+ end
+
+ def root
+ self[path_field].first.nil? ? self : self.class.find(self[path_field].first)
+ end
+
+ def ancestors
+ return [] if root?
+ self.class.find(self[path_field])
+ end
+
+ def self_and_ancestors
+ ancestors << self
+ end
+
+ def siblings
+ self.class.all(:_id => {"$ne" => self._id}, parent_id_field => self[parent_id_field], :order => tree_order)
+ end
+
+ def self_and_siblings
+ self.class.all(parent_id_field => self[parent_id_field], :order => tree_order)
+ end
+
+ def children
+ self.class.all(parent_id_field => self._id, :order => tree_order)
+ end
+
+ def descendants
+ return [] if new_record?
+ self.class.all(path_field => self._id, :order => tree_order)
+ end
+
+ def self_and_descendants
+ [self] + self.descendants
+ end
+
+ def is_ancestor_of?(other)
+ other[path_field].include?(self._id)
+ end
+
+ def is_or_is_ancestor_of?(other)
+ (other == self) or is_ancestor_of?(other)
+ end
+
+ def is_descendant_of?(other)
+ self[path_field].include?(other._id)
+ end
+
+ def is_or_is_descendant_of?(other)
+ (other == self) or is_descendant_of?(other)
+ end
+
+ def is_sibling_of?(other)
+ (other != self) and (other[parent_id_field] == self[parent_id_field])
+ end
+
+ def is_or_is_sibling_of?(other)
+ (other == self) or is_sibling_of?(other)
+ end
+
+ def move_children
+ if @_will_move
+ @_will_move = false
+ for child in self.children
+ child.fix_position
+ child.save
+ end
+ @_will_move = true
+ end
+ end
+
+ def destroy_descendants
+ self.class.destroy(self.descendants.map(&:_id))
+ end
+ end
+
+ module Fields
+ def parent_id_field
+ acts_as_tree_options[:parent_id_field]
+ end
+
+ def path_field
+ acts_as_tree_options[:path_field]
+ end
+
+ def depth_field
+ acts_as_tree_options[:depth_field]
+ end
+
+ def tree_order
+ acts_as_tree_options[:order] or ""
+ end
+ end
+ end
+ end
+end
63 ramdiv-mongo_mapper_acts_as_tree.gemspec
@@ -0,0 +1,63 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{ramdiv-mongo_mapper_acts_as_tree}
+ s.version = "0.0.5"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jakob Vidmar"]
+ s.date = %q{2010-03-27}
+ s.description = %q{Port of the old, venerable ActsAsTree with a bit of a twist}
+ s.email = %q{jakob.vidmar@gmail.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/mongo_mapper_acts_as_tree.rb",
+ "ramdiv-mongo_mapper_acts_as_tree.gemspec",
+ "test/helper.rb",
+ "test/models/category.rb",
+ "test/models/ordered_category.rb",
+ "test/test_order.rb",
+ "test/test_tree.rb"
+ ]
+ s.homepage = %q{http://github.com/ramdiv/mongo_mapper_acts_as_tree}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{ActsAsTree plugin for MongoMapper}
+ s.test_files = [
+ "test/helper.rb",
+ "test/models/category.rb",
+ "test/models/ordered_category.rb",
+ "test/test_order.rb",
+ "test/test_tree.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<mongo_mapper>, [">= 0.6.8"])
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
+ else
+ s.add_dependency(%q<mongo_mapper>, [">= 0.6.8"])
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
+ end
+ else
+ s.add_dependency(%q<mongo_mapper>, [">= 0.6.8"])
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
+ end
+end
+
32 test/helper.rb
@@ -0,0 +1,32 @@
+require 'rubygems'
+require 'test/unit'
+require 'shoulda'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'mongo_mapper'
+
+MongoMapper.database = "acts_as_tree-test"
+
+Dir["#{File.dirname(__FILE__)}/models/*.rb"].each {|file| require file}
+
+class Test::Unit::TestCase
+ # Drop all columns after each test case.
+ def teardown
+ MongoMapper.database.collections.each do |coll|
+ coll.remove
+ end
+ end
+
+ # Make sure that each test case has a teardown
+ # method to clear the db after each test.
+ def inherited(base)
+ base.define_method teardown do
+ super
+ end
+ end
+
+ def eql_arrays?(first, second)
+ first.collect(&:_id).to_set == second.collect(&:_id).to_set
+ end
+end
11 test/models/category.rb
@@ -0,0 +1,11 @@
+require "mongo_mapper"
+require "mongo_mapper_acts_as_tree"
+
+class Category
+ include MongoMapper::Document
+ include MongoMapper::Acts::Tree
+
+ key :name, String
+
+ acts_as_tree
+end
12 test/models/ordered_category.rb
@@ -0,0 +1,12 @@
+require "mongo_mapper"
+require "mongo_mapper_acts_as_tree"
+
+class OrderedCategory
+ include MongoMapper::Document
+ include MongoMapper::Acts::Tree
+
+ key :name, String
+ key :value, Integer
+
+ acts_as_tree :order => "value asc"
+end
28 test/test_order.rb
@@ -0,0 +1,28 @@
+require 'helper'
+require 'set'
+
+class TestMongomapperActsAsTree < Test::Unit::TestCase
+ context "Ordered tree" do
+ setup do
+ @root_1 = OrderedCategory.create(:name => "Root 1", :value => 2)
+ @child_1 = OrderedCategory.create(:name => "Child 1", :parent => @root_1, :value => 1)
+ @child_2 = OrderedCategory.create(:name => "Child 2", :parent => @root_1, :value => 9)
+ @child_2_1 = OrderedCategory.create(:name => "Child 2.1", :parent => @child_2, :value => 2)
+ @child_3 = OrderedCategory.create(:name => "Child 3", :parent => @root_1, :value => 5)
+ @root_2 = OrderedCategory.create(:name => "Root 2", :value => 1)
+ end
+
+ should "be in order" do
+ assert_equal OrderedCategory.roots, [@root_2, @root_1]
+
+ assert_equal @root_1.children, [@child_1, @child_3, @child_2]
+
+ assert_equal @root_1.descendants, [@child_1, @child_2_1, @child_3, @child_2]
+ assert_equal @root_1.self_and_descendants, [@root_1, @child_1, @child_2_1, @child_3, @child_2]
+
+ assert_equal @child_2.siblings, [@child_1, @child_3]
+ assert_equal @child_2.self_and_siblings, [@child_1, @child_3, @child_2]
+ assert_equal @root_1.self_and_siblings, [@root_2, @root_1]
+ end
+ end
+end
148 test/test_tree.rb
@@ -0,0 +1,148 @@
+require 'helper'
+require 'set'
+
+class TestMongomapperActsAsTree < Test::Unit::TestCase
+ context "Tree" do
+ setup do
+ @root_1 = Category.create(:name => "Root 1")
+ @child_1 = Category.create(:name => "Child 1", :parent => @root_1)
+ @child_2 = Category.create(:name => "Child 2", :parent => @root_1)
+ @child_2_1 = Category.create(:name => "Child 2.1", :parent => @child_2)
+ @child_3 = Category.create(:name => "Child 3", :parent => @root_1)
+ @root_2 = Category.create(:name => "Root 2")
+ end
+
+ should "create node from id " do
+ assert Category.create(:name => "Child 2.2", :parent => @root_1.id.to_s).parent == @root_1
+ end
+
+ should "have roots" do
+ assert eql_arrays?(Category.roots, [@root_1, @root_2])
+ end
+
+ context "node" do
+ should "have a root" do
+ assert_equal @root_1.root, @root_1
+ assert_not_equal @root_1.root, @root_2.root
+ assert_equal @root_1, @child_2_1.root
+ end
+
+ should "have ancestors" do
+ assert_equal @root_1.ancestors, []
+ assert_equal @child_2_1.ancestors, [@root_1, @child_2]
+ assert_equal @root_1.self_and_ancestors, [@root_1]
+ assert_equal @child_2_1.self_and_ancestors, [@root_1, @child_2, @child_2_1]
+ end
+
+ should "have siblings" do
+ assert eql_arrays?(@root_1.siblings, [@root_2])
+ assert eql_arrays?(@child_2.siblings, [@child_1, @child_3])
+ assert eql_arrays?(@child_2_1.siblings, [])
+ assert eql_arrays?(@root_1.self_and_siblings, [@root_1, @root_2])
+ assert eql_arrays?(@child_2.self_and_siblings, [@child_1, @child_2, @child_3])
+ assert eql_arrays?(@child_2_1.self_and_siblings, [@child_2_1])
+ end
+
+ should "set depth" do
+ assert_equal 0, @root_1.depth
+ assert_equal 1, @child_1.depth
+ assert_equal 2, @child_2_1.depth
+ end
+
+ should "have children" do
+ assert @child_2_1.children.empty?
+ assert eql_arrays?(@root_1.children, [@child_1, @child_2, @child_3])
+ end
+
+ should "have descendants" do
+ assert eql_arrays?(@root_1.descendants, [@child_1, @child_2, @child_3, @child_2_1])
+ assert eql_arrays?(@child_2.descendants, [@child_2_1])
+ assert @child_2_1.descendants.empty?
+ assert eql_arrays?(@root_1.self_and_descendants, [@root_1, @child_1, @child_2, @child_3, @child_2_1])
+ assert eql_arrays?(@child_2.self_and_descendants, [@child_2, @child_2_1])
+ assert eql_arrays?(@child_2_1.self_and_descendants, [@child_2_1])
+ end
+
+ should "be able to tell if ancestor" do
+ assert @root_1.is_ancestor_of?(@child_1)
+ assert !@root_2.is_ancestor_of?(@child_2_1)
+ assert !@child_2.is_ancestor_of?(@child_2)
+
+ assert @root_1.is_or_is_ancestor_of?(@child_1)
+ assert !@root_2.is_or_is_ancestor_of?(@child_2_1)
+ assert @child_2.is_or_is_ancestor_of?(@child_2)
+ end
+
+ should "be able to tell if descendant" do
+ assert !@root_1.is_descendant_of?(@child_1)
+ assert @child_1.is_descendant_of?(@root_1)
+ assert !@child_2.is_descendant_of?(@child_2)
+
+ assert !@root_1.is_or_is_descendant_of?(@child_1)
+ assert @child_1.is_or_is_descendant_of?(@root_1)
+ assert @child_2.is_or_is_descendant_of?(@child_2)
+ end
+
+ should "be able to tell if sibling" do
+ assert !@root_1.is_sibling_of?(@child_1)
+ assert !@child_1.is_sibling_of?(@child_1)
+ assert !@child_2.is_sibling_of?(@child_2)
+
+ assert !@root_1.is_or_is_sibling_of?(@child_1)
+ assert @child_1.is_or_is_sibling_of?(@child_2)
+ assert @child_2.is_or_is_sibling_of?(@child_2)
+ end
+
+ context "when moving" do
+ should "recalculate path and depth" do
+ @child_3.parent = @child_2
+ @child_3.save
+
+ assert @child_2.is_or_is_ancestor_of?(@child_3)
+ assert @child_3.is_or_is_descendant_of?(@child_2)
+ assert @child_2.children.include?(@child_3)
+ assert @child_2.descendants.include?(@child_3)
+ assert @child_2_1.is_or_is_sibling_of?(@child_3)
+ assert_equal 2, @child_3.depth
+ end
+
+ should "move children on save" do
+ @child_2.parent = @root_2
+
+ assert !@root_2.is_or_is_ancestor_of?(@child_2_1)
+ assert !@child_2_1.is_or_is_descendant_of?(@root_2)
+ assert !@root_2.descendants.include?(@child_2_1)
+
+ @child_2.save
+ @child_2_1.reload
+
+ assert @root_2.is_or_is_ancestor_of?(@child_2_1)
+ assert @child_2_1.is_or_is_descendant_of?(@root_2)
+ assert @root_2.descendants.include?(@child_2_1)
+ end
+
+ should "check against cyclic graph" do
+ @root_1.parent = @child_2_1
+ assert !@root_1.save
+ end
+ end
+
+ should "destroy descendants when destroyed" do
+ @child_2.destroy
+ assert_nil Category.find(@child_2_1._id)
+ end
+ end
+
+ context "root node" do
+ should "not have a parent" do
+ assert_nil @root_1.parent
+ end
+ end
+
+ context "child_node" do
+ should "have a parent" do
+ assert_equal @child_2, @child_2_1.parent
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.