active_record_uuid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/.rvmrc +52 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +86 -0
- data/Rakefile +2 -0
- data/active_record_uuid.gemspec +23 -0
- data/lib/active_record_uuid.rb +9 -0
- data/lib/active_record_uuid/railtie.rb +7 -0
- data/lib/active_record_uuid/tasks/db.rake +26 -0
- data/lib/active_record_uuid/uuid_base.rb +19 -0
- data/lib/active_record_uuid/uuid_base_helper.rb +89 -0
- data/lib/active_record_uuid/version.rb +3 -0
- data/spec/lib/association_spec.rb +40 -0
- data/spec/lib/uuid_base_helper_spec.rb +69 -0
- data/spec/lib/uuid_base_spec.rb +68 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/models.rb +22 -0
- data/spec/support/schema.rb +29 -0
- metadata +151 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.rvmrc
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p125@active_record_uuid"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.12.1 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
if [[ $- == *i* ]] # check for interactive shells
|
29
|
+
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
|
30
|
+
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
|
31
|
+
fi
|
32
|
+
else
|
33
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
34
|
+
rvm --create use "$environment_id" || {
|
35
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
36
|
+
return 1
|
37
|
+
}
|
38
|
+
fi
|
39
|
+
|
40
|
+
# If you use bundler, this might be useful to you:
|
41
|
+
if [[ -s Gemfile ]] && {
|
42
|
+
! builtin command -v bundle >/dev/null ||
|
43
|
+
builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
|
44
|
+
}
|
45
|
+
then
|
46
|
+
printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
47
|
+
gem install bundler
|
48
|
+
fi
|
49
|
+
if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
50
|
+
then
|
51
|
+
bundle install | grep -vE '^Using|Your bundle is complete'
|
52
|
+
fi
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chamnap
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# ActiveRecordUuid
|
2
|
+
|
3
|
+
`active_record_uuid` is a nice and simple gem that add uuid support to your `activerecord` models, `associations`, `schema.rb`, and `rake script`.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_record_uuid'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_record_uuid
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
This gem doesn't try to overwrite anything, it just makes use of existing activerecord apis in order to work with `uuid`.
|
22
|
+
|
23
|
+
In your migration files, you don't add any `:primary`, because it will make that column as auto-increment integer primary key. Do something like this:
|
24
|
+
|
25
|
+
create_table :posts, :force => true, :id => false do |t|
|
26
|
+
t.string :uuid, :limit => 36
|
27
|
+
t.string :text
|
28
|
+
t.timestamps
|
29
|
+
end
|
30
|
+
|
31
|
+
Add primary keys to all tables
|
32
|
+
|
33
|
+
$ rake db:add_primary_keys
|
34
|
+
|
35
|
+
There are two ways you can use this gem: by `inherit` and `include`.
|
36
|
+
|
37
|
+
class Post < ActiveRecord::UuidBase
|
38
|
+
end
|
39
|
+
|
40
|
+
Or
|
41
|
+
|
42
|
+
class Post < ActiveRecord::Base
|
43
|
+
include UuidBaseHelper
|
44
|
+
end
|
45
|
+
|
46
|
+
You include `UuidBaseHelper` to your model when your model is already inherited from other classes (STI, ...).
|
47
|
+
|
48
|
+
# create a post with auto-generated uuid
|
49
|
+
post = Post.new(:text => "Manual uuid")
|
50
|
+
post.save
|
51
|
+
post.uuid # "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
|
52
|
+
|
53
|
+
# create a post with manual uuid
|
54
|
+
post = Post.new(:text => "Manual uuid")
|
55
|
+
post.uuid = "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
|
56
|
+
post.save
|
57
|
+
|
58
|
+
# assign a uuid
|
59
|
+
@post.assign_uuid
|
60
|
+
|
61
|
+
# assign a uuid and save immediately
|
62
|
+
@post.assign_uuid!
|
63
|
+
|
64
|
+
# generate to a uuid
|
65
|
+
Post.generate_uuid
|
66
|
+
|
67
|
+
# It expects you have foreign_keys with `_uuid`
|
68
|
+
# you don't have to pass `:foreign_key` option inside association methods
|
69
|
+
class Author < ActiveRecord::UuidBase
|
70
|
+
has_and_belongs_to_many :posts
|
71
|
+
end
|
72
|
+
|
73
|
+
class Post < ActiveRecord::UuidBase
|
74
|
+
has_many :comments
|
75
|
+
has_one :comment
|
76
|
+
has_and_belongs_to_many :authors
|
77
|
+
end
|
78
|
+
|
79
|
+
class Comment < ActiveRecord::UuidBase
|
80
|
+
belongs_to :post
|
81
|
+
end
|
82
|
+
|
83
|
+
# overwrite :foreign_key option and add additional option
|
84
|
+
class Post < ActiveRecord::UuidBase
|
85
|
+
has_many :comments, :foreign_key => "comment_id", :inverse_of => :post
|
86
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_record_uuid/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Chamnap"]
|
6
|
+
gem.email = ["chamnapchhorn@gmail.com"]
|
7
|
+
gem.description = %q{A nice gem to add uuids to your models, associations, and schema.rb}
|
8
|
+
gem.summary = %q{A gem for adding uuids to your active record models}
|
9
|
+
gem.homepage = "https://github.com/chamnap/active_record_uuid"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "active_record_uuid"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveRecordUuid::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "bundler", ">= 1.1.3"
|
19
|
+
gem.add_development_dependency "rspec", "~> 2.8.0"
|
20
|
+
gem.add_development_dependency "sqlite3"
|
21
|
+
gem.add_dependency "activerecord", "~> 3.0"
|
22
|
+
gem.add_dependency "uuidtools", "~> 2.1.2"
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
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
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "uuid_base_helper.rb")
|
2
|
+
|
3
|
+
class ActiveRecord::UuidBase < ::ActiveRecord::Base
|
4
|
+
include UuidBaseHelper
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def inherited_with_uuid(kls)
|
8
|
+
inherited_without_uuid kls
|
9
|
+
kls.assign_defaults
|
10
|
+
end
|
11
|
+
alias_method_chain :inherited, :uuid
|
12
|
+
end
|
13
|
+
|
14
|
+
self.descendants.each do |kls|
|
15
|
+
kls.assign_defaults
|
16
|
+
end
|
17
|
+
|
18
|
+
self.abstract_class = true
|
19
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module UuidBaseHelper
|
2
|
+
UUID_REG = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$/
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send(:extend, ClassMethods)
|
6
|
+
base.send(:extend, AssociationMethods)
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
base.assign_defaults
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def assign_uuid
|
13
|
+
self.id = UUIDTools::UUID.timestamp_create().to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def assign_uuid!
|
17
|
+
assign_uuid
|
18
|
+
save!
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def assign_uuid_when_blank
|
23
|
+
assign_uuid if self.id.blank?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
def generate_uuid
|
29
|
+
UUIDTools::UUID.send("timestamp_create").to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def assign_defaults
|
33
|
+
self.primary_key = 'uuid'
|
34
|
+
self.before_create :assign_uuid_when_blank
|
35
|
+
self.validates_format_of :uuid, :with => UUID_REG, :if => Proc.new { |r| r.id.present? }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module AssociationMethods
|
40
|
+
def has_many(name, options = {}, &extension)
|
41
|
+
options = uuid_assoc_options(:has_many, name, options)
|
42
|
+
super
|
43
|
+
end
|
44
|
+
|
45
|
+
def has_one(name, options = {})
|
46
|
+
options = uuid_assoc_options(:has_one, name, options)
|
47
|
+
super
|
48
|
+
end
|
49
|
+
|
50
|
+
def belongs_to(name, options = {})
|
51
|
+
options = uuid_assoc_options(:belongs_to, name, options)
|
52
|
+
super
|
53
|
+
end
|
54
|
+
|
55
|
+
def has_and_belongs_to_many(name, options = {}, &extension)
|
56
|
+
options = uuid_assoc_options(:has_and_belongs_to_many, name, options)
|
57
|
+
super
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def uuid_assoc_options(macro, association_name, options)
|
62
|
+
opts = {}
|
63
|
+
|
64
|
+
# Set class_name only if not a has-through relation or poly relation
|
65
|
+
if options[:through].blank? and options[:as].blank? and options[:class_name].blank? and !self.name.match(/::/)
|
66
|
+
opts[:class_name] = "::#{association_name.to_s.singularize.camelize}"
|
67
|
+
end
|
68
|
+
|
69
|
+
# Set foreign_key only if not passed
|
70
|
+
if options[:foreign_key].blank?
|
71
|
+
case macro
|
72
|
+
when :has_many, :has_one
|
73
|
+
opts[:foreign_key] = uuid_foreign_key(self.name)
|
74
|
+
when :belongs_to
|
75
|
+
opts[:foreign_key] = uuid_foreign_key(association_name)
|
76
|
+
when :has_and_belongs_to_many
|
77
|
+
opts[:foreign_key] = uuid_foreign_key(self.name)
|
78
|
+
opts[:association_foreign_key] = uuid_foreign_key(association_name)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
options.merge(opts)
|
83
|
+
end
|
84
|
+
|
85
|
+
def uuid_foreign_key(name)
|
86
|
+
name.to_s.singularize.underscore.downcase + "_uuid"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "UuidBase" do
|
4
|
+
context "uuid association" do
|
5
|
+
before(:all) do
|
6
|
+
@post1 = Post.create!(:text => "Post 1")
|
7
|
+
@post2 = Post.create!(:text => "Post 2")
|
8
|
+
@comment1 = Comment.create!(:text => "Comment 1", :post_uuid => @post1.uuid)
|
9
|
+
@comment2 = Comment.create!(:text => "Comment 2", :post_uuid => @post1.uuid)
|
10
|
+
|
11
|
+
@author = Author.create!(:name => "Chamnap")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should retreive has_many relation" do
|
15
|
+
@post1.comments.count.should eq(2)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should retreive has_one relation" do
|
19
|
+
@post1.comment.should be_instance_of(Comment)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should retreive belongs_to relation" do
|
23
|
+
@comment1.post.text.should eq(@post1.text)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should retreive has_and_belongs_to_many relation from post" do
|
27
|
+
@post1.authors << @author
|
28
|
+
|
29
|
+
@post1.authors.count.should eq(1)
|
30
|
+
@post1.authors.should include(@author)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should retreive has_and_belongs_to_many relation from author" do
|
34
|
+
@author.posts << @post2
|
35
|
+
|
36
|
+
@author.posts.count.should eq(2)
|
37
|
+
@author.posts.should include(@post2)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "UuidBaseHelper" do
|
4
|
+
context "uuid as primary key" do
|
5
|
+
it "should have uuid as primary key for new models" do
|
6
|
+
class NewPostInclude < ActiveRecord::Base
|
7
|
+
include UuidBaseHelper
|
8
|
+
self.table_name = "posts"
|
9
|
+
end
|
10
|
+
NewPostInclude.primary_key.should eq('uuid')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should have uuid as primary key for existing models" do
|
14
|
+
PostInclude.primary_key.should eq('uuid')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should assign uuid automatically when create new record" do
|
18
|
+
post = PostInclude.create(:text => "Auto Generate uuid")
|
19
|
+
post.reload
|
20
|
+
|
21
|
+
post.uuid.should be_present
|
22
|
+
post.uuid.should be_instance_of(String)
|
23
|
+
post.uuid.length.should eq(36)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should assign uuid manually" do
|
27
|
+
post = PostInclude.new(:text => "Manual uuid")
|
28
|
+
post.uuid = "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
|
29
|
+
post.save
|
30
|
+
post.reload
|
31
|
+
|
32
|
+
post.uuid.should eq("79f8a42e-ae60-11e1-9aa9-0026b90faf3c")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should assign uuid if blank" do
|
36
|
+
post = PostInclude.new(:text => "Manual uuid 2")
|
37
|
+
post.assign_uuid
|
38
|
+
|
39
|
+
post.uuid.should be_present
|
40
|
+
post.uuid.should be_instance_of(String)
|
41
|
+
post.uuid.length.should eq(36)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should assign uuid if blank and save immediately" do
|
45
|
+
post = PostInclude.new(:text => "Manual uuid 3")
|
46
|
+
post.assign_uuid!
|
47
|
+
|
48
|
+
post.uuid.should be_present
|
49
|
+
post.uuid.should be_instance_of(String)
|
50
|
+
post.uuid.length.should eq(36)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should have valid uuid" do
|
54
|
+
post = PostInclude.new(:text => "Invalid uuid")
|
55
|
+
post.uuid = "invalid"
|
56
|
+
|
57
|
+
post.valid?.should eq(false)
|
58
|
+
post.errors[:uuid].first.should eq("is invalid")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should generate uuid" do
|
62
|
+
uuid = PostInclude.generate_uuid
|
63
|
+
|
64
|
+
uuid.should be_present
|
65
|
+
uuid.should be_instance_of(String)
|
66
|
+
uuid.length.should eq(36)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "UuidBase" do
|
4
|
+
context "uuid as primary key" do
|
5
|
+
it "should have uuid as primary key for new models" do
|
6
|
+
class NewPostInherit < ActiveRecord::UuidBase
|
7
|
+
self.table_name = "posts"
|
8
|
+
end
|
9
|
+
NewPostInherit.primary_key.should eq('uuid')
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have uuid as primary key for existing models" do
|
13
|
+
PostInherit.primary_key.should eq('uuid')
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should assign uuid automatically when create new record" do
|
17
|
+
post = PostInherit.create(:text => "Auto Generate uuid")
|
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 assign uuid manually" do
|
26
|
+
post = PostInherit.new(:text => "Manual uuid")
|
27
|
+
post.uuid = "79f8a42e-ae60-11e1-9aa9-0026b90faf3c"
|
28
|
+
post.save
|
29
|
+
post.reload
|
30
|
+
|
31
|
+
post.uuid.should eq("79f8a42e-ae60-11e1-9aa9-0026b90faf3c")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should assign uuid if blank" do
|
35
|
+
post = PostInherit.new(:text => "Manual uuid 2")
|
36
|
+
post.assign_uuid
|
37
|
+
|
38
|
+
post.uuid.should be_present
|
39
|
+
post.uuid.should be_instance_of(String)
|
40
|
+
post.uuid.length.should eq(36)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should assign uuid if blank and save immediately" do
|
44
|
+
post = PostInherit.new(:text => "Manual uuid 3")
|
45
|
+
post.assign_uuid!
|
46
|
+
|
47
|
+
post.uuid.should be_present
|
48
|
+
post.uuid.should be_instance_of(String)
|
49
|
+
post.uuid.length.should eq(36)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should have valid uuid" do
|
53
|
+
post = PostInherit.new(:text => "Invalid uuid")
|
54
|
+
post.uuid = "invalid"
|
55
|
+
|
56
|
+
post.valid?.should eq(false)
|
57
|
+
post.errors[:uuid].first.should eq("is invalid")
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should generate uuid" do
|
61
|
+
uuid = PostInherit.generate_uuid
|
62
|
+
|
63
|
+
uuid.should be_present
|
64
|
+
uuid.should be_instance_of(String)
|
65
|
+
uuid.length.should eq(36)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'active_record_uuid'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(
|
4
|
+
:adapter => "sqlite3",
|
5
|
+
:database => File.dirname(__FILE__) + "/active_record_uuid.sqlite3"
|
6
|
+
)
|
7
|
+
|
8
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
9
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class PostInherit < ActiveRecord::UuidBase
|
2
|
+
self.table_name = "posts"
|
3
|
+
end
|
4
|
+
|
5
|
+
class PostInclude < ActiveRecord::Base
|
6
|
+
include UuidBaseHelper
|
7
|
+
self.table_name = "posts"
|
8
|
+
end
|
9
|
+
|
10
|
+
class Author < ActiveRecord::UuidBase
|
11
|
+
has_and_belongs_to_many :posts
|
12
|
+
end
|
13
|
+
|
14
|
+
class Post < ActiveRecord::UuidBase
|
15
|
+
has_many :comments
|
16
|
+
has_one :comment
|
17
|
+
has_and_belongs_to_many :authors
|
18
|
+
end
|
19
|
+
|
20
|
+
class Comment < ActiveRecord::UuidBase
|
21
|
+
belongs_to :post
|
22
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
self.verbose = false
|
3
|
+
|
4
|
+
create_table :posts, :force => true, :id => false do |t|
|
5
|
+
t.string :uuid, :limit => 36
|
6
|
+
t.string :text
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :comments, :force => true, :id => false do |t|
|
11
|
+
t.string :uuid, :limit => 36
|
12
|
+
t.string :text
|
13
|
+
t.string :post_uuid
|
14
|
+
|
15
|
+
t.timestamps
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table :authors, :force => true, :id => false do |t|
|
19
|
+
t.string :uuid, :limit => 36
|
20
|
+
t.string :name
|
21
|
+
|
22
|
+
t.timestamps
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :authors_posts, :force => true, :id => false do |t|
|
26
|
+
t.string :post_uuid, :limit => 36
|
27
|
+
t.string :author_uuid, :limit => 36
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chamnap
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.3
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.1.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.8.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.8.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activerecord
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: uuidtools
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 2.1.2
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.1.2
|
94
|
+
description: A nice gem to add uuids to your models, associations, and schema.rb
|
95
|
+
email:
|
96
|
+
- chamnapchhorn@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- .rvmrc
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- active_record_uuid.gemspec
|
109
|
+
- lib/active_record_uuid.rb
|
110
|
+
- lib/active_record_uuid/railtie.rb
|
111
|
+
- lib/active_record_uuid/tasks/db.rake
|
112
|
+
- lib/active_record_uuid/uuid_base.rb
|
113
|
+
- lib/active_record_uuid/uuid_base_helper.rb
|
114
|
+
- lib/active_record_uuid/version.rb
|
115
|
+
- spec/lib/association_spec.rb
|
116
|
+
- spec/lib/uuid_base_helper_spec.rb
|
117
|
+
- spec/lib/uuid_base_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
- spec/support/models.rb
|
120
|
+
- spec/support/schema.rb
|
121
|
+
homepage: https://github.com/chamnap/active_record_uuid
|
122
|
+
licenses: []
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.8.21
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: A gem for adding uuids to your active record models
|
145
|
+
test_files:
|
146
|
+
- spec/lib/association_spec.rb
|
147
|
+
- spec/lib/uuid_base_helper_spec.rb
|
148
|
+
- spec/lib/uuid_base_spec.rb
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
- spec/support/models.rb
|
151
|
+
- spec/support/schema.rb
|