rails_has_uuid 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -19
- data/lib/has_uuid.rb +30 -28
- data/lib/has_uuid/active_record/associations/builder/collection_association.rb +4 -3
- data/lib/has_uuid/railtie.rb +1 -1
- data/lib/has_uuid/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1688491f971cda03fc46f446461b5c89ae55358
|
4
|
+
data.tar.gz: 9bbe90fb2000bb4f06769d8a621ec9b7a8b3d127
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e355b002321ccb51e2e893d08c0652ebdff186142b98de1a4d514584c8a0db99daf2e4aab21662e8a07af6d8aac51f4f68ece6f5648235e568cf34d7ecdea5c
|
7
|
+
data.tar.gz: 4274dab71059f05f5aa0d17abfcac72887f6281a49957733862c29b1437ed5ffaebef7d25c795f6452d2d0d0b8a894b6fff12432b5c6a7ca42c9f85148b22069
|
data/README.md
CHANGED
@@ -14,6 +14,18 @@ Enter: has_uuid
|
|
14
14
|
|
15
15
|
To use has_uuid you mirror all of the primary key id, and foreign key ids with another uuid column, and it makes sure you can search the whole object graph using uuids! If that didn't make sense check this out.
|
16
16
|
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
_NOTE_ The name of the gem is *rails_has_uuid* because has_uuid was already taken.
|
20
|
+
|
21
|
+
Via Gemfile:
|
22
|
+
|
23
|
+
```gem 'rails_has_uuid', require: 'has_uuid'```
|
24
|
+
|
25
|
+
On the commandline
|
26
|
+
|
27
|
+
```get install rails_has_uuid```
|
28
|
+
|
17
29
|
## Example
|
18
30
|
|
19
31
|
###Migration
|
@@ -38,7 +50,7 @@ class SetupDatabase < ActiveRecord::Migration
|
|
38
50
|
end
|
39
51
|
```
|
40
52
|
|
41
|
-
has_uuid adds a uuid type to mirations. On SQLite and MySQL it's a binary(16), on PostgreSQL it uses their native uuid type. Notice how we have both a
|
53
|
+
has_uuid adds a uuid type to mirations. On SQLite and MySQL it's a binary(16), on PostgreSQL it uses their native uuid type. Notice how we have both a
|
42
54
|
record_label_id and record_label_uuid column...
|
43
55
|
|
44
56
|
###Model
|
@@ -57,7 +69,7 @@ end
|
|
57
69
|
|
58
70
|
By calling the has_uuid class method, your model is primed.
|
59
71
|
|
60
|
-
### Finders
|
72
|
+
### Finders
|
61
73
|
|
62
74
|
```ruby
|
63
75
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
@@ -73,9 +85,9 @@ RecordLabel.find(UUIDTools::UUID.parse('cf1ba930-6946-4bd5-9265-d9043e5dbb93'),
|
|
73
85
|
...will return an array of objects that match those ids
|
74
86
|
|
75
87
|
### Relationships
|
76
|
-
```ruby
|
88
|
+
```ruby
|
77
89
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
78
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
90
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
79
91
|
|
80
92
|
artist.record_label = record_label
|
81
93
|
artist.record_label_uuid
|
@@ -86,7 +98,7 @@ Will return the uuid of the associated record label.
|
|
86
98
|
The reverse is also true
|
87
99
|
```ruby
|
88
100
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
89
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
101
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
90
102
|
|
91
103
|
artist.record_label_uuid = record_label.uuid
|
92
104
|
artist.record_label
|
@@ -98,7 +110,7 @@ Finally, it'll find the uuid when you associate via id
|
|
98
110
|
i
|
99
111
|
```ruby
|
100
112
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
101
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
113
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
102
114
|
|
103
115
|
artist.record_label_id = record_label.id
|
104
116
|
artist.record_label_uuid
|
@@ -113,7 +125,7 @@ uuid = UUIDTools::UUID.random_create
|
|
113
125
|
=> #<UUID:0x3fd4186240e0 UUID:7c9748da-f9fe-467e-bdb3-34ce2dc67605>
|
114
126
|
|
115
127
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords', :uuid => uuid)
|
116
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
128
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
117
129
|
|
118
130
|
artist.record_label_uuid = uuid.to
|
119
131
|
# is the same as
|
@@ -124,9 +136,9 @@ artist.record_label_uuid = '7c9748da-f9fe-467e-bdb3-34ce2dc67605'
|
|
124
136
|
|
125
137
|
```ruby
|
126
138
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
127
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
128
|
-
artist_2 = Artist.create!(:name => 'Strung Out')
|
129
|
-
artist_3 = Artist.create!(:name => 'Screeching Weasel')
|
139
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
140
|
+
artist_2 = Artist.create!(:name => 'Strung Out')
|
141
|
+
artist_3 = Artist.create!(:name => 'Screeching Weasel')
|
130
142
|
|
131
143
|
record_label.artists = [ artist_1, artist_2, artist_3 ]
|
132
144
|
record_label.save!
|
@@ -140,9 +152,9 @@ it also works the other way:
|
|
140
152
|
|
141
153
|
```ruby
|
142
154
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
143
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
144
|
-
artist_2 = Artist.create!(:name => 'Strung Out')
|
145
|
-
artist_3 = Artist.create!(:name => 'Screeching Weasel')
|
155
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
156
|
+
artist_2 = Artist.create!(:name => 'Strung Out')
|
157
|
+
artist_3 = Artist.create!(:name => 'Screeching Weasel')
|
146
158
|
|
147
159
|
record_label.artist_uuids = [ artist_1.uuid, artist_2.uuid, artist_3.uuid ]
|
148
160
|
record_label.save!
|
@@ -156,9 +168,9 @@ Finally, if you set a relationship id, it will automatically fetch the uuid for
|
|
156
168
|
|
157
169
|
```ruby
|
158
170
|
record_label = RecordLabel.create!(:name => 'Fat Wreck Chords')
|
159
|
-
artist_1 = Artist.create!(:name => 'NOFX')
|
160
|
-
artist_2 = Artist.create!(:name => 'Strung Out')
|
161
|
-
artist_3 = Artist.create!(:name => 'Screeching Weasel')
|
171
|
+
artist_1 = Artist.create!(:name => 'NOFX')
|
172
|
+
artist_2 = Artist.create!(:name => 'Strung Out')
|
173
|
+
artist_3 = Artist.create!(:name => 'Screeching Weasel')
|
162
174
|
|
163
175
|
record_label.artist_ids = [ artist_1.uuid, artist_2.uuid, artist_3.uuid ]
|
164
176
|
record_label.save!
|
@@ -184,13 +196,13 @@ Unfortunately, because of the way ARel works, you can only search via a UUIDTool
|
|
184
196
|
|
185
197
|
```ruby
|
186
198
|
uuid = UUIDTools::UUID.random_create
|
187
|
-
=> #<UUID:0x3fd4186323c0 UUID:7cd2feb5-6929-4288-9ea4-c4e68927f289>
|
199
|
+
=> #<UUID:0x3fd4186323c0 UUID:7cd2feb5-6929-4288-9ea4-c4e68927f289>
|
188
200
|
|
189
201
|
Artist.where('uuid = ?', uuid) # This works
|
190
202
|
Artist.where('uuid = ?', '7cd2feb5-6929-4288-9ea4-c4e68927f289') # This won't (except on PostgreSQL)
|
191
203
|
|
192
204
|
As a result, this also won't work
|
193
|
-
Artist.find_by_uuid('7cd2feb5-6929-4288-9ea4-c4e68927f289')
|
205
|
+
Artist.find_by_uuid('7cd2feb5-6929-4288-9ea4-c4e68927f289')
|
194
206
|
```
|
195
207
|
|
196
208
|
## TODO
|
@@ -200,7 +212,7 @@ Artist.find_by_uuid('7cd2feb5-6929-4288-9ea4-c4e68927f289')
|
|
200
212
|
* Probably other stuff I haven't thought of yet
|
201
213
|
|
202
214
|
## Contributing to has_uuid
|
203
|
-
|
215
|
+
|
204
216
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
205
217
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
206
218
|
* Fork the project.
|
data/lib/has_uuid.rb
CHANGED
@@ -5,47 +5,49 @@ require 'uuidtools'
|
|
5
5
|
require 'activeuuid'
|
6
6
|
|
7
7
|
module HasUuid
|
8
|
-
|
9
|
-
base
|
10
|
-
|
8
|
+
module Mixin
|
9
|
+
def self.included(base)
|
10
|
+
base.send :extend, ClassMethods
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
module ClassMethods
|
14
|
+
def has_uuid(options = {})
|
15
|
+
self.class_eval do
|
16
|
+
cattr_accessor :has_uuid_options
|
17
|
+
self.has_uuid_options = options
|
18
|
+
options[:primary_uuid] ||= :uuid
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
if options[:primary_uuid] != :uuid
|
21
|
+
class_eval do
|
22
|
+
include ActiveUUID::UUID
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def uuid
|
25
|
+
self.send self.class.primary_uuid
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
def uuid=(uuid)
|
29
|
+
self.send "#{self.class.primary_uuid}=".to_sym, uuid
|
30
|
+
end
|
29
31
|
end
|
30
32
|
end
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
class_eval do
|
35
|
+
before_create :generate_uuids_if_needed
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
def generate_uuids_if_needed
|
38
|
+
unless self.uuid
|
39
|
+
begin
|
40
|
+
self.uuid = UUIDTools::UUID.random_create
|
41
|
+
end while !HasUuid.check_uuid(self)
|
42
|
+
end
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
44
46
|
end
|
45
|
-
end
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
def primary_uuid
|
49
|
+
self.has_uuid_options[:primary_uuid]
|
50
|
+
end
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
@@ -4,12 +4,13 @@ module HasUuid
|
|
4
4
|
module Builder
|
5
5
|
module CollectionAssociation
|
6
6
|
extend ActiveSupport::Concern
|
7
|
+
|
7
8
|
def self.included(base)
|
8
9
|
if ::ActiveRecord::VERSION::STRING >= "4.1"
|
9
|
-
base.extend RedefinedReader
|
10
|
-
base.extend RedefinedWriter
|
11
|
-
|
12
10
|
base.class_eval do
|
11
|
+
base.extend RedefinedReader
|
12
|
+
base.extend RedefinedWriter
|
13
|
+
|
13
14
|
class << self
|
14
15
|
alias_method_chain :define_readers, :uuid_args
|
15
16
|
alias_method_chain :define_writers, :uuid_args
|
data/lib/has_uuid/railtie.rb
CHANGED
@@ -9,7 +9,7 @@ module HasUuid
|
|
9
9
|
::ActiveRecord::Associations::CollectionAssociation.send :include, HasUuid::ActiveRecord::Associations::CollectionAssociation
|
10
10
|
::ActiveRecord::Associations::Builder::SingularAssociation.send :include, HasUuid::ActiveRecord::Associations::Builder::SingularAssociation
|
11
11
|
::ActiveRecord::Associations::Builder::CollectionAssociation.send :include, HasUuid::ActiveRecord::Associations::Builder::CollectionAssociation
|
12
|
-
::ActiveRecord::Base.send :include, HasUuid
|
12
|
+
::ActiveRecord::Base.send :include, HasUuid::Mixin
|
13
13
|
end
|
14
14
|
|
15
15
|
ActiveSupport.on_load :activeuuid do
|
data/lib/has_uuid/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_has_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Myles Eftos
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uuidtools
|