Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow UUIDs and other types of non-integer primary keys #40

Merged
merged 5 commits into from
Feb 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions closure_tree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'mysql2'
gem.add_development_dependency 'pg'
gem.add_development_dependency 'sqlite3'
gem.add_development_dependency 'uuidtools'
gem.add_development_dependency 'strong_parameters'
# TODO: gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
end
14 changes: 9 additions & 5 deletions lib/closure_tree/acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def find_all_by_generation(generation_level)
INNER JOIN (
SELECT descendant_id
FROM #{quoted_hierarchy_table_name}
WHERE ancestor_id = #{self.id}
WHERE ancestor_id = #{ct_quote(self.id)}
GROUP BY 1
HAVING MAX(#{quoted_hierarchy_table_name}.generations) = #{generation_level.to_i}
) AS descendants ON (#{quoted_table_name}.#{ct_base_class.primary_key} = descendants.descendant_id)
Expand Down Expand Up @@ -290,9 +290,9 @@ def rebuild!
connection.execute <<-SQL
INSERT INTO #{quoted_hierarchy_table_name}
(ancestor_id, descendant_id, generations)
SELECT x.ancestor_id, #{id}, x.generations + 1
SELECT x.ancestor_id, #{ct_quote(id)}, x.generations + 1
FROM #{quoted_hierarchy_table_name} x
WHERE x.descendant_id = #{self.ct_parent_id}
WHERE x.descendant_id = #{ct_quote(self.ct_parent_id)}
SQL
end
children.each { |c| c.rebuild! }
Expand All @@ -316,9 +316,9 @@ def delete_hierarchy_references
SELECT DISTINCT descendant_id
FROM ( SELECT descendant_id
FROM #{quoted_hierarchy_table_name}
WHERE ancestor_id = #{id}
WHERE ancestor_id = #{ct_quote(id)}
) AS x )
OR descendant_id = #{id}
OR descendant_id = #{ct_quote(id)}
SQL
end

Expand All @@ -334,6 +334,10 @@ def ids_from(scope)
end
end

def ct_quote(id)
self.class.connection.quote(id)
end

# TODO: _parent_id will be removed in the next major version
alias :_parent_id :ct_parent_id

Expand Down
16 changes: 16 additions & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ def force_add_index(table_name, columns, options = {})

ActiveRecord::Schema.define(:version => 0) do

create_table "nodes", :id => false do |t|
t.string "id"
t.string "name"
t.string "parent_id"
t.datetime "created_at"
t.datetime "updated_at"
end

force_add_index "nodes", [:id], :name => "node_id", :unique => true

create_table "node_hierarchies", :id => false, :force => true do |t|
t.string "ancestor_id", :null => false
t.string "descendant_id", :null => false
t.integer "generations", :null => false
end

create_table "tags", :force => true do |t|
t.string "name"
t.string "title"
Expand Down
148 changes: 148 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
require 'spec_helper'

shared_examples_for Node do

it "has correct accessible_attributes" do
Node.accessible_attributes.to_a.should =~ %w(parent name)
end

describe "empty db" do

def nuke_db
NodeHierarchy.delete_all
Node.delete_all
end

before :each do
nuke_db
end

context "empty db" do
it "should return no entities" do
Node.roots.should be_empty
Node.leaves.should be_empty
end
end

context "1 node db" do
it "should return the only entity as a root and leaf" do
a = Node.create!(:name => "a")
Node.roots.should == [a]
Node.leaves.should == [a]
end
end

context "2 node db" do
it "should return a simple root and leaf" do
root = Node.create!(:name => "root")
leaf = root.add_child(Node.create!(:name => "leaf"))
Node.roots.should == [root]
Node.leaves.should == [leaf]
end
end

context "3 node collection.create db" do
before :each do
@root = Node.create! :name => "root"
@mid = @root.children.create! :name => "mid"
@leaf = @mid.children.create! :name => "leaf"
end

it "should create all nodes" do
Node.all.should =~ [@root, @mid, @leaf]
end

it "should return a root and leaf without middle node" do
Node.roots.should == [@root]
Node.leaves.should == [@leaf]
end

it "should delete leaves" do
Node.leaves.destroy_all
Node.roots.should == [@root] # untouched
Node.leaves.should == [@mid]
end

it "should delete everything if you delete the roots" do
Node.roots.destroy_all
Node.all.should be_empty
Node.roots.should be_empty
Node.leaves.should be_empty
end
end

context "3 node explicit_create db" do
before :each do
@root = Node.create!(:name => "root")
@mid = @root.add_child(Node.create!(:name => "mid"))
@leaf = @mid.add_child(Node.create!(:name => "leaf"))
end

it "should create all nodes" do
Node.all.should =~ [@root, @mid, @leaf]
end

it "should return a root and leaf without middle node" do
Node.roots.should == [@root]
Node.leaves.should == [@leaf]
end

it "should prevent parental loops from torso" do
@mid.children << @root
@root.valid?.should be_false
@mid.reload.children.should == [@leaf]
end

it "should prevent parental loops from toes" do
@leaf.children << @root
@root.valid?.should be_false
@leaf.reload.children.should be_empty
end

it "should support re-parenting" do
@root.children << @leaf
Node.leaves.should =~ [@leaf, @mid]
end

it "cleans up hierarchy references for leaves" do
@leaf.destroy
NodeHierarchy.find_all_by_ancestor_id(@leaf.id).should be_empty
NodeHierarchy.find_all_by_descendant_id(@leaf.id).should be_empty
end

it "cleans up hierarchy references" do
@mid.destroy
NodeHierarchy.find_all_by_ancestor_id(@mid.id).should be_empty
NodeHierarchy.find_all_by_descendant_id(@mid.id).should be_empty
@root.reload.should be_root
root_hiers = @root.ancestor_hierarchies.to_a
root_hiers.size.should == 1
NodeHierarchy.find_all_by_ancestor_id(@root.id).should == root_hiers
NodeHierarchy.find_all_by_descendant_id(@root.id).should == root_hiers
end
end

it "performs as the readme says it does" do
grandparent = Node.create(:name => 'Grandparent')
parent = grandparent.children.create(:name => 'Parent')
child1 = Node.create(:name => 'First Child', :parent => parent)
child2 = Node.new(:name => 'Second Child')
parent.children << child2
child3 = Node.new(:name => 'Third Child')
parent.add_child child3
grandparent.self_and_descendants.collect(&:name).should ==
["Grandparent", "Parent", "First Child", "Second Child", "Third Child"]
child1.ancestry_path.should ==
["Grandparent", "Parent", "First Child"]
child3.ancestry_path.should ==
["Grandparent", "Parent", "Third Child"]
d = Node.find_or_create_by_path %w(a b c d)
h = Node.find_or_create_by_path %w(e f g h)
e = h.root
d.add_child(e) # "d.children << e" would work too, of course
h.ancestry_path.should == %w(a b c d e f g h)
end

end

end
14 changes: 13 additions & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
require 'uuidtools'

class Node < ActiveRecord::Base
acts_as_tree :dependent => :destroy
before_create :generate_uuid
attr_accessible :name

def generate_uuid
self.id = UUIDTools::UUID.random_create.to_s
end
end

class Tag < ActiveRecord::Base
acts_as_tree :dependent => :destroy, :order => "name"
before_destroy :add_destroyed_tag
Expand Down Expand Up @@ -60,4 +72,4 @@ class DirectoryLabel < Label

class CuisineType < ActiveRecord::Base
acts_as_tree
end
end