acts_as_uuid 0.0.3 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +38 -4
- data/lib/acts_as_uuid/acts_as_uuid.rb +15 -3
- data/lib/acts_as_uuid/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Add this to your `Gemfile`:
|
|
10
10
|
|
11
11
|
`gem "acts_as_uuid"`
|
12
12
|
|
13
|
-
You can now set the type `uuid` for
|
13
|
+
You can now set the type `uuid` for an attribute in your models.
|
14
14
|
|
15
15
|
## Example: Adding a UUID primary key to your models
|
16
16
|
|
@@ -27,6 +27,8 @@ class AddUUIDPrimaryKeyToEmail < ActiveRecord::Migration
|
|
27
27
|
end
|
28
28
|
```
|
29
29
|
|
30
|
+
*Don't forget:* You will also need to change any foreign keys to the `uuid` type.
|
31
|
+
|
30
32
|
### Step 1b: Migrating existing models
|
31
33
|
|
32
34
|
For existing models just add:
|
@@ -40,16 +42,48 @@ class AddUUIDPrimaryKeyToEmail < ActiveRecord::Migration
|
|
40
42
|
end
|
41
43
|
end
|
42
44
|
```
|
43
|
-
Keep in mind that after this all your UUIDs will be set to nil.
|
44
45
|
|
45
|
-
|
46
|
+
*Keep in mind:* after this all your UUIDs will be set to nil.
|
47
|
+
|
48
|
+
### Step 2: Extend your model to auto generate UUIDs for a field
|
46
49
|
|
47
50
|
```ruby
|
48
51
|
class Email < ActiveRecord::Base
|
49
52
|
include ActsAsUUID
|
53
|
+
uuid_on :id, :as => :primary_key
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Example: Adding a UUID as a separate field
|
58
|
+
|
59
|
+
### Step 1: Migrate a model
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class AddUUIDPrimaryKeyToEmail < ActiveRecord::Migration
|
63
|
+
def self.change
|
64
|
+
add_column :emails, :guid, :uuid
|
65
|
+
add_index :emails, :guid, :unique => true
|
66
|
+
end
|
50
67
|
end
|
51
68
|
```
|
52
69
|
|
53
|
-
|
70
|
+
*Keep in mind:* after this all your UUIDs will be set to nil.
|
71
|
+
|
72
|
+
### Step 2: Extend your model to auto generate UUIDs for a field
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class Email < ActiveRecord::Base
|
76
|
+
include ActsAsUUID
|
77
|
+
uuid_on :guid
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
## Important notes
|
82
|
+
|
83
|
+
* Currently only supports 1 field to be set to auto generate a UUID
|
84
|
+
* Integers can't be migrated to UUIDs without losing data
|
85
|
+
* If you use a UUID as a primary key you will lose some ActiveRecord features like: `.first`, `.find_each` and `.find_in_batches`. Basically any method that requires the ID to be sorted as an integer.
|
86
|
+
|
87
|
+
## License
|
54
88
|
|
55
89
|
See LICENSE
|
@@ -1,11 +1,23 @@
|
|
1
1
|
module ActsAsUUID
|
2
2
|
def self.included klass
|
3
3
|
klass.before_validation :generate_uuid
|
4
|
-
klass.
|
5
|
-
klass.primary_key = :id
|
4
|
+
klass.extend ClassMethods
|
6
5
|
end
|
7
6
|
|
8
7
|
def generate_uuid
|
9
|
-
self.
|
8
|
+
self[self.class.acts_as_uuid_field] ||= UUID.generate
|
10
9
|
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
def uuid_on field, options = {}
|
14
|
+
self.acts_as_uuid_field = field
|
15
|
+
self.validates field, :uniqueness => true
|
16
|
+
self.primary_key = field if options[:as] == :primary_key
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :acts_as_uuid_field
|
20
|
+
protected :acts_as_uuid_field=
|
21
|
+
end
|
22
|
+
|
11
23
|
end
|
data/lib/acts_as_uuid/version.rb
CHANGED
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Cristiano Betta
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01
|
18
|
+
date: 2012-02-01 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|