treeoid 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +67 -0
- data/VERSION +1 -0
- data/features/step_definitions/treeoid_steps.rb +0 -0
- data/features/support/env.rb +6 -0
- data/features/treeoid.feature +9 -0
- data/lib/treeoid.rb +49 -0
- data/test/helper.rb +23 -0
- data/test/test_treeoid.rb +71 -0
- data/treeoid.gemspec +66 -0
- metadata +145 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Jeremy Weiland
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= treeoid
|
2
|
+
|
3
|
+
A tree hierarchy module for Mongoid::Document classes. Super beta.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(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)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Jeremy Weiland. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "treeoid"
|
8
|
+
gem.summary = %q{Tree structure mixin for Mongoid::Document classes}
|
9
|
+
gem.description = %q{With treeoid, you can create tree-style hierarchies for Mongoid classes. Just "include Treeoid" and you're ready to roll. You get a "parent" accessor, a "children" array, plus a scope called "hierarchically" that spits everything out in hierarchical order - perfect for front end rendering.}
|
10
|
+
gem.email = "jeremy6d@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/jeremy6d/treeoid"
|
12
|
+
gem.authors = ["Jeremy Weiland"]
|
13
|
+
gem.add_runtime_dependency "mongoid", ">= 2.0.0.beta11"
|
14
|
+
gem.add_development_dependency "thoughtbot-shoulda", "~> 2.11.1"
|
15
|
+
gem.add_development_dependency "jnunemaker-matchy", ">= 0.4.0"
|
16
|
+
gem.add_development_dependency "cucumber", "~> 0.8.5"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
Rake::TestTask.new(:test) do |test|
|
26
|
+
test.libs << 'lib' << 'test'
|
27
|
+
test.pattern = 'test/**/test_*.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'rcov/rcovtask'
|
33
|
+
Rcov::RcovTask.new do |test|
|
34
|
+
test.libs << 'test'
|
35
|
+
test.pattern = 'test/**/test_*.rb'
|
36
|
+
test.verbose = true
|
37
|
+
end
|
38
|
+
rescue LoadError
|
39
|
+
task :rcov do
|
40
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
task :test => :check_dependencies
|
45
|
+
|
46
|
+
begin
|
47
|
+
require 'cucumber/rake/task'
|
48
|
+
Cucumber::Rake::Task.new(:features)
|
49
|
+
|
50
|
+
task :features => :check_dependencies
|
51
|
+
rescue LoadError
|
52
|
+
task :features do
|
53
|
+
abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
task :default => :test
|
58
|
+
|
59
|
+
require 'rake/rdoctask'
|
60
|
+
Rake::RDocTask.new do |rdoc|
|
61
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
62
|
+
|
63
|
+
rdoc.rdoc_dir = 'rdoc'
|
64
|
+
rdoc.title = "treeoid #{version}"
|
65
|
+
rdoc.rdoc_files.include('README*')
|
66
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
67
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
File without changes
|
data/lib/treeoid.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Treeoid
|
2
|
+
def self.included(base)
|
3
|
+
base.class_eval do
|
4
|
+
field :ancestry, :type => Array
|
5
|
+
|
6
|
+
references_many :children, :class_name => base.name, :foreign_key => :parent_id
|
7
|
+
referenced_in :parent, :class_name => base.name
|
8
|
+
|
9
|
+
validate :hierarchical_integrity
|
10
|
+
before_save :set_ancestry!
|
11
|
+
after_save :set_child_ancestries!
|
12
|
+
|
13
|
+
scope :root, where(:parent_id => nil)
|
14
|
+
|
15
|
+
def has_children?
|
16
|
+
!children.empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def has_parent?
|
20
|
+
!parent.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
def hierarchical_integrity
|
25
|
+
return unless parent_id
|
26
|
+
errors.add(:parent_id, "cannot be itself") if parent_id == self.id
|
27
|
+
unless self.class.where(:ancestry => id).id(parent_id).empty?
|
28
|
+
errors.add(:parent_id, "cannot be a current child")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_ancestry!
|
33
|
+
new_path = [(parent.try(:ancestry) || parent_id), id].compact.flatten
|
34
|
+
write_attribute :ancestry, new_path
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_child_ancestries!
|
39
|
+
children.each(&:save)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
base.instance_eval do
|
44
|
+
def hierarchically
|
45
|
+
criteria.asc :ancestry
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mongoid'
|
5
|
+
require 'matchy'
|
6
|
+
require 'treeoid'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
+
|
11
|
+
Mongoid.configure do |config|
|
12
|
+
name = "treeoid_test"
|
13
|
+
host = "localhost"
|
14
|
+
config.master = Mongo::Connection.new.db(name)
|
15
|
+
end
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
def teardown
|
19
|
+
Mongoid.master.collections.reject do |c|
|
20
|
+
c.name == 'system.indexes'
|
21
|
+
end.each(&:drop)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestTreeoid < Test::Unit::TestCase
|
4
|
+
class Node
|
5
|
+
include Mongoid::Document
|
6
|
+
include Treeoid
|
7
|
+
|
8
|
+
field :title, :type => String
|
9
|
+
end
|
10
|
+
|
11
|
+
context "A treeoid hierarchy" do
|
12
|
+
setup do
|
13
|
+
@node_one = Node.create :title => "one"
|
14
|
+
@node_two = Node.create :parent => @node_one, :title => "two"
|
15
|
+
@node_three = Node.create :title => "three"
|
16
|
+
@node_four = Node.create :parent => @node_two, :title => "four"
|
17
|
+
@node_five = Node.create :parent => @node_one, :title => "five"
|
18
|
+
assert_equal 5, Node.count
|
19
|
+
end
|
20
|
+
|
21
|
+
should "be able to access all root nodes" do
|
22
|
+
Node.root.size.should == 2
|
23
|
+
Node.root.should include(@node_three, @node_one)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "be able to add a child node" do
|
27
|
+
@new_node = Node.create :title => "blah"
|
28
|
+
@new_node.children << @node_one
|
29
|
+
@new_node.save
|
30
|
+
@new_node.children.should include(@node_one)
|
31
|
+
end
|
32
|
+
|
33
|
+
should "be able to assign a parent" do
|
34
|
+
@node_one.parent = @node_three
|
35
|
+
@node_one.save
|
36
|
+
@node_one.parent.should == @node_three
|
37
|
+
end
|
38
|
+
|
39
|
+
should "not allow a node to be its own parent" do
|
40
|
+
@node_one.parent = @node_one
|
41
|
+
@node_one.should_not be_valid
|
42
|
+
@node_one.errors[:parent_id].should include("cannot be itself")
|
43
|
+
end
|
44
|
+
|
45
|
+
should "not allow a node's parent to be set to a current child" do
|
46
|
+
@node_one.parent = @node_four
|
47
|
+
@node_one.should_not be_valid
|
48
|
+
@node_one.errors[:parent_id].should include("cannot be a current child")
|
49
|
+
end
|
50
|
+
|
51
|
+
should "recalculate the paths of all children when path is set" do
|
52
|
+
@node_one.parent = @node_three
|
53
|
+
@node_one.save
|
54
|
+
@node_one.children.each do |child|
|
55
|
+
child.ancestry.first.should == @node_three.id
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
should "correctly set a root node's path correctly" do
|
60
|
+
@node_one.ancestry.should == [@node_one.id]
|
61
|
+
end
|
62
|
+
|
63
|
+
should "correctly set a child node's path" do
|
64
|
+
@node_four.ancestry.should == [@node_one, @node_two, @node_four].map { |p| p.id }
|
65
|
+
end
|
66
|
+
|
67
|
+
should "return in hierarchical order" do
|
68
|
+
Node.hierarchically.to_a.should == [@node_one, @node_two, @node_four, @node_five, @node_three]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/treeoid.gemspec
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{treeoid}
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jeremy Weiland"]
|
12
|
+
s.date = %q{2010-07-28}
|
13
|
+
s.description = %q{With treeoid, you can create tree-style hierarchies for Mongoid classes. Just "include Treeoid" and you're ready to roll. You get a "parent" accessor, a "children" array, plus a scope called "hierarchically" that spits everything out in hierarchical order - perfect for front end rendering.}
|
14
|
+
s.email = %q{jeremy6d@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"features/step_definitions/treeoid_steps.rb",
|
27
|
+
"features/support/env.rb",
|
28
|
+
"features/treeoid.feature",
|
29
|
+
"lib/treeoid.rb",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_treeoid.rb",
|
32
|
+
"treeoid.gemspec"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/jeremy6d/treeoid}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Tree structure mixin for Mongoid::Document classes}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_treeoid.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta11"])
|
50
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, ["~> 2.11.1"])
|
51
|
+
s.add_development_dependency(%q<jnunemaker-matchy>, [">= 0.4.0"])
|
52
|
+
s.add_development_dependency(%q<cucumber>, ["~> 0.8.5"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.0.beta11"])
|
55
|
+
s.add_dependency(%q<thoughtbot-shoulda>, ["~> 2.11.1"])
|
56
|
+
s.add_dependency(%q<jnunemaker-matchy>, [">= 0.4.0"])
|
57
|
+
s.add_dependency(%q<cucumber>, ["~> 0.8.5"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.0.beta11"])
|
61
|
+
s.add_dependency(%q<thoughtbot-shoulda>, ["~> 2.11.1"])
|
62
|
+
s.add_dependency(%q<jnunemaker-matchy>, [">= 0.4.0"])
|
63
|
+
s.add_dependency(%q<cucumber>, ["~> 0.8.5"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: treeoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeremy Weiland
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-28 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongoid
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: -1967355489
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta11
|
35
|
+
version: 2.0.0.beta11
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: thoughtbot-shoulda
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 33
|
47
|
+
segments:
|
48
|
+
- 2
|
49
|
+
- 11
|
50
|
+
- 1
|
51
|
+
version: 2.11.1
|
52
|
+
type: :development
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: jnunemaker-matchy
|
56
|
+
prerelease: false
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 15
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
- 4
|
66
|
+
- 0
|
67
|
+
version: 0.4.0
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id003
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: cucumber
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 53
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
- 8
|
82
|
+
- 5
|
83
|
+
version: 0.8.5
|
84
|
+
type: :development
|
85
|
+
version_requirements: *id004
|
86
|
+
description: With treeoid, you can create tree-style hierarchies for Mongoid classes. Just "include Treeoid" and you're ready to roll. You get a "parent" accessor, a "children" array, plus a scope called "hierarchically" that spits everything out in hierarchical order - perfect for front end rendering.
|
87
|
+
email: jeremy6d@gmail.com
|
88
|
+
executables: []
|
89
|
+
|
90
|
+
extensions: []
|
91
|
+
|
92
|
+
extra_rdoc_files:
|
93
|
+
- LICENSE
|
94
|
+
- README.rdoc
|
95
|
+
files:
|
96
|
+
- .document
|
97
|
+
- .gitignore
|
98
|
+
- LICENSE
|
99
|
+
- README.rdoc
|
100
|
+
- Rakefile
|
101
|
+
- VERSION
|
102
|
+
- features/step_definitions/treeoid_steps.rb
|
103
|
+
- features/support/env.rb
|
104
|
+
- features/treeoid.feature
|
105
|
+
- lib/treeoid.rb
|
106
|
+
- test/helper.rb
|
107
|
+
- test/test_treeoid.rb
|
108
|
+
- treeoid.gemspec
|
109
|
+
has_rdoc: true
|
110
|
+
homepage: http://github.com/jeremy6d/treeoid
|
111
|
+
licenses: []
|
112
|
+
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options:
|
115
|
+
- --charset=UTF-8
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 3
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
version: "0"
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
requirements: []
|
137
|
+
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.3.7
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: Tree structure mixin for Mongoid::Document classes
|
143
|
+
test_files:
|
144
|
+
- test/helper.rb
|
145
|
+
- test/test_treeoid.rb
|