acts_as_tree-1.8 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/README.md +76 -0
- data/Rakefile +21 -0
- data/acts_as_tree.gemspec +25 -0
- data/lib/acts_as_tree.rb +169 -0
- data/lib/acts_as_tree/active_record/acts/tree.rb +15 -0
- data/lib/acts_as_tree/version.rb +3 -0
- data/test/acts_as_tree_test.rb +304 -0
- metadata +123 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# ActsAsTree [![Build Status](https://secure.travis-ci.org/amerine/acts_as_tree.png?branch=master)][travis] [![Dependency Status](https://gemnasium.com/amerine/acts_as_tree.png?travis)][gemnasium]
|
2
|
+
[travis]: (http://travis-ci.org/amerine/acts_as_tree)
|
3
|
+
[gemnasium]: (https://gemnasium.com/amerine/acts_as_tree)
|
4
|
+
|
5
|
+
|
6
|
+
ActsAsTree extends ActiveRecord to add simple support for organizing items into
|
7
|
+
parent–children relationships.
|
8
|
+
|
9
|
+
## Example
|
10
|
+
|
11
|
+
class Category < ActiveRecord::Base
|
12
|
+
include ActsAsTree
|
13
|
+
|
14
|
+
acts_as_tree order: "name"
|
15
|
+
end
|
16
|
+
|
17
|
+
root = Category.create("name" => "root")
|
18
|
+
child1 = root.children.create("name" => "child1")
|
19
|
+
subchild1 = child1.children.create("name" => "subchild1")
|
20
|
+
|
21
|
+
root.parent # => nil
|
22
|
+
child1.parent # => root
|
23
|
+
root.children # => [child1]
|
24
|
+
root.children.first.children.first # => subchild1
|
25
|
+
|
26
|
+
## Compatibility
|
27
|
+
|
28
|
+
We no longer support Ruby 1.8 or versions if Rails/ActiveRecord older than 3.0. If you're using a version of ActiveRecord older than 3.0 please use 0.1.1.
|
29
|
+
|
30
|
+
Moving forward we will do our best to support the latest versions of ActiveRecord and Ruby.
|
31
|
+
|
32
|
+
## Change Log
|
33
|
+
* 1.1.0 - April 24th, 2012
|
34
|
+
* Deprecate the ActiveRecord::Acts::Tree module in favor of ActsAsTree
|
35
|
+
* 1.0.1 - April 18th, 2012
|
36
|
+
* Include the Railtie for easier loading in Rails. Will reassess the forced module inclusion in the future.
|
37
|
+
* 1.0.0 - April 14th, 2012
|
38
|
+
* Official 1.0 release. Force users to include the ActiveRecord::Acts::Tree module.
|
39
|
+
* 0.2.0 - April 9, 2012
|
40
|
+
* Rails 3 Support
|
41
|
+
* 0.1.1 - February 3, 2010
|
42
|
+
* Bug Fixes
|
43
|
+
* 0.1.0 - October 9th, 2009
|
44
|
+
* First Gem Release
|
45
|
+
|
46
|
+
## Note on Patches/Pull Requests
|
47
|
+
|
48
|
+
1. Fork the project.
|
49
|
+
2. Make your feature addition or bug fix.
|
50
|
+
3. Add tests for it. This is important so we don't break it in a future version
|
51
|
+
unintentionally.
|
52
|
+
4. Commit, do not mess with rakefile, version, or history. (if you want to have
|
53
|
+
your own version, that is fine but bump version in a commit by itself so we can
|
54
|
+
ignore when we pull)
|
55
|
+
5. Send us a pull request. Bonus points for topic branches.
|
56
|
+
|
57
|
+
## License (MIT)
|
58
|
+
|
59
|
+
Copyright (c) 2007 David Heinemeier Hansson
|
60
|
+
|
61
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
62
|
+
this software and associated documentation files (the 'Software'), to deal in the
|
63
|
+
Software without restriction, including without limitation the rights to use,
|
64
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
65
|
+
Software, and to permit persons to whom the Software is furnished to do so,
|
66
|
+
subject to the following conditions:
|
67
|
+
|
68
|
+
The above copyright notice and this permission notice shall be included in all
|
69
|
+
copies or substantial portions of the Software.
|
70
|
+
|
71
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
72
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
73
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
74
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
75
|
+
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
76
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
desc "Run the tests."
|
4
|
+
task :test do
|
5
|
+
$: << "lib" << "test"
|
6
|
+
Dir["test/*_test.rb"].each { |f| require f[5..-4] }
|
7
|
+
end
|
8
|
+
|
9
|
+
task :default => :test
|
10
|
+
|
11
|
+
# Run the rdoc task to generate rdocs for this gem
|
12
|
+
require 'rdoc/task'
|
13
|
+
RDoc::Task.new do |rdoc|
|
14
|
+
require "acts_as_tree/version"
|
15
|
+
version = ActsAsTree::VERSION
|
16
|
+
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = "acts_as_tree-rails3 #{version}"
|
19
|
+
rdoc.rdoc_files.include('README*')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'acts_as_tree/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'acts_as_tree-1.8'
|
7
|
+
s.version = ActsAsTree::VERSION
|
8
|
+
s.authors = ['Erik Dahlstrand', 'Rails Core', 'Mark Turner', 'Swanand Pagnis']
|
9
|
+
s.email = ['erik.dahlstrand@gmail.com', 'mark@amerine.net', 'swanand.pagnis@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/dryade/acts_as_tree'
|
11
|
+
s.summary = %q{Provides a simple tree behaviour to active_record models.}
|
12
|
+
s.description = %q{A gem that adds simple support for organizing ActiveRecord models into parent–children relationships.}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
19
|
+
|
20
|
+
s.add_dependency "activerecord", ">= 3.0.0"
|
21
|
+
|
22
|
+
# Dependencies (installed via 'bundle install')...
|
23
|
+
s.add_development_dependency "sqlite3"
|
24
|
+
s.add_development_dependency "rdoc"
|
25
|
+
end
|
data/lib/acts_as_tree.rb
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'acts_as_tree/version'
|
2
|
+
|
3
|
+
module ActsAsTree
|
4
|
+
|
5
|
+
if defined? Rails::Railtie
|
6
|
+
require 'rails'
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer 'acts_as_tree.insert_into_active_record' do
|
9
|
+
ActiveSupport.on_load :active_record do
|
10
|
+
ActsAsTree::Railtie.insert
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Railtie
|
17
|
+
def self.insert
|
18
|
+
if defined?(ActiveRecord)
|
19
|
+
ActiveRecord::Base.send(:include, ActsAsTree)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included(base)
|
25
|
+
base.extend(ClassMethods)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Specify this +acts_as+ extension if you want to model a tree structure
|
29
|
+
# by providing a parent association and a children association. This
|
30
|
+
# requires that you have a foreign key column, which by default is called
|
31
|
+
# +parent_id+.
|
32
|
+
#
|
33
|
+
# class Category < ActiveRecord::Base
|
34
|
+
# include ActiveRecord::Acts:Tree
|
35
|
+
#
|
36
|
+
# acts_as_tree :order => "name"
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# Example:
|
40
|
+
# root
|
41
|
+
# \_ child1
|
42
|
+
# \_ subchild1
|
43
|
+
# \_ subchild2
|
44
|
+
#
|
45
|
+
# root = Category.create("name" => "root")
|
46
|
+
# child1 = root.children.create("name" => "child1")
|
47
|
+
# subchild1 = child1.children.create("name" => "subchild1")
|
48
|
+
#
|
49
|
+
# root.parent # => nil
|
50
|
+
# child1.parent # => root
|
51
|
+
# root.children # => [child1]
|
52
|
+
# root.children.first.children.first # => subchild1
|
53
|
+
#
|
54
|
+
# In addition to the parent and children associations, the following
|
55
|
+
# instance methods are added to the class after calling
|
56
|
+
# <tt>acts_as_tree</tt>:
|
57
|
+
# * <tt>siblings</tt> - Returns all the children of the parent, excluding
|
58
|
+
# the current node (<tt>[subchild2]</tt> when called
|
59
|
+
# on <tt>subchild1</tt>)
|
60
|
+
# * <tt>self_and_siblings</tt> - Returns all the children of the parent,
|
61
|
+
# including the current node (<tt>[subchild1, subchild2]</tt>
|
62
|
+
# when called on <tt>subchild1</tt>)
|
63
|
+
# * <tt>ancestors</tt> - Returns all the ancestors of the current node
|
64
|
+
# (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>)
|
65
|
+
# * <tt>root</tt> - Returns the root of the current node (<tt>root</tt>
|
66
|
+
# when called on <tt>subchild2</tt>)
|
67
|
+
module ClassMethods
|
68
|
+
# Configuration options are:
|
69
|
+
#
|
70
|
+
# * <tt>foreign_key</tt> - specifies the column name to use for tracking
|
71
|
+
# of the tree (default: +parent_id+)
|
72
|
+
# * <tt>order</tt> - makes it possible to sort the children according to
|
73
|
+
# this SQL snippet.
|
74
|
+
# * <tt>counter_cache</tt> - keeps a count in a +children_count+ column
|
75
|
+
# if set to +true+ (default: +false+).
|
76
|
+
def acts_as_tree(options = {})
|
77
|
+
configuration = {
|
78
|
+
:foreign_key => "parent_id",
|
79
|
+
:order => nil,
|
80
|
+
:counter_cache => nil,
|
81
|
+
:dependent => :destroy
|
82
|
+
}
|
83
|
+
|
84
|
+
configuration.update(options) if options.is_a?(Hash)
|
85
|
+
|
86
|
+
belongs_to :parent, :class_name => name,
|
87
|
+
:foreign_key => configuration[:foreign_key],
|
88
|
+
:counter_cache => configuration[:counter_cache],
|
89
|
+
:inverse_of => :children
|
90
|
+
|
91
|
+
has_many :children, :class_name => name,
|
92
|
+
:foreign_key => configuration[:foreign_key],
|
93
|
+
:order => configuration[:order],
|
94
|
+
:dependent => configuration[:dependent],
|
95
|
+
:inverse_of => :parent
|
96
|
+
|
97
|
+
class_eval <<-EOV
|
98
|
+
include ActsAsTree::InstanceMethods
|
99
|
+
|
100
|
+
after_update :update_parents_counter_cache
|
101
|
+
|
102
|
+
def self.roots
|
103
|
+
order_option = %Q{#{configuration.fetch :order, "nil"}}
|
104
|
+
|
105
|
+
find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL",
|
106
|
+
:order => order_option)
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.root
|
110
|
+
order_option = %Q{#{configuration.fetch :order, "nil"}}
|
111
|
+
|
112
|
+
find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL",
|
113
|
+
:order => order_option)
|
114
|
+
end
|
115
|
+
EOV
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
module InstanceMethods
|
120
|
+
# Returns list of ancestors, starting from parent until root.
|
121
|
+
#
|
122
|
+
# subchild1.ancestors # => [child1, root]
|
123
|
+
def ancestors
|
124
|
+
node, nodes = self, []
|
125
|
+
nodes << node = node.parent while node.parent
|
126
|
+
nodes
|
127
|
+
end
|
128
|
+
|
129
|
+
# Returns the root node of the tree.
|
130
|
+
def root
|
131
|
+
node = self
|
132
|
+
node = node.parent while node.parent
|
133
|
+
node
|
134
|
+
end
|
135
|
+
|
136
|
+
# Returns all siblings of the current node.
|
137
|
+
#
|
138
|
+
# subchild1.siblings # => [subchild2]
|
139
|
+
def siblings
|
140
|
+
self_and_siblings - [self]
|
141
|
+
end
|
142
|
+
|
143
|
+
# Returns all siblings and a reference to the current node.
|
144
|
+
#
|
145
|
+
# subchild1.self_and_siblings # => [subchild1, subchild2]
|
146
|
+
def self_and_siblings
|
147
|
+
parent ? parent.children : self.class.roots
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns children (without subchildren) and current node itself.
|
151
|
+
#
|
152
|
+
# root.self_and_children # => [root, child1]
|
153
|
+
def self_and_children
|
154
|
+
[self] + self.children
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def update_parents_counter_cache
|
160
|
+
if self.respond_to?(:children_count) && parent_id_changed?
|
161
|
+
self.class.decrement_counter(:children_count, parent_id_was)
|
162
|
+
self.class.increment_counter(:children_count, parent_id)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# Deprecating the following code in the future.
|
169
|
+
require 'acts_as_tree/active_record/acts/tree'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'acts_as_tree'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Acts #:nodoc:
|
5
|
+
module Tree
|
6
|
+
include ::ActsAsTree
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
Kernel.warn "[DEPRECATION] The module ActiveRecord::Acts::Tree has moved to ActsAsTree"
|
10
|
+
|
11
|
+
base.extend ::ActsAsTree::ClassMethods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,304 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'active_record'
|
3
|
+
require 'acts_as_tree'
|
4
|
+
|
5
|
+
class Test::Unit::TestCase
|
6
|
+
def assert_queries(num = 1)
|
7
|
+
$query_count = 0
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
|
11
|
+
end
|
12
|
+
|
13
|
+
def assert_no_queries(&block)
|
14
|
+
assert_queries(0, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
19
|
+
|
20
|
+
# AR keeps printing annoying schema statements
|
21
|
+
$stdout = StringIO.new
|
22
|
+
|
23
|
+
def setup_db
|
24
|
+
ActiveRecord::Base.logger
|
25
|
+
ActiveRecord::Schema.define(version: 1) do
|
26
|
+
create_table :mixins do |t|
|
27
|
+
t.column :type, :string
|
28
|
+
t.column :parent_id, :integer
|
29
|
+
t.column :children_count, :integer, default: 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def teardown_db
|
35
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
36
|
+
ActiveRecord::Base.connection.drop_table(table)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Mixin < ActiveRecord::Base
|
41
|
+
include ActsAsTree
|
42
|
+
end
|
43
|
+
|
44
|
+
class TreeMixin < Mixin
|
45
|
+
acts_as_tree foreign_key: "parent_id", order: "id"
|
46
|
+
end
|
47
|
+
|
48
|
+
class TreeMixinWithoutOrder < Mixin
|
49
|
+
acts_as_tree foreign_key: "parent_id"
|
50
|
+
end
|
51
|
+
|
52
|
+
class TreeMixinNullify < Mixin
|
53
|
+
acts_as_tree foreign_key: "parent_id", order: "id", dependent: :nullify
|
54
|
+
end
|
55
|
+
|
56
|
+
class TreeMixinWithCounterCache < Mixin
|
57
|
+
acts_as_tree foreign_key: "parent_id", order: "id", counter_cache: :children_count
|
58
|
+
end
|
59
|
+
|
60
|
+
class RecursivelyCascadedTreeMixin < Mixin
|
61
|
+
acts_as_tree foreign_key: "parent_id"
|
62
|
+
has_one :first_child, class_name: 'RecursivelyCascadedTreeMixin', foreign_key: :parent_id
|
63
|
+
end
|
64
|
+
|
65
|
+
class TreeTest < Test::Unit::TestCase
|
66
|
+
|
67
|
+
def setup
|
68
|
+
setup_db
|
69
|
+
@root1 = TreeMixin.create!
|
70
|
+
@root_child1 = TreeMixin.create! parent_id: @root1.id
|
71
|
+
@child1_child = TreeMixin.create! parent_id: @root_child1.id
|
72
|
+
@child1_child_child = TreeMixin.create! parent_id: @child1_child.id
|
73
|
+
@root_child2 = TreeMixin.create! parent_id: @root1.id
|
74
|
+
@root2 = TreeMixin.create!
|
75
|
+
@root3 = TreeMixin.create!
|
76
|
+
end
|
77
|
+
|
78
|
+
def teardown
|
79
|
+
teardown_db
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_children
|
83
|
+
assert_equal @root1.children, [@root_child1, @root_child2]
|
84
|
+
assert_equal @root_child1.children, [@child1_child]
|
85
|
+
assert_equal @child1_child.children, [@child1_child_child]
|
86
|
+
assert_equal @child1_child_child.children, []
|
87
|
+
assert_equal @root_child2.children, []
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_parent
|
91
|
+
assert_equal @root_child1.parent, @root1
|
92
|
+
assert_equal @root_child1.parent, @root_child2.parent
|
93
|
+
assert_nil @root1.parent
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_delete
|
97
|
+
assert_equal 7, TreeMixin.count
|
98
|
+
@root1.destroy
|
99
|
+
assert_equal 2, TreeMixin.count
|
100
|
+
@root2.destroy
|
101
|
+
@root3.destroy
|
102
|
+
assert_equal 0, TreeMixin.count
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_insert
|
106
|
+
@extra = @root1.children.create
|
107
|
+
|
108
|
+
assert @extra
|
109
|
+
|
110
|
+
assert_equal @extra.parent, @root1
|
111
|
+
|
112
|
+
assert_equal 3, @root1.reload.children.count
|
113
|
+
assert @root1.children.include?(@extra)
|
114
|
+
assert @root1.children.include?(@root_child1)
|
115
|
+
assert @root1.children.include?(@root_child2)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_ancestors
|
119
|
+
assert_equal [], @root1.ancestors
|
120
|
+
assert_equal [@root1], @root_child1.ancestors
|
121
|
+
assert_equal [@root_child1, @root1], @child1_child.ancestors
|
122
|
+
assert_equal [@root1], @root_child2.ancestors
|
123
|
+
assert_equal [], @root2.ancestors
|
124
|
+
assert_equal [], @root3.ancestors
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_root
|
128
|
+
assert_equal @root1, TreeMixin.root
|
129
|
+
assert_equal @root1, @root1.root
|
130
|
+
assert_equal @root1, @root_child1.root
|
131
|
+
assert_equal @root1, @child1_child.root
|
132
|
+
assert_equal @root1, @root_child2.root
|
133
|
+
assert_equal @root2, @root2.root
|
134
|
+
assert_equal @root3, @root3.root
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_roots
|
138
|
+
assert_equal [@root1, @root2, @root3], TreeMixin.roots
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_siblings
|
142
|
+
assert_equal [@root2, @root3], @root1.siblings
|
143
|
+
assert_equal [@root_child2], @root_child1.siblings
|
144
|
+
assert_equal [], @child1_child.siblings
|
145
|
+
assert_equal [@root_child1], @root_child2.siblings
|
146
|
+
assert_equal [@root1, @root3], @root2.siblings
|
147
|
+
assert_equal [@root1, @root2], @root3.siblings
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_self_and_siblings
|
151
|
+
assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
|
152
|
+
assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
|
153
|
+
assert_equal [@child1_child], @child1_child.self_and_siblings
|
154
|
+
assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
|
155
|
+
assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
|
156
|
+
assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_self_and_children
|
160
|
+
assert_equal [@root1, @root_child1, @root_child2], @root1.self_and_children
|
161
|
+
assert_equal [@root2], @root2.self_and_children
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_nullify
|
165
|
+
root4 = TreeMixinNullify.create!
|
166
|
+
root4_child = TreeMixinNullify.create! parent_id: root4.id
|
167
|
+
|
168
|
+
assert_equal 2, TreeMixinNullify.count
|
169
|
+
assert_equal root4.id, root4_child.parent_id
|
170
|
+
|
171
|
+
root4.destroy
|
172
|
+
|
173
|
+
assert_equal 1, TreeMixinNullify.count
|
174
|
+
assert_nil root4_child.reload.parent_id
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
class TreeTestWithEagerLoading < Test::Unit::TestCase
|
180
|
+
|
181
|
+
def setup
|
182
|
+
teardown_db
|
183
|
+
setup_db
|
184
|
+
@root1 = TreeMixin.create!
|
185
|
+
@root_child1 = TreeMixin.create! parent_id: @root1.id
|
186
|
+
@child1_child = TreeMixin.create! parent_id: @root_child1.id
|
187
|
+
@root_child2 = TreeMixin.create! parent_id: @root1.id
|
188
|
+
@root2 = TreeMixin.create!
|
189
|
+
@root3 = TreeMixin.create!
|
190
|
+
|
191
|
+
@rc1 = RecursivelyCascadedTreeMixin.create!
|
192
|
+
@rc2 = RecursivelyCascadedTreeMixin.create! parent_id: @rc1.id
|
193
|
+
@rc3 = RecursivelyCascadedTreeMixin.create! parent_id: @rc2.id
|
194
|
+
@rc4 = RecursivelyCascadedTreeMixin.create! parent_id: @rc3.id
|
195
|
+
end
|
196
|
+
|
197
|
+
def teardown
|
198
|
+
teardown_db
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_eager_association_loading
|
202
|
+
roots = TreeMixin.find :all, include: :children,
|
203
|
+
conditions: "mixins.parent_id IS NULL",
|
204
|
+
order: "mixins.id"
|
205
|
+
|
206
|
+
assert_equal [@root1, @root2, @root3], roots
|
207
|
+
|
208
|
+
assert_no_queries do
|
209
|
+
assert_equal 2, roots[0].children.count
|
210
|
+
assert_equal 0, roots[1].children.count
|
211
|
+
assert_equal 0, roots[2].children.count
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
|
216
|
+
root_node = RecursivelyCascadedTreeMixin.find :first,
|
217
|
+
include: {children: {children: :children}},
|
218
|
+
order: 'mixins.id'
|
219
|
+
|
220
|
+
assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
|
224
|
+
root_node = RecursivelyCascadedTreeMixin.find :first,
|
225
|
+
include: {first_child: {first_child: :first_child}},
|
226
|
+
order: 'mixins.id'
|
227
|
+
|
228
|
+
assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
|
232
|
+
leaf_node = RecursivelyCascadedTreeMixin.find :first,
|
233
|
+
include: {parent: {parent: :parent}},
|
234
|
+
order: 'mixins.id DESC'
|
235
|
+
|
236
|
+
assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
class TreeTestWithoutOrder < Test::Unit::TestCase
|
241
|
+
|
242
|
+
def setup
|
243
|
+
setup_db
|
244
|
+
@root1 = TreeMixinWithoutOrder.create!
|
245
|
+
@root2 = TreeMixinWithoutOrder.create!
|
246
|
+
end
|
247
|
+
|
248
|
+
def teardown
|
249
|
+
teardown_db
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_root
|
253
|
+
assert [@root1, @root2].include? TreeMixinWithoutOrder.root
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_roots
|
257
|
+
assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
class UnsavedTreeTest < Test::Unit::TestCase
|
262
|
+
def setup
|
263
|
+
setup_db
|
264
|
+
@root = TreeMixin.new
|
265
|
+
@root_child = @root.children.build
|
266
|
+
end
|
267
|
+
|
268
|
+
def teardown
|
269
|
+
teardown_db
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_inverse_of
|
273
|
+
# We want children to be aware of their parent before saving either
|
274
|
+
assert_equal @root, @root_child.parent
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
|
279
|
+
class TreeTestWithCounterCache < Test::Unit::TestCase
|
280
|
+
def setup
|
281
|
+
teardown_db
|
282
|
+
setup_db
|
283
|
+
@root = TreeMixinWithCounterCache.create!
|
284
|
+
@child1 = TreeMixinWithCounterCache.create! parent_id: @root.id
|
285
|
+
@child1_child1 = TreeMixinWithCounterCache.create! parent_id: @child1.id
|
286
|
+
@child2 = TreeMixinWithCounterCache.create! parent_id: @root.id
|
287
|
+
end
|
288
|
+
|
289
|
+
def teardown
|
290
|
+
teardown_db
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_counter_cache
|
294
|
+
assert_equal 2, @root.reload.children_count
|
295
|
+
assert_equal 1, @child1.reload.children_count
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_update_parents_counter_cache
|
299
|
+
@child1_child1.update_attributes(:parent_id => @root.id)
|
300
|
+
assert_equal 3, @root.reload.children_count
|
301
|
+
assert_equal 0, @child1.reload.children_count
|
302
|
+
end
|
303
|
+
|
304
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_tree-1.8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Erik Dahlstrand
|
14
|
+
- Rails Core
|
15
|
+
- Mark Turner
|
16
|
+
- Swanand Pagnis
|
17
|
+
autorequire:
|
18
|
+
bindir: bin
|
19
|
+
cert_chain: []
|
20
|
+
|
21
|
+
date: 2013-02-22 00:00:00 Z
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
requirement: *id001
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
name: activerecord
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
requirement: *id002
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
name: sqlite3
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirement: *id003
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
name: rdoc
|
67
|
+
description: "A gem that adds simple support for organizing ActiveRecord models into parent\xE2\x80\x93children relationships."
|
68
|
+
email:
|
69
|
+
- erik.dahlstrand@gmail.com
|
70
|
+
- mark@amerine.net
|
71
|
+
- swanand.pagnis@gmail.com
|
72
|
+
executables: []
|
73
|
+
|
74
|
+
extensions: []
|
75
|
+
|
76
|
+
extra_rdoc_files: []
|
77
|
+
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- .travis.yml
|
81
|
+
- Gemfile
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- acts_as_tree.gemspec
|
85
|
+
- lib/acts_as_tree.rb
|
86
|
+
- lib/acts_as_tree/active_record/acts/tree.rb
|
87
|
+
- lib/acts_as_tree/version.rb
|
88
|
+
- test/acts_as_tree_test.rb
|
89
|
+
homepage: https://github.com/dryade/acts_as_tree
|
90
|
+
licenses: []
|
91
|
+
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options:
|
94
|
+
- --charset=UTF-8
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.24
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Provides a simple tree behaviour to active_record models.
|
122
|
+
test_files: []
|
123
|
+
|