acts_as_better_tree 0.9.4 → 0.9.5

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.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .DS_STORE
7
+ Gemfile.lock
8
+ gemtest
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- acts_as_better_tree (0.0.1)
4
+ acts_as_better_tree (0.9.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # ActsAsBetterTree
2
2
 
3
- acts_as_better_tree is great for anyone who needs a fast tree capable of handling millions of nodes without slowing down on writes like nestedset or on reads like a standard tree.
4
- It is backwards compatible with acts_as_tree and remains fast with large datasets by storing the ancestry of every node in the field csv_ids.
3
+ An alternative to nested_sets and acts_as_tree. Designed to be a drop in replacement for acts_as_tree. Replaces betternestedset without the slow inserts when dealing with a large dataset. Used by upillar.com on a dataset of over 900,000 categories with no slow downs. In tests it shows a 285% speed increase on inserts with a dataset of 100k categories. As datasets become larger its insert speed stays about the same when nested_sets become slower. In all of my tests read speeds have been comparable with nested sets on everything but all_children which is inperceptibly slower on a dataset of 100k than betternestedset.
5
4
 
6
5
 
7
6
  ## Installation
@@ -29,7 +28,7 @@ Or install it yourself as:
29
28
  t.column :name, :string
30
29
  end
31
30
 
32
- If upgrading from acts_as_tree just add root_id and csv_ids and run Category.build_csv_ids!
31
+ If upgrading from acts_as_tree just add root_id and csv_ids and run Category.tree_to_better_tree!
33
32
 
34
33
  class Category < ActiveRecord::Base
35
34
  acts_as_better_tree :order => "name"
@@ -49,8 +48,19 @@ Or install it yourself as:
49
48
  child1.parent # => root
50
49
  root.children # => [child1]
51
50
  root.children.first.children.first # => subchild1
52
-
53
- Copyright (c) 2008 Isaac Sloan, released under the MIT license
51
+ child1.self_and_ancestors # => [root, child1]
52
+ child1.ancestors
53
+ child1.siblings
54
+ child1.self_and_siblings
55
+ child1.move_to_child_of(parent)
56
+ child1.childless?
57
+ child1.ancestor_of?(subchild1)
58
+ child1.descendant_of?(root)
59
+ root.to_csv # => "root,child1,subchild1\n" returns and array of all children
60
+ Category.to_csv # => "root,child1,subchild1\nroot,child1,subchild2\n" returns entire tree in a csv string
61
+
62
+
63
+ Copyright (c) 2008 Isaac Sloan, released under the MIT license.
54
64
  Inspired by David Heinemeier Hansson's acts_as_tree
55
65
 
56
66
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module ActsAsBetterTree
2
- VERSION = "0.9.4"
2
+ VERSION = "0.9.5"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require "acts_as_better_tree/version"
2
-
2
+ require "csv"
3
3
  module ActiveRecord
4
4
  module Acts
5
5
  module BetterTree
@@ -27,22 +27,26 @@ module ActiveRecord
27
27
  roots(options).first
28
28
  end
29
29
 
30
- def traverse(nodes = self.roots, &block)
30
+ def recursively_traverse(nodes = self.roots, &block)
31
31
  nodes.each do |node|
32
32
  yield node
33
- traverse(node.children, &block)
33
+ recursively_traverse(node.children, &block)
34
34
  end
35
35
  end
36
36
 
37
37
  # Call this to upgrade an existing acts_as_tree to acts_as_better_tree
38
- def build_csv_ids!(nodes = self.roots)
38
+ def tree_to_better_tree!(nodes = self.roots)
39
39
  transaction do
40
- traverse(nodes) do |node|
40
+ recursively_traverse(nodes) do |node|
41
41
  node.csv_ids = node.build_csv_ids
42
42
  node.save
43
43
  end
44
44
  end
45
45
  end
46
+
47
+ def to_csv
48
+ return new.to_csv(roots)
49
+ end
46
50
  end
47
51
  end
48
52
  end
@@ -123,12 +127,12 @@ module ActiveRecord
123
127
  csv = []
124
128
  nodes.each do |node|
125
129
  if node.childless?
126
- csv += [node.self_and_ancestors.map(&:name).join(",")]
130
+ csv += [node.self_and_ancestors.map(&:name).to_csv]
127
131
  else
128
132
  csv += [to_csv(node.children)]
129
133
  end
130
134
  end
131
- return csv.join("\n")
135
+ return csv.join("")
132
136
  end
133
137
 
134
138
  def build_csv_ids
@@ -69,4 +69,7 @@ describe "ActsAsBetterTree" do
69
69
  bobcats.parent.name.should eql "Cats"
70
70
  end
71
71
 
72
+ it "should return a csv string of all nodes" do
73
+ Category.to_csv.should eql "Animals,Cats,Lions\nAnimals,Cats,Tiger\nAnimals,Cats,House Cat\nAnimals,Cats,Panther\nAnimals,Cats,Bobcats\n"
74
+ end
72
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_better_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: acts_as_better_tree is great for anyone who needs a fast tree capable
15
15
  of handling millions of nodes without slowing down on writes like nestedset or on
@@ -20,16 +20,13 @@ executables: []
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
- - .DS_Store
23
+ - .gitignore
24
24
  - Gemfile
25
25
  - Gemfile.lock
26
26
  - LICENSE
27
27
  - README.md
28
28
  - Rakefile
29
- - acts_as_better_tree-0.0.1.gem
30
- - acts_as_better_tree-0.9.3.gem
31
29
  - acts_as_better_tree.gemspec
32
- - gemtest
33
30
  - lib/acts_as_better_tree.rb
34
31
  - lib/acts_as_better_tree/version.rb
35
32
  - spec/acts_as_better_tree_spec.rb
data/.DS_Store DELETED
Binary file
Binary file
Binary file
data/gemtest DELETED
Binary file