jcnetdev-acts_as_tree 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test acts_as_tree plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for acts_as_tree plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'acts_as_tree'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,34 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'acts_as_tree'
3
+ s.version = '1.0.1'
4
+ s.date = '2008-09-27'
5
+
6
+ s.summary = "Allows ActiveRecord Models to be easily structured as a tree"
7
+ s.description = ""
8
+
9
+ s.authors = ["RailsJedi", 'David Heinemeier Hansson']
10
+ s.email = 'railsjedi@gmail.com'
11
+ s.homepage = 'http://github.com/jcnetdev/acts_as_tree'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+
20
+ s.files = ["README",
21
+ "Rakefile",
22
+ "acts_as_tree.gemspec",
23
+ "init.rb",
24
+ "lib/active_record/acts/tree.rb",
25
+ "lib/acts_as_tree.rb",
26
+ "rails/init.rb"]
27
+
28
+ s.test_files = ["test/abstract_unit.rb",
29
+ "test/acts_as_tree_test.rb",
30
+ "test/database.yml",
31
+ "test/fixtures/mixin.rb",
32
+ "test/fixtures/mixins.yml",
33
+ "test/schema.rb"]
34
+ end
@@ -0,0 +1,96 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Tree
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ # Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
9
+ # association. This requires that you have a foreign key column, which by default is called +parent_id+.
10
+ #
11
+ # class Category < ActiveRecord::Base
12
+ # acts_as_tree :order => "name"
13
+ # end
14
+ #
15
+ # Example:
16
+ # root
17
+ # \_ child1
18
+ # \_ subchild1
19
+ # \_ subchild2
20
+ #
21
+ # root = Category.create("name" => "root")
22
+ # child1 = root.children.create("name" => "child1")
23
+ # subchild1 = child1.children.create("name" => "subchild1")
24
+ #
25
+ # root.parent # => nil
26
+ # child1.parent # => root
27
+ # root.children # => [child1]
28
+ # root.children.first.children.first # => subchild1
29
+ #
30
+ # In addition to the parent and children associations, the following instance methods are added to the class
31
+ # after calling <tt>acts_as_tree</tt>:
32
+ # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node (<tt>[subchild2]</tt> when called on <tt>subchild1</tt>)
33
+ # * <tt>self_and_siblings</tt> - Returns all the children of the parent, including the current node (<tt>[subchild1, subchild2]</tt> when called on <tt>subchild1</tt>)
34
+ # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>)
35
+ # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>subchild2</tt>)
36
+ module ClassMethods
37
+ # Configuration options are:
38
+ #
39
+ # * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
40
+ # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
41
+ # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
42
+ def acts_as_tree(options = {})
43
+ configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
44
+ configuration.update(options) if options.is_a?(Hash)
45
+
46
+ belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
47
+ has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
48
+
49
+ class_eval <<-EOV
50
+ include ActiveRecord::Acts::Tree::InstanceMethods
51
+
52
+ def self.roots
53
+ find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
54
+ end
55
+
56
+ def self.root
57
+ find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
58
+ end
59
+ EOV
60
+ end
61
+ end
62
+
63
+ module InstanceMethods
64
+ # Returns list of ancestors, starting from parent until root.
65
+ #
66
+ # subchild1.ancestors # => [child1, root]
67
+ def ancestors
68
+ node, nodes = self, []
69
+ nodes << node = node.parent while node.parent
70
+ nodes
71
+ end
72
+
73
+ # Returns the root node of the tree.
74
+ def root
75
+ node = self
76
+ node = node.parent while node.parent
77
+ node
78
+ end
79
+
80
+ # Returns all siblings of the current node.
81
+ #
82
+ # subchild1.siblings # => [subchild2]
83
+ def siblings
84
+ self_and_siblings - [self]
85
+ end
86
+
87
+ # Returns all siblings and a reference to the current node.
88
+ #
89
+ # subchild1.self_and_siblings # => [subchild1, subchild2]
90
+ def self_and_siblings
91
+ parent ? parent.children : self.class.roots
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,2 @@
1
+ require "active_record/acts/tree"
2
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::Tree
@@ -1 +1 @@
1
- ActiveRecord::Base.send :include, ActiveRecord::Acts::Tree
1
+ require "acts_as_tree"
File without changes
@@ -0,0 +1,219 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+
6
+ $:.unshift File.dirname(__FILE__) + '/../lib'
7
+ require File.dirname(__FILE__) + '/../init'
8
+
9
+ class Test::Unit::TestCase
10
+ def assert_queries(num = 1)
11
+ $query_count = 0
12
+ yield
13
+ ensure
14
+ assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
15
+ end
16
+
17
+ def assert_no_queries(&block)
18
+ assert_queries(0, &block)
19
+ end
20
+ end
21
+
22
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
23
+
24
+ # AR keeps printing annoying schema statements
25
+ $stdout = StringIO.new
26
+
27
+ def setup_db
28
+ ActiveRecord::Base.logger
29
+ ActiveRecord::Schema.define(:version => 1) do
30
+ create_table :mixins do |t|
31
+ t.column :type, :string
32
+ t.column :parent_id, :integer
33
+ end
34
+ end
35
+ end
36
+
37
+ def teardown_db
38
+ ActiveRecord::Base.connection.tables.each do |table|
39
+ ActiveRecord::Base.connection.drop_table(table)
40
+ end
41
+ end
42
+
43
+ class Mixin < ActiveRecord::Base
44
+ end
45
+
46
+ class TreeMixin < Mixin
47
+ acts_as_tree :foreign_key => "parent_id", :order => "id"
48
+ end
49
+
50
+ class TreeMixinWithoutOrder < Mixin
51
+ acts_as_tree :foreign_key => "parent_id"
52
+ end
53
+
54
+ class RecursivelyCascadedTreeMixin < Mixin
55
+ acts_as_tree :foreign_key => "parent_id"
56
+ has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
57
+ end
58
+
59
+ class TreeTest < Test::Unit::TestCase
60
+
61
+ def setup
62
+ setup_db
63
+ @root1 = TreeMixin.create!
64
+ @root_child1 = TreeMixin.create! :parent_id => @root1.id
65
+ @child1_child = TreeMixin.create! :parent_id => @root_child1.id
66
+ @root_child2 = TreeMixin.create! :parent_id => @root1.id
67
+ @root2 = TreeMixin.create!
68
+ @root3 = TreeMixin.create!
69
+ end
70
+
71
+ def teardown
72
+ teardown_db
73
+ end
74
+
75
+ def test_children
76
+ assert_equal @root1.children, [@root_child1, @root_child2]
77
+ assert_equal @root_child1.children, [@child1_child]
78
+ assert_equal @child1_child.children, []
79
+ assert_equal @root_child2.children, []
80
+ end
81
+
82
+ def test_parent
83
+ assert_equal @root_child1.parent, @root1
84
+ assert_equal @root_child1.parent, @root_child2.parent
85
+ assert_nil @root1.parent
86
+ end
87
+
88
+ def test_delete
89
+ assert_equal 6, TreeMixin.count
90
+ @root1.destroy
91
+ assert_equal 2, TreeMixin.count
92
+ @root2.destroy
93
+ @root3.destroy
94
+ assert_equal 0, TreeMixin.count
95
+ end
96
+
97
+ def test_insert
98
+ @extra = @root1.children.create
99
+
100
+ assert @extra
101
+
102
+ assert_equal @extra.parent, @root1
103
+
104
+ assert_equal 3, @root1.children.size
105
+ assert @root1.children.include?(@extra)
106
+ assert @root1.children.include?(@root_child1)
107
+ assert @root1.children.include?(@root_child2)
108
+ end
109
+
110
+ def test_ancestors
111
+ assert_equal [], @root1.ancestors
112
+ assert_equal [@root1], @root_child1.ancestors
113
+ assert_equal [@root_child1, @root1], @child1_child.ancestors
114
+ assert_equal [@root1], @root_child2.ancestors
115
+ assert_equal [], @root2.ancestors
116
+ assert_equal [], @root3.ancestors
117
+ end
118
+
119
+ def test_root
120
+ assert_equal @root1, TreeMixin.root
121
+ assert_equal @root1, @root1.root
122
+ assert_equal @root1, @root_child1.root
123
+ assert_equal @root1, @child1_child.root
124
+ assert_equal @root1, @root_child2.root
125
+ assert_equal @root2, @root2.root
126
+ assert_equal @root3, @root3.root
127
+ end
128
+
129
+ def test_roots
130
+ assert_equal [@root1, @root2, @root3], TreeMixin.roots
131
+ end
132
+
133
+ def test_siblings
134
+ assert_equal [@root2, @root3], @root1.siblings
135
+ assert_equal [@root_child2], @root_child1.siblings
136
+ assert_equal [], @child1_child.siblings
137
+ assert_equal [@root_child1], @root_child2.siblings
138
+ assert_equal [@root1, @root3], @root2.siblings
139
+ assert_equal [@root1, @root2], @root3.siblings
140
+ end
141
+
142
+ def test_self_and_siblings
143
+ assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
144
+ assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
145
+ assert_equal [@child1_child], @child1_child.self_and_siblings
146
+ assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
147
+ assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
148
+ assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
149
+ end
150
+ end
151
+
152
+ class TreeTestWithEagerLoading < Test::Unit::TestCase
153
+
154
+ def setup
155
+ teardown_db
156
+ setup_db
157
+ @root1 = TreeMixin.create!
158
+ @root_child1 = TreeMixin.create! :parent_id => @root1.id
159
+ @child1_child = TreeMixin.create! :parent_id => @root_child1.id
160
+ @root_child2 = TreeMixin.create! :parent_id => @root1.id
161
+ @root2 = TreeMixin.create!
162
+ @root3 = TreeMixin.create!
163
+
164
+ @rc1 = RecursivelyCascadedTreeMixin.create!
165
+ @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
166
+ @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
167
+ @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
168
+ end
169
+
170
+ def teardown
171
+ teardown_db
172
+ end
173
+
174
+ def test_eager_association_loading
175
+ roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
176
+ assert_equal [@root1, @root2, @root3], roots
177
+ assert_no_queries do
178
+ assert_equal 2, roots[0].children.size
179
+ assert_equal 0, roots[1].children.size
180
+ assert_equal 0, roots[2].children.size
181
+ end
182
+ end
183
+
184
+ def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
185
+ root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
186
+ assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
187
+ end
188
+
189
+ def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
190
+ root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
191
+ assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
192
+ end
193
+
194
+ def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
195
+ leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
196
+ assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
197
+ end
198
+ end
199
+
200
+ class TreeTestWithoutOrder < Test::Unit::TestCase
201
+
202
+ def setup
203
+ setup_db
204
+ @root1 = TreeMixinWithoutOrder.create!
205
+ @root2 = TreeMixinWithoutOrder.create!
206
+ end
207
+
208
+ def teardown
209
+ teardown_db
210
+ end
211
+
212
+ def test_root
213
+ assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
214
+ end
215
+
216
+ def test_roots
217
+ assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
218
+ end
219
+ end
File without changes
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jcnetdev-acts_as_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - RailsJedi
@@ -32,9 +32,11 @@ extra_rdoc_files:
32
32
  - README
33
33
  files:
34
34
  - README
35
- - acts_as_list.gemspec
35
+ - Rakefile
36
+ - acts_as_tree.gemspec
36
37
  - init.rb
37
- - lib/acts_as_list.rb
38
+ - lib/active_record/acts/tree.rb
39
+ - lib/acts_as_tree.rb
38
40
  - rails/init.rb
39
41
  has_rdoc: true
40
42
  homepage: http://github.com/jcnetdev/acts_as_tree
@@ -64,4 +66,9 @@ signing_key:
64
66
  specification_version: 2
65
67
  summary: Allows ActiveRecord Models to be easily structured as a tree
66
68
  test_files:
67
- - test/list_test.rb
69
+ - test/abstract_unit.rb
70
+ - test/acts_as_tree_test.rb
71
+ - test/database.yml
72
+ - test/fixtures/mixin.rb
73
+ - test/fixtures/mixins.yml
74
+ - test/schema.rb