uuid_parameter 0.1.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 +7 -0
- data/LICENSE +20 -0
- data/README.md +98 -0
- data/Rakefile +39 -0
- data/lib/uuid_parameter/railtie.rb +4 -0
- data/lib/uuid_parameter/uuid_version4_validator.rb +16 -0
- data/lib/uuid_parameter/version.rb +3 -0
- data/lib/uuid_parameter.rb +39 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '090d5a9e80813acdeed189b4946249c92072ef14b9519c253d60710c86405132'
|
4
|
+
data.tar.gz: 55aefd1612e65362d133972bc56271ddab529ef4739cddf359898a1c7640bbf0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c8972c83b056bfa1b024949ecbef4957b1e4e9314447995f2455306ca64f59850f75dc3798accde471530281e8dd2a6607d6a85b4076a0803af862740530069
|
7
|
+
data.tar.gz: '079c228088fc5d79467457cbbe0c444178e85765210f3ff372459958b59b93e0a45f1861e11f3d03490fbd2f4b4398e206678bd5ca4b6037f533b7981b4fe99b'
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 hellekin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# UUIDParameter
|
2
|
+
|
3
|
+
The `UUIDParameter` module provides support for UUIDs in `ActiveRecord` models.
|
4
|
+
|
5
|
+
It takes care of generating (if one was not provided), validating, and keeping
|
6
|
+
this UUID intact, protecting the `:uuid` field from being changed once set.
|
7
|
+
|
8
|
+
Models including the `UUIDParameter` module will:
|
9
|
+
- use their `:uuid` rather than their `:id` (primary key) for URLs.
|
10
|
+
- accept an given UUID on creation to allow offline resource generation.
|
11
|
+
|
12
|
+
### Features
|
13
|
+
|
14
|
+
- Can be used with existing models (simply add a `uuid` column).
|
15
|
+
- Does not affect existing primary key.
|
16
|
+
- Can accept any valid random UUID (version 4) provided externally.
|
17
|
+
- Automatically generates a UUID on `:create` if one is not set.
|
18
|
+
- Only works with UUID version 4 (random).
|
19
|
+
- Prevents changing the UUID once set.
|
20
|
+
- Silently ignores any attempt at changing a set UUID.
|
21
|
+
- Overrides `:to_param` to provide the UUID instead of the primary key.
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Add a `uuid` column to your model if it does not have one already:
|
26
|
+
``` bash
|
27
|
+
$ rails g migration AddUuidColumnToUser uuid:string{36}
|
28
|
+
$ rails db:migrate
|
29
|
+
```
|
30
|
+
|
31
|
+
If you're using Postgres, you should use the native `uuid` type instead:
|
32
|
+
``` bash
|
33
|
+
$ rails g migration AddUuidColumnToUser uuid:uuid
|
34
|
+
$ rails db:migrate
|
35
|
+
```
|
36
|
+
|
37
|
+
Then, simply include the module in your model:
|
38
|
+
``` ruby
|
39
|
+
class User < ApplicationRecord
|
40
|
+
include UUIDParameter
|
41
|
+
end
|
42
|
+
|
43
|
+
# u = User.create # Generates a new UUID
|
44
|
+
# u.id # => 123 (does not change primary key)
|
45
|
+
# u.uuid # => '8bb27724-7439-4965-9598-883419179b21'
|
46
|
+
# u.to_param # => '8bb27724-7439-4965-9598-883419179b21'
|
47
|
+
# u.uuid = SecureRandom.uuid
|
48
|
+
# u.save # Silently ignores changes to :uuid
|
49
|
+
# u.reload # Instead, it restores the original:
|
50
|
+
# u.uuid # => '8bb27724-7439-4965-9598-883419179b21'
|
51
|
+
```
|
52
|
+
|
53
|
+
## Installation
|
54
|
+
|
55
|
+
Add this line to your application's Gemfile:
|
56
|
+
```ruby
|
57
|
+
gem 'uuid_parameter'
|
58
|
+
```
|
59
|
+
|
60
|
+
And then execute:
|
61
|
+
```bash
|
62
|
+
$ bundle
|
63
|
+
```
|
64
|
+
|
65
|
+
Or install it yourself as:
|
66
|
+
```bash
|
67
|
+
$ gem install uuid_parameter
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on Gitlab at
|
73
|
+
https://gitlab.com/incommon.cc/uuid_parameter.
|
74
|
+
|
75
|
+
The [Github repository] is a mirror to facilitate integration with other Rails
|
76
|
+
development, but I don't like Microsoft, and never will. They may show the face
|
77
|
+
they like, they come from enemity and, as far as I'm concerned, will remain
|
78
|
+
there.
|
79
|
+
|
80
|
+
[Github repository]: https://github.com/moners/uuid_parameter
|
81
|
+
|
82
|
+
# Development
|
83
|
+
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
85
|
+
Run specifications with `bundle exec rake`.
|
86
|
+
|
87
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
88
|
+
release a new version, update the version number in `version.rb`, and then run
|
89
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
90
|
+
git commits and tags, and push the `.gem` file to
|
91
|
+
[rubygems.org](https://rubygems.org).
|
92
|
+
See `bundle exec rake -T` for more options.
|
93
|
+
|
94
|
+
## License
|
95
|
+
|
96
|
+
This gem is free software under the same [license] terms as Rails.
|
97
|
+
|
98
|
+
[license]: ./LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'UuidParameter'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
begin
|
19
|
+
require 'bundler/setup'
|
20
|
+
rescue LoadError
|
21
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rdoc/task'
|
25
|
+
|
26
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
27
|
+
rdoc.rdoc_dir = 'rdoc'
|
28
|
+
rdoc.title = 'ActiveRecordFixIntegerLimit'
|
29
|
+
rdoc.options << '--line-numbers'
|
30
|
+
rdoc.rdoc_files.include('README.md')
|
31
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'bundler/gem_tasks'
|
35
|
+
require "rspec/core/rake_task"
|
36
|
+
|
37
|
+
RSpec::Core::RakeTask.new(:spec)
|
38
|
+
|
39
|
+
task :default => :spec
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UUIDParameter
|
4
|
+
# Validate a UUID v4: it must be correctly formatted and cannot change.
|
5
|
+
# This latter responsibility is defined in the UUIDParameter model concern.
|
6
|
+
class UUIDVersion4Validator < ActiveModel::Validator
|
7
|
+
# Note the static '4' in the third group: that's the UUID version.
|
8
|
+
UUID_V4_REGEX = %r[\A[a-f0-9]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}\z]
|
9
|
+
|
10
|
+
def validate(record)
|
11
|
+
unless UUID_V4_REGEX.match?(record.uuid)
|
12
|
+
record.errors.add(:uuid, 'must be a valid random UUID (v4)')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uuid_parameter/uuid_version4_validator'
|
4
|
+
|
5
|
+
# == UUIDParameter
|
6
|
+
#
|
7
|
+
# UUIDParameter module provides support for UUIDs in models. It takes care of
|
8
|
+
# generating (if one was not provided), validating, and keeping this UUID
|
9
|
+
# intact, protecting the :uuid field from being changed once set. Models
|
10
|
+
# including the UUIDParameter module will use their :uuid rather than their :id
|
11
|
+
# for URLs.
|
12
|
+
#
|
13
|
+
module UUIDParameter
|
14
|
+
extend ActiveSupport::Concern
|
15
|
+
included do
|
16
|
+
validates_with UUIDVersion4Validator
|
17
|
+
|
18
|
+
before_validation :assign_uuid, on: :create
|
19
|
+
before_save :keep_existing_uuid
|
20
|
+
|
21
|
+
def to_param
|
22
|
+
uuid.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def assign_uuid
|
28
|
+
self.uuid ||= SecureRandom.uuid
|
29
|
+
end
|
30
|
+
|
31
|
+
def keep_existing_uuid
|
32
|
+
self.uuid = uuid_was if existing_uuid_changed?
|
33
|
+
end
|
34
|
+
|
35
|
+
def existing_uuid_changed?
|
36
|
+
!new_record? && uuid_changed?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uuid_parameter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hellekin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: UUIDParameter handles a :uuid column and validation for any model.
|
70
|
+
email:
|
71
|
+
- hellekin@cepheide.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- lib/uuid_parameter.rb
|
80
|
+
- lib/uuid_parameter/railtie.rb
|
81
|
+
- lib/uuid_parameter/uuid_version4_validator.rb
|
82
|
+
- lib/uuid_parameter/version.rb
|
83
|
+
homepage: https://gitlab.com/incommon.cc/uuid_parameter
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.7.6
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Adds a UUID to an ActiveRecord model.
|
107
|
+
test_files: []
|