Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61a67b1

Browse files
committedFeb 13, 2013
Merge pull request #42 from calexicoz/master
Refactoring of the tests for UUID support
2 parents 97bc196 + 20c53f3 commit 61a67b1

File tree

4 files changed

+45
-178
lines changed

4 files changed

+45
-178
lines changed
 

‎spec/db/schema.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,34 @@ def force_add_index(table_name, columns, options = {})
1111

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

14-
create_table "nodes", :id => false, :force => true do |t|
15-
t.string "uuid", :unique => true
14+
create_table "tags", :force => true do |t|
1615
t.string "name"
17-
t.string "parent_id"
16+
t.string "title"
17+
t.integer "parent_id"
18+
t.integer "sort_order"
1819
t.datetime "created_at"
1920
t.datetime "updated_at"
2021
end
2122

22-
force_add_index "nodes", [:uuid], :name => "node_id", :unique => true
23-
24-
create_table "node_hierarchies", :id => false, :force => true do |t|
25-
t.string "ancestor_id", :null => false
26-
t.string "descendant_id", :null => false
23+
create_table "tag_hierarchies", :id => false, :force => true do |t|
24+
t.integer "ancestor_id", :null => false
25+
t.integer "descendant_id", :null => false
2726
t.integer "generations", :null => false
2827
end
2928

30-
create_table "tags", :force => true do |t|
29+
create_table "tags_uuid", :id => false, :force => true do |t|
30+
t.string "id", :unique => true
3131
t.string "name"
3232
t.string "title"
33-
t.integer "parent_id"
33+
t.string "parent_id"
3434
t.integer "sort_order"
3535
t.datetime "created_at"
3636
t.datetime "updated_at"
3737
end
3838

39-
create_table "tag_hierarchies", :id => false, :force => true do |t|
40-
t.integer "ancestor_id", :null => false
41-
t.integer "descendant_id", :null => false
39+
create_table "tag_hierarchies_uuid", :id => false, :force => true do |t|
40+
t.string "ancestor_id", :null => false
41+
t.string "descendant_id", :null => false
4242
t.integer "generations", :null => false
4343
end
4444

‎spec/node_spec.rb

Lines changed: 0 additions & 149 deletions
This file was deleted.

‎spec/support/models.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
require 'uuidtools'
22

3-
class Node < ActiveRecord::Base
4-
acts_as_tree :dependent => :destroy
5-
self.primary_key = 'uuid'
6-
before_create :generate_uuid
7-
attr_accessible :name
8-
9-
def generate_uuid
10-
self.uuid = UUIDTools::UUID.random_create.to_s
11-
end
12-
end
13-
143
class Tag < ActiveRecord::Base
154
acts_as_tree :dependent => :destroy, :order => "name"
165
before_destroy :add_destroyed_tag

‎spec/tag_spec.rb

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
shared_examples_for Tag do
3+
shared_examples_for "Tag (1)" do
44

55
it "has correct accessible_attributes" do
66
Tag.accessible_attributes.to_a.should =~ %w(parent name)
@@ -146,8 +146,10 @@ def nuke_db
146146
end
147147

148148
end
149+
end
149150

150-
describe "Tag with fixtures" do
151+
shared_examples_for "Tag (2)" do
152+
describe "Tag (2)" do
151153

152154
fixtures :tags
153155

@@ -367,7 +369,8 @@ def validate_city_tag city
367369
Tag.ancestors.should_not include(ActiveModel::ForbiddenAttributesProtection)
368370
end
369371
end
370-
it_behaves_like Tag
372+
it_behaves_like "Tag (1)"
373+
it_behaves_like "Tag (2)"
371374
end
372375

373376
describe "Tag with AR whitelisted attributes enabled" do
@@ -380,7 +383,8 @@ def validate_city_tag city
380383
Tag.ancestors.should_not include(ActiveModel::ForbiddenAttributesProtection)
381384
end
382385
end
383-
it_behaves_like Tag
386+
it_behaves_like "Tag (1)"
387+
it_behaves_like "Tag (2)"
384388
end
385389

386390
# This has to be the last one, because we include strong parameters into Tag
@@ -391,6 +395,29 @@ class Tag
391395
include ActiveModel::ForbiddenAttributesProtection
392396
end
393397
end
394-
it_behaves_like Tag
398+
it_behaves_like "Tag (1)"
399+
it_behaves_like "Tag (2)"
395400
end
396401

402+
describe "Tag with UUID" do
403+
before(:all) do
404+
# Change tables
405+
Tag.table_name = Tag.table_name.gsub('tags', 'tags_uuid')
406+
Tag.reset_column_information
407+
TagHierarchy.table_name = TagHierarchy.table_name.gsub('tag_hierarchies', 'tag_hierarchies_uuid')
408+
TagHierarchy.reset_column_information
409+
410+
# We have to reset a few other caches
411+
Tag.closure_tree_options[:hierarchy_table_name] = 'tag_hierarchies_uuid'
412+
Tag.reflections.each do |key, ref|
413+
ref.instance_variable_set('@table_name', nil)
414+
ref.instance_variable_set('@quoted_table_name', nil)
415+
ref.options[:order].sub! 'tag_hierarchies', 'tag_hierarchies_uuid' if ref.options[:order]
416+
end
417+
418+
# Add ID
419+
Tag.before_create { self.id = UUIDTools::UUID.random_create.to_s }
420+
end
421+
422+
it_behaves_like "Tag (1)"
423+
end

0 commit comments

Comments
 (0)
Failed to load comments.