activerecord-uuid 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/CHANGELOG +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +8 -0
- data/README.md +73 -0
- data/Rakefile +1 -0
- data/activerecord-uuid.gemspec +27 -0
- data/config/initializers/active_record.rb +30 -0
- data/lib/active_record/uuid.rb +20 -0
- data/lib/active_record/uuid/finder_methods.rb +25 -0
- data/lib/active_record/uuid/model.rb +23 -0
- data/lib/active_record/uuid/railtie.rb +10 -0
- data/lib/active_record/uuid/relation.rb +24 -0
- data/lib/active_record/uuid/version.rb +5 -0
- data/lib/activerecord-uuid.rb +1 -0
- metadata +118 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright (c) 2012 Adaptly
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# ActiveRecord::Uuid
|
2
|
+
|
3
|
+
Adds automatically-assigned UUIDs to your model and allows you to find by UUID directly.
|
4
|
+
|
5
|
+
## Configuration
|
6
|
+
Add the gem to your Gemfile:
|
7
|
+
|
8
|
+
```
|
9
|
+
gem 'activerecord-uuid'
|
10
|
+
```
|
11
|
+
|
12
|
+
Create a migration to inclue a uuid column:
|
13
|
+
|
14
|
+
```
|
15
|
+
class CreateFoos < ActiveRecord::Migration
|
16
|
+
def up
|
17
|
+
create_table :foos do |t|
|
18
|
+
t.string :uuid # Store it as a string. See below to use native PostgreSQL UUID columns.
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Use it in your ActiveRecord model:
|
25
|
+
|
26
|
+
```
|
27
|
+
class Foo < ActiveRecord::Base
|
28
|
+
include ActiveRecord::Uuid
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
See it work:
|
33
|
+
|
34
|
+
```
|
35
|
+
Foo.create
|
36
|
+
=> #<Foo id: 1, uuid: "680d92fc-8b1e-11e1-a80a-70cd60fffe5d">
|
37
|
+
Foo.find(1)
|
38
|
+
=> #<Foo id: 1, uuid: "680d92fc-8b1e-11e1-a80a-70cd60fffe5d">
|
39
|
+
Foo.find("680d92fc-8b1e-11e1-a80a-70cd60fffe5d")
|
40
|
+
=> #<Foo id: 1, uuid: "680d92fc-8b1e-11e1-a80a-70cd60fffe5d">
|
41
|
+
```
|
42
|
+
|
43
|
+
## Native UUID Columns
|
44
|
+
|
45
|
+
This gem adds a Rails initializer that allows you to use native UUID types in PostgreSQL. To create a column in your migration:
|
46
|
+
|
47
|
+
```
|
48
|
+
class CreateFoos < ActiveRecord::Migration
|
49
|
+
def up
|
50
|
+
create_table :foos
|
51
|
+
add_column :foos, :uuid, :uuid
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Note that this does not work with `create_table` and `change_table` block syntaxes. Feel free to add support and open a pull request.
|
57
|
+
|
58
|
+
Dumping your `db/schema.rb` file will not work with native UUID types, so instead you should use this line in your `config/application.rb` file to instead dump to `db/structure.sql`:
|
59
|
+
|
60
|
+
```
|
61
|
+
config.active_record.schema_format = :sql
|
62
|
+
```
|
63
|
+
|
64
|
+
## TODO
|
65
|
+
|
66
|
+
1. Allow configuration of UUID columns.
|
67
|
+
2. Figure out how to make native migrations work with `create_table` and `change_table`.
|
68
|
+
3. Write tests!
|
69
|
+
|
70
|
+
## Copyright
|
71
|
+
|
72
|
+
© 2012 Adaptly. See LICENSE.txt for further details.
|
73
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "active_record/uuid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "activerecord-uuid"
|
7
|
+
s.version = ActiveRecord::Uuid::VERSION
|
8
|
+
s.authors = ["Philip Champon", "Nathan Bryan"]
|
9
|
+
s.email = ["pchampon@gmail.com", "nathan.bryan@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/adaptly/activerecord-uuid"
|
11
|
+
s.summary = %q{Bundles all UUID for ActiveRecord and Postgres}
|
12
|
+
s.description = %q{Installs blah blah blah}
|
13
|
+
|
14
|
+
s.rubyforge_project = "activerecord-uuid"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_runtime_dependency "activerecord", "~> 3.1"
|
22
|
+
s.add_runtime_dependency "railties", "~> 3.1"
|
23
|
+
s.add_runtime_dependency "uuidtools"
|
24
|
+
|
25
|
+
s.add_development_dependency "rake"
|
26
|
+
s.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#
|
2
|
+
# Shamelessly stolen from: http://pilif.github.com/2011/03/rails-postgresql-and-the-native-uuid-type/
|
3
|
+
# This monkey patch renders `rake db:schema:load` useless. You will need to run `rake db:structure:load` from here to eternity.
|
4
|
+
#
|
5
|
+
module ActiveRecord
|
6
|
+
module ConnectionAdapters
|
7
|
+
if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
|
8
|
+
PostgreSQLAdapter.class_eval do
|
9
|
+
def native_database_types_with_uuid_support
|
10
|
+
a = native_database_types_without_uuid_support
|
11
|
+
a[:uuid] = {:name => 'uuid'}
|
12
|
+
return a
|
13
|
+
end
|
14
|
+
alias_method_chain :native_database_types, :uuid_support
|
15
|
+
end
|
16
|
+
|
17
|
+
PostgreSQLColumn.class_eval do
|
18
|
+
def simplified_type_with_uuid_support(field_type)
|
19
|
+
if field_type == 'uuid'
|
20
|
+
:uuid
|
21
|
+
else
|
22
|
+
simplified_type_without_uuid_support(field_type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
alias_method_chain :simplified_type, :uuid_support
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_record/uuid/finder_methods'
|
2
|
+
require 'active_record/uuid/model'
|
3
|
+
require 'active_record/uuid/railtie'
|
4
|
+
require 'active_record/uuid/relation'
|
5
|
+
require 'active_record/uuid/version'
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
module Uuid
|
9
|
+
def self.included(model)
|
10
|
+
class << model
|
11
|
+
alias_method :relation_without_uuid, :relation
|
12
|
+
end
|
13
|
+
|
14
|
+
model.instance_eval do
|
15
|
+
extend Relation
|
16
|
+
include Model
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Uuid
|
3
|
+
module FinderMethods
|
4
|
+
VALID_UUID = /[0-9A-F]{32}/i
|
5
|
+
|
6
|
+
# Ultimately, many methods call #find_one rather than simply just #find. Overriding just
|
7
|
+
# this allows more things to just work properly.
|
8
|
+
def find_one(id)
|
9
|
+
if id.to_s.gsub('-', '') =~ VALID_UUID && record = where(:uuid => id).limit(1).first
|
10
|
+
record
|
11
|
+
else
|
12
|
+
super(id)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def exists?(id = false)
|
17
|
+
if id.to_s.gsub('-', '') =~ VALID_UUID
|
18
|
+
super :uuid => id
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'uuidtools'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Uuid
|
5
|
+
module Model
|
6
|
+
def self.included(model)
|
7
|
+
model.instance_eval do
|
8
|
+
# This should never be user-assigned
|
9
|
+
attr_protected :uuid
|
10
|
+
# Validate on update since uuid won't exist yet on creation
|
11
|
+
validates :uuid, :presence => true, :uniqueness => true, :on => :update
|
12
|
+
before_create :assign_uuid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def assign_uuid
|
19
|
+
write_attribute(:uuid, UUIDTools::UUID.timestamp_create.to_s)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#
|
2
|
+
# Provide a Railtie, which changes the schema format to :sql. This in turn allows development_structure.sql to define uuid fields as the Postgresql uuid type.
|
3
|
+
#
|
4
|
+
module ActiveRecord
|
5
|
+
module Uuid
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
config.active_record.schema_format = :sql # For UUID columns and other PGSQL goodness.
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Uuid
|
3
|
+
module Relation
|
4
|
+
private
|
5
|
+
|
6
|
+
# Rather than monkey-patching ActiveRecord::Relation, subclass it with our own implementation
|
7
|
+
# of the finder methods. This is the way FriendlyId does it, and should place nicely with it.
|
8
|
+
def uuid_relation_class
|
9
|
+
@uuid_relation_class ||= Class.new(relation_without_uuid.class) do
|
10
|
+
alias_method :find_one_without_uuid, :find_one
|
11
|
+
alias_method :exists_without_uuid?, :exists?
|
12
|
+
include Uuid::FinderMethods
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Override the model's relation method to return our subclassed version.
|
17
|
+
def relation
|
18
|
+
@relation = nil unless @relation.class <= uuid_relation_class
|
19
|
+
@relation ||= uuid_relation_class.new(self, arel_table)
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_record/uuid'
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Philip Champon
|
9
|
+
- Nathan Bryan
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-04-20 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
requirement: &70261265704540 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70261265704540
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: railties
|
28
|
+
requirement: &70261265704040 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70261265704040
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: uuidtools
|
39
|
+
requirement: &70261265703660 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70261265703660
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
requirement: &70261265703200 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *70261265703200
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rspec
|
61
|
+
requirement: &70261265702780 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *70261265702780
|
70
|
+
description: Installs blah blah blah
|
71
|
+
email:
|
72
|
+
- pchampon@gmail.com
|
73
|
+
- nathan.bryan@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- .gitignore
|
79
|
+
- CHANGELOG
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- activerecord-uuid.gemspec
|
85
|
+
- config/initializers/active_record.rb
|
86
|
+
- lib/active_record/uuid.rb
|
87
|
+
- lib/active_record/uuid/finder_methods.rb
|
88
|
+
- lib/active_record/uuid/model.rb
|
89
|
+
- lib/active_record/uuid/railtie.rb
|
90
|
+
- lib/active_record/uuid/relation.rb
|
91
|
+
- lib/active_record/uuid/version.rb
|
92
|
+
- lib/activerecord-uuid.rb
|
93
|
+
homepage: http://github.com/adaptly/activerecord-uuid
|
94
|
+
licenses: []
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project: activerecord-uuid
|
113
|
+
rubygems_version: 1.8.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Bundles all UUID for ActiveRecord and Postgres
|
117
|
+
test_files: []
|
118
|
+
has_rdoc:
|