active_record_uuid 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/README.md +126 -22
- data/active_record_uuid.gemspec +4 -3
- data/lib/active_record_uuid.rb +10 -4
- data/lib/active_record_uuid/association_methods.rb +52 -0
- data/lib/active_record_uuid/config.rb +37 -0
- data/lib/active_record_uuid/extensions/quoting_extension.rb +31 -0
- data/lib/active_record_uuid/model.rb +66 -0
- data/lib/active_record_uuid/rails/db.rake +25 -0
- data/lib/active_record_uuid/rails/railtie.rb +19 -0
- data/lib/active_record_uuid/serializer.rb +50 -0
- data/lib/active_record_uuid/uuid_base.rb +12 -4
- data/lib/active_record_uuid/uuid_base_helper.rb +6 -83
- data/lib/active_record_uuid/version.rb +1 -1
- data/spec/lib/association_spec.rb +1 -1
- data/spec/lib/base64_uuid_spec.rb +57 -0
- data/spec/lib/binary_uuid_spec.rb +69 -0
- data/spec/lib/config_spec.rb +41 -0
- data/spec/lib/deprecation_spec.rb +17 -0
- data/spec/lib/hexdigest_uuid_spec.rb +57 -0
- data/spec/lib/serializer_spec.rb +11 -0
- data/spec/lib/string_uuid_spec.rb +57 -0
- data/spec/lib/uuid_config_spec.rb +100 -0
- data/spec/spec_helper.rb +24 -5
- data/spec/support/models.rb +60 -8
- data/spec/support/schema.rb +30 -0
- metadata +48 -12
- data/lib/active_record_uuid/railtie.rb +0 -7
- data/lib/active_record_uuid/tasks/db.rake +0 -26
- data/spec/lib/uuid_base_helper_spec.rb +0 -69
- data/spec/lib/uuid_base_spec.rb +0 -68
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Serializer" do
|
4
|
+
it "should raise exception when loading invalid uuid" do
|
5
|
+
@serializer = ActiveRecordUuid::Serializer.new(:binary)
|
6
|
+
|
7
|
+
lambda {
|
8
|
+
@serializer.load("abkc")
|
9
|
+
}.should raise_error ActiveRecord::SerializationTypeMismatch
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "String Uuid" do
|
4
|
+
context "configure: Post model" do
|
5
|
+
it "should have uuid as primary key based config" do
|
6
|
+
Post.primary_key.should eq('uuid')
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should store uuid as String" do
|
10
|
+
post = Post.create(:text => "String uuid1")
|
11
|
+
post.reload
|
12
|
+
|
13
|
+
post.attributes_before_type_cast["uuid"]["value"].bytesize.should eq(36)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should retreive back as uuid string from String" do
|
17
|
+
post = Post.create(:text => "String uuid2")
|
18
|
+
post.reload
|
19
|
+
|
20
|
+
post.uuid.should be_present
|
21
|
+
post.uuid.should be_instance_of(String)
|
22
|
+
post.uuid.length.should eq(36)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should retreive uuid back with the same value that was assigned" do
|
26
|
+
post = Post.new(:text => "String uuid2")
|
27
|
+
post.uuid = "b360c78e-b62e-11e1-9870-0026b90faf3c"
|
28
|
+
post.save
|
29
|
+
post.reload
|
30
|
+
|
31
|
+
post.uuid.should eq("b360c78e-b62e-11e1-9870-0026b90faf3c")
|
32
|
+
post.uuid.should be_instance_of(String)
|
33
|
+
post.uuid.length.should eq(36)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should find by uuid column" do
|
37
|
+
post = Post.create(:text => "String uuid3")
|
38
|
+
|
39
|
+
Post.find_by_uuid(post.uuid).should eq(post)
|
40
|
+
Post.where(:uuid => post.uuid).should eq([post])
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should find by primary key" do
|
44
|
+
post = Post.create(:text => "String uuid4")
|
45
|
+
|
46
|
+
Post.find(post).should eq(post)
|
47
|
+
Post.find(post.uuid).should eq(post)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should find by array of primary keys" do
|
51
|
+
post1 = Post.create(:text => "String uuid5")
|
52
|
+
post2 = Post.create(:text => "String uuid6")
|
53
|
+
|
54
|
+
Post.find([post1.uuid, post2.uuid]).should eq([post1, post2])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "UuidConfig" do
|
4
|
+
context "uuid base configuration - default option" do
|
5
|
+
it "should not use primary_key" do
|
6
|
+
Article.primary_key.should eq("id")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not set association" do
|
10
|
+
Article.reflections[:comments].options[:foreign_key].should be(nil)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should generate uuid string on uuid column" do
|
14
|
+
article = Article.create(:title => "Hello World")
|
15
|
+
|
16
|
+
article.uuid_valid?.should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should generate uuid on before_validation" do
|
20
|
+
article = Article.new(:title => "Hello World")
|
21
|
+
|
22
|
+
article.valid?.should be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "generate and validate uuid" do
|
27
|
+
it "should validate duplicate uuid" do
|
28
|
+
article1 = Article.create(:uuid => '8df4689b-b580-11e1-9d31-0026b90faf3c', :title => "Hello World1")
|
29
|
+
article2 = Article.new(:uuid => '8df4689b-b580-11e1-9d31-0026b90faf3c', :title => "Hello World2")
|
30
|
+
|
31
|
+
article2.valid?.should be_false
|
32
|
+
article2.errors[:uuid].first.should be_include("been taken")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should assign uuid manually" do
|
36
|
+
article = Article.new(:title => "Manual uuid")
|
37
|
+
article.uuid = "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
|
38
|
+
article.save
|
39
|
+
article.reload
|
40
|
+
|
41
|
+
article.uuid.should eq("79f8a42e-ae60-11e1-9aa9-0026b90faf3c")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should assign uuid if blank" do
|
45
|
+
article = Article.new(:title => "Manual uuid 2")
|
46
|
+
article.assign_uuid
|
47
|
+
|
48
|
+
article.uuid.should be_present
|
49
|
+
article.uuid_valid?.should be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should assign uuid if blank and save immediately" do
|
53
|
+
article = Article.new(:title => "Manual uuid 3")
|
54
|
+
article.assign_uuid!
|
55
|
+
|
56
|
+
article.uuid.should be_present
|
57
|
+
article.uuid_valid?.should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should have valid uuid" do
|
61
|
+
article = Article.new(:title => "Invalid uuid")
|
62
|
+
article.uuid = "invalid"
|
63
|
+
|
64
|
+
article.valid?.should eq(false)
|
65
|
+
article.errors[:uuid].first.should be_include("is invalid")
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should generate uuid" do
|
69
|
+
uuid = Article.generate_uuid
|
70
|
+
|
71
|
+
uuid.should be_present
|
72
|
+
uuid.should be_instance_of(String)
|
73
|
+
uuid.length.should eq(36)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should generate uuid on :after_initialize" do
|
77
|
+
class AfterInitializeArticle < ActiveRecord::Base
|
78
|
+
self.table_name = "articles"
|
79
|
+
uuid_config do
|
80
|
+
hook :after_initialize
|
81
|
+
end
|
82
|
+
end
|
83
|
+
article = AfterInitializeArticle.new(:title => "After initialize uuid")
|
84
|
+
|
85
|
+
article.uuid_valid?.should be_true
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should generate uuid on :before_create" do
|
89
|
+
class BeforeCreateArticle < ActiveRecord::Base
|
90
|
+
self.table_name = "articles"
|
91
|
+
uuid_config do
|
92
|
+
hook :before_create
|
93
|
+
end
|
94
|
+
end
|
95
|
+
article = BeforeCreateArticle.create(:title => "Before create uuid")
|
96
|
+
|
97
|
+
article.uuid_valid?.should be_true
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,28 @@
|
|
1
1
|
require 'active_record_uuid'
|
2
2
|
|
3
|
-
|
4
|
-
:adapter => "
|
5
|
-
:database =>
|
6
|
-
|
3
|
+
db_config = {
|
4
|
+
:adapter => "mysql2",
|
5
|
+
:database => "active_record_uuid",
|
6
|
+
:user => "root",
|
7
|
+
:password => "Q1p2m3g4"
|
8
|
+
}
|
7
9
|
|
10
|
+
ActiveRecord::Base.establish_connection(db_config) rescue nil
|
11
|
+
ActiveRecord::Base.connection.drop_database(db_config[:database]) rescue nil
|
12
|
+
ActiveRecord::Base.establish_connection(db_config.merge(:database => nil))
|
13
|
+
ActiveRecord::Base.connection.create_database(db_config[:database], { :charset => 'utf8', :collation => 'utf8_unicode_ci' })
|
14
|
+
ActiveRecord::Base.establish_connection(db_config)
|
15
|
+
|
16
|
+
# load extension
|
17
|
+
require 'active_record_uuid/extensions/quoting_extension'
|
18
|
+
::ActiveRecord::Base.connection.class.send :include, ActiveRecordUuid::QuotingExtension
|
19
|
+
::ActiveRecord::Base.send(:include, ActiveRecordUuid::Model)
|
20
|
+
|
21
|
+
# load support
|
8
22
|
load File.dirname(__FILE__) + '/support/schema.rb'
|
9
|
-
load File.dirname(__FILE__) + '/support/models.rb'
|
23
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
24
|
+
|
25
|
+
RSpec.configure do |config|
|
26
|
+
config.filter_run :focus => true
|
27
|
+
config.run_all_when_everything_filtered = true
|
28
|
+
end
|
data/spec/support/models.rb
CHANGED
@@ -1,22 +1,74 @@
|
|
1
|
-
class
|
2
|
-
|
1
|
+
class Blog < ActiveRecord::Base
|
2
|
+
uuid_config do
|
3
|
+
association true
|
4
|
+
primary_key true
|
5
|
+
store_as :binary
|
6
|
+
end
|
7
|
+
|
8
|
+
has_many :articles
|
9
|
+
end
|
10
|
+
|
11
|
+
class Article < ActiveRecord::Base
|
12
|
+
uuid_config
|
13
|
+
|
14
|
+
has_many :comments
|
15
|
+
end
|
16
|
+
|
17
|
+
class ArticleUuidBase < ActiveRecord::UuidBase
|
18
|
+
self.table_name = "articles"
|
19
|
+
|
20
|
+
has_many :comments
|
21
|
+
end
|
22
|
+
|
23
|
+
class PostBinary < ActiveRecord::Base
|
24
|
+
uuid_config do
|
25
|
+
association true
|
26
|
+
primary_key true
|
27
|
+
store_as :binary
|
28
|
+
end
|
29
|
+
|
30
|
+
has_many :comments
|
3
31
|
end
|
4
32
|
|
5
|
-
class
|
6
|
-
|
7
|
-
|
33
|
+
class PostBase64 < ActiveRecord::Base
|
34
|
+
uuid_config do
|
35
|
+
primary_key true
|
36
|
+
store_as :base64
|
37
|
+
end
|
8
38
|
end
|
9
39
|
|
10
|
-
class
|
40
|
+
class PostHexDigest < ActiveRecord::Base
|
41
|
+
uuid_config do
|
42
|
+
primary_key true
|
43
|
+
store_as :hexdigest
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Author < ActiveRecord::Base
|
48
|
+
uuid_config do
|
49
|
+
association true
|
50
|
+
primary_key true
|
51
|
+
end
|
52
|
+
|
11
53
|
has_and_belongs_to_many :posts
|
12
54
|
end
|
13
55
|
|
14
|
-
class Post < ActiveRecord::
|
56
|
+
class Post < ActiveRecord::Base
|
57
|
+
uuid_config do
|
58
|
+
primary_key true
|
59
|
+
association true
|
60
|
+
end
|
61
|
+
|
15
62
|
has_many :comments
|
16
63
|
has_one :comment
|
17
64
|
has_and_belongs_to_many :authors
|
18
65
|
end
|
19
66
|
|
20
|
-
class Comment < ActiveRecord::
|
67
|
+
class Comment < ActiveRecord::Base
|
68
|
+
uuid_config do
|
69
|
+
association true
|
70
|
+
primary_key true
|
71
|
+
end
|
72
|
+
|
21
73
|
belongs_to :post
|
22
74
|
end
|
data/spec/support/schema.rb
CHANGED
@@ -1,6 +1,36 @@
|
|
1
1
|
ActiveRecord::Schema.define do
|
2
2
|
self.verbose = false
|
3
3
|
|
4
|
+
create_table :blogs, :force => true, :id => false do |t|
|
5
|
+
t.binary :uuid, :limit => 16
|
6
|
+
t.string :name
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :articles, :force => true do |t|
|
11
|
+
t.string :title
|
12
|
+
t.string :uuid, :limit => 36
|
13
|
+
t.binary :blog_uuid, :limit => 16
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :post_binaries, :force => true, :id => false do |t|
|
17
|
+
t.binary :uuid, :limit => 16
|
18
|
+
t.string :text
|
19
|
+
t.timestamps
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table :post_base64s, :force => true, :id => false do |t|
|
23
|
+
t.string :uuid, :limit => 24
|
24
|
+
t.string :text
|
25
|
+
t.timestamps
|
26
|
+
end
|
27
|
+
|
28
|
+
create_table :post_hex_digests, :force => true, :id => false do |t|
|
29
|
+
t.string :uuid, :limit => 32
|
30
|
+
t.string :text
|
31
|
+
t.timestamps
|
32
|
+
end
|
33
|
+
|
4
34
|
create_table :posts, :force => true, :id => false do |t|
|
5
35
|
t.string :uuid, :limit => 36
|
6
36
|
t.string :text
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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-06-
|
12
|
+
date: 2012-06-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 2.8.0
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
47
|
+
name: mysql2
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
@@ -91,7 +91,26 @@ dependencies:
|
|
91
91
|
- - ~>
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: 2.1.2
|
94
|
-
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mysql2
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: ! 'active_record_uuid is a nice gem that add uuid supports to your activerecord
|
111
|
+
models (MySQL). It allows you to store uuid in various formats: binary (16 bytes),
|
112
|
+
base64 (24 bytes), hexdigest (32 bytes), or string (36 bytes), and query back with
|
113
|
+
uuid string.'
|
95
114
|
email:
|
96
115
|
- chamnapchhorn@gmail.com
|
97
116
|
executables: []
|
@@ -107,14 +126,25 @@ files:
|
|
107
126
|
- Rakefile
|
108
127
|
- active_record_uuid.gemspec
|
109
128
|
- lib/active_record_uuid.rb
|
110
|
-
- lib/active_record_uuid/
|
111
|
-
- lib/active_record_uuid/
|
129
|
+
- lib/active_record_uuid/association_methods.rb
|
130
|
+
- lib/active_record_uuid/config.rb
|
131
|
+
- lib/active_record_uuid/extensions/quoting_extension.rb
|
132
|
+
- lib/active_record_uuid/model.rb
|
133
|
+
- lib/active_record_uuid/rails/db.rake
|
134
|
+
- lib/active_record_uuid/rails/railtie.rb
|
135
|
+
- lib/active_record_uuid/serializer.rb
|
112
136
|
- lib/active_record_uuid/uuid_base.rb
|
113
137
|
- lib/active_record_uuid/uuid_base_helper.rb
|
114
138
|
- lib/active_record_uuid/version.rb
|
115
139
|
- spec/lib/association_spec.rb
|
116
|
-
- spec/lib/
|
117
|
-
- spec/lib/
|
140
|
+
- spec/lib/base64_uuid_spec.rb
|
141
|
+
- spec/lib/binary_uuid_spec.rb
|
142
|
+
- spec/lib/config_spec.rb
|
143
|
+
- spec/lib/deprecation_spec.rb
|
144
|
+
- spec/lib/hexdigest_uuid_spec.rb
|
145
|
+
- spec/lib/serializer_spec.rb
|
146
|
+
- spec/lib/string_uuid_spec.rb
|
147
|
+
- spec/lib/uuid_config_spec.rb
|
118
148
|
- spec/spec_helper.rb
|
119
149
|
- spec/support/models.rb
|
120
150
|
- spec/support/schema.rb
|
@@ -138,14 +168,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
168
|
version: '0'
|
139
169
|
requirements: []
|
140
170
|
rubyforge_project:
|
141
|
-
rubygems_version: 1.8.
|
171
|
+
rubygems_version: 1.8.24
|
142
172
|
signing_key:
|
143
173
|
specification_version: 3
|
144
|
-
summary: A gem for adding
|
174
|
+
summary: A full-featured gem for adding uuid support to your active record models
|
145
175
|
test_files:
|
146
176
|
- spec/lib/association_spec.rb
|
147
|
-
- spec/lib/
|
148
|
-
- spec/lib/
|
177
|
+
- spec/lib/base64_uuid_spec.rb
|
178
|
+
- spec/lib/binary_uuid_spec.rb
|
179
|
+
- spec/lib/config_spec.rb
|
180
|
+
- spec/lib/deprecation_spec.rb
|
181
|
+
- spec/lib/hexdigest_uuid_spec.rb
|
182
|
+
- spec/lib/serializer_spec.rb
|
183
|
+
- spec/lib/string_uuid_spec.rb
|
184
|
+
- spec/lib/uuid_config_spec.rb
|
149
185
|
- spec/spec_helper.rb
|
150
186
|
- spec/support/models.rb
|
151
187
|
- spec/support/schema.rb
|
@@ -1,26 +0,0 @@
|
|
1
|
-
namespace :db do
|
2
|
-
task :update_schema do
|
3
|
-
system "sed -i '/:primary_key => \"uuid\"/ a\
|
4
|
-
t.string \"uuid\", :limit => 36, :default => \"\", :null => false
|
5
|
-
' db/schema.rb"
|
6
|
-
system "sed -i 's/:primary_key => \"uuid\"/:id => false/' db/schema.rb"
|
7
|
-
end
|
8
|
-
|
9
|
-
# Append behavior to rails rake task to correct db/schema.rb, so that the test would work correctly
|
10
|
-
Rake::Task["db:schema:dump"].enhance do
|
11
|
-
Rake::Task["db:update_schema"].invoke
|
12
|
-
end
|
13
|
-
|
14
|
-
task :add_primary_keys => :environment do
|
15
|
-
Dir["#{Rails.root}/app/models/**/*.rb"].each { |path| require path }
|
16
|
-
ActiveRecord::Base.descendants.each do |model|
|
17
|
-
next if model.abstract_class
|
18
|
-
|
19
|
-
begin
|
20
|
-
ActiveRecord::Base.connection.execute "ALTER TABLE #{model.table_name} ADD PRIMARY KEY (#{model.primary_key});"
|
21
|
-
rescue => e
|
22
|
-
p e
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|