has_secure_uuid 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -11
- data/lib/has_secure_uuid.rb +4 -5
- data/lib/has_secure_uuid/version.rb +1 -1
- data/test/models/user.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25f9924b4ac24f32e7bcdc90b1bc134e4a64a4e96da4e69a2542eab953b2c13d
|
4
|
+
data.tar.gz: 06556e7c8b76a7bb3e84ed28758b3006ab67c1a7c6a279eb1a4bc1e3a0b9b749
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4df32ff48ecffb0481019b46119f87965ebc343407e696fd3438c788b443f0e0ef38ef7b2a57785f31f50a995c873c7554e24d448496f72705b3c8b2e1aeda09
|
7
|
+
data.tar.gz: c41afe2e74f6bfd06d235d00698ad02fc30f18ed514d37584cc5cd0061dd8bf738f7682d23b501dbdfa89f64a02aedf5b3ec4e719bb3d367628a38ee358febc4
|
data/README.md
CHANGED
@@ -20,41 +20,41 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Setting your Model
|
22
22
|
|
23
|
-
The first step is to generate a migration in order to add the
|
23
|
+
The first step is to generate a migration in order to add the uuid key field.
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
rails g migration AddidentifierToUsers
|
26
|
+
rails g migration AddidentifierToUsers uuid:string
|
27
27
|
=>
|
28
28
|
invoke active_record
|
29
|
-
create db/migrate/
|
29
|
+
create db/migrate/20150424010931_add_uuid_to_users.rb
|
30
30
|
```
|
31
31
|
|
32
32
|
Then run `rake db:migrate` in order to update users table in the database. The next step is to add `has_secure_uuid`
|
33
33
|
to the model:
|
34
34
|
```ruby
|
35
|
-
# Schema: User(
|
35
|
+
# Schema: User(uuid:string, identifier:string)
|
36
36
|
class User < ActiveRecord::Base
|
37
37
|
has_secure_uuid
|
38
38
|
end
|
39
39
|
|
40
40
|
user = User.new
|
41
41
|
user.save
|
42
|
-
user.
|
43
|
-
user.
|
42
|
+
user.uuid # => "6c3d256c-aaa7-443a-a16b-75a99ecde277"
|
43
|
+
user.regenerate_uuid # => true
|
44
44
|
```
|
45
45
|
|
46
|
-
To use a custom column to store the uuid field you can specify the column_name option. See example above (e.g:
|
46
|
+
To use a custom column to store the uuid field you can specify the column_name option. See example above (e.g: identifier):
|
47
47
|
|
48
48
|
```ruby
|
49
|
-
# Schema: User(
|
49
|
+
# Schema: User(uuid:string, identifier:string)
|
50
50
|
class User < ActiveRecord::Base
|
51
|
-
has_secure_uuid :
|
51
|
+
has_secure_uuid :identifier
|
52
52
|
end
|
53
53
|
|
54
54
|
user = User.new
|
55
55
|
user.save
|
56
|
-
user.
|
57
|
-
user.
|
56
|
+
user.identifier # => "6c3d256c-aaa7-443a-a16b-75a99ecde277"
|
57
|
+
user.regenerate_identifier # => true
|
58
58
|
```
|
59
59
|
|
60
60
|
## Running tests
|
@@ -78,3 +78,6 @@ Should return
|
|
78
78
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
79
|
4. Push to the branch (`git push origin my-new-feature`)
|
80
80
|
5. Create a new Pull Request
|
81
|
+
|
82
|
+
|
83
|
+
Modified from: https://github.com/robertomiranda/has_secure_token
|
data/lib/has_secure_uuid.rb
CHANGED
@@ -9,24 +9,23 @@ module ActiveRecord
|
|
9
9
|
module ClassMethods
|
10
10
|
# Example using has_secure_uuid
|
11
11
|
#
|
12
|
-
# # Schema: User(identifier:string,
|
12
|
+
# # Schema: User(identifier:string, identifier:string)
|
13
13
|
# class User < ActiveRecord::Base
|
14
14
|
# has_secure_uuid
|
15
|
-
# has_secure_uuid :
|
15
|
+
# has_secure_uuid :identifier
|
16
16
|
# end
|
17
17
|
#
|
18
18
|
# user = User.new
|
19
19
|
# user.save
|
20
|
-
# user.identifier # => "93712506-fe9e-4ae9-a713-53c67cd9bccc"
|
21
20
|
# user.uuid # => "245f9ae1-9f8d-4a0e-817d-8ba3e4d663c1"
|
21
|
+
# user.identifier # => "93712506-fe9e-4ae9-a713-53c67cd9bccc"
|
22
22
|
# user.regenerate_identifier # => true
|
23
23
|
# user.regenerate_uuid # => true
|
24
24
|
#
|
25
25
|
# Note that it's still possible to generate a race condition in the database in the same way that
|
26
26
|
# <tt>validates_uniqueness_of</tt> can. You're encouraged to add a unique index in the database to deal
|
27
27
|
# with this even more unlikely scenario.
|
28
|
-
def has_secure_uuid(attribute = :
|
29
|
-
# Load securerandom only when has_secure_token is used.
|
28
|
+
def has_secure_uuid(attribute = :uuid)
|
30
29
|
define_method("regenerate_#{attribute}") do
|
31
30
|
update_attributes attribute => self.class.generate_unique_uuid
|
32
31
|
end
|
data/test/models/user.rb
CHANGED