activerecord-ulid 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/CHANGELOG.md +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/lib/activerecord/ulid/attribute_default.rb +17 -0
- data/lib/activerecord/ulid/type.rb +31 -0
- data/lib/activerecord/ulid/version.rb +1 -1
- data/lib/activerecord/ulid.rb +6 -3
- metadata +18 -16
- data/lib/activerecord/ulid/ulid_attribute.rb +0 -24
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 06673f10a49138758408e9726a955f0f36a593e957ddb7dd39ef45a6f1cd0c75
|
|
4
|
+
data.tar.gz: 2aafcce35ba85952f3fb6dc07e066415b042eecbedfc2d5ec90350b59332cba8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 730788e32883f470e99bad3e4cc406e21369a9a47300404cab39d5f59046c9945b6522f19b4ac12b48f1d2e5367e03936f33c84226e58d55ecb0fd9ae9941b1e
|
|
7
|
+
data.tar.gz: 3646d1a6ca3d5245855d996b969b3f0c1956ccfc83211460f0e8754c3014c916e0bac74618ac3abe17f7221ff514f84d679b24c1519989b1787de0056d2845d3
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
- Requires Ruby 4.0+ and Active Record 8.1+.
|
|
6
|
+
- `ULIDAttribute` is now `ActiveRecordULID::Type`; the gem no longer defines a top-level constant.
|
|
7
|
+
- Generate each record's ULID through a per-record attribute default. Rails main freezes a model's default attributes at
|
|
8
|
+
schema load, which cast `nil` once and handed every new record the same ULID.
|
|
9
|
+
- Persist prefixed ULID columns when `partial_inserts` is enabled; they used to be skipped as unchanged and read back as
|
|
10
|
+
a new random value.
|
|
11
|
+
- `initial: false` without `nullable: true` no longer raises on `nil`.
|
|
12
|
+
- An explicit `default:` on a `:ulid` attribute is kept as given.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Abdelkader Boudih
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# activerecord-ulid
|
|
2
|
+
|
|
3
|
+
A `:ulid` attribute type for Active Record. Every record gets its own
|
|
4
|
+
[ULID](https://github.com/ulid/spec) the moment it's built: sortable, unique,
|
|
5
|
+
and readable in a URL.
|
|
6
|
+
|
|
7
|
+
Requires Ruby 4.0+ and Active Record 8.1+.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem "activerecord-ulid"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
ULIDs live in string columns:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
create_table :users, id: :string do |t|
|
|
21
|
+
t.string :invite_code
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class User < ApplicationRecord
|
|
25
|
+
attribute :id, :ulid
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
User.new.id # => "01JBQ8Z3X4M6V2N9R7T5K1W0HD"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Prefix them, Stripe style:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
attribute :id, :ulid, prefix: "usr_" # => "usr_01JBQ8Z3X4M6V2N9R7T5K1W0HD"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Options:
|
|
38
|
+
|
|
39
|
+
- `prefix:` prepended to the ULID.
|
|
40
|
+
- `nullable: true` (or `initial: false`) leaves `nil` alone: no ULID is generated.
|
|
41
|
+
- `default:` given explicitly wins over the generated ULID.
|
|
42
|
+
|
|
43
|
+
Assigning `nil` to a generating attribute gives it a fresh ULID.
|
|
44
|
+
|
|
45
|
+
## Development
|
|
46
|
+
|
|
47
|
+
```console
|
|
48
|
+
$ bundle exec rake test # padded_cell, Rails 8.1
|
|
49
|
+
$ RAILS_VERSION=edge bundle exec rake # Rails main
|
|
50
|
+
$ dictator lint .
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT. See [LICENSE.txt](LICENSE.txt).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecordULID
|
|
4
|
+
# Rails casts a model's default attributes once and shares them, so a ULID
|
|
5
|
+
# generated by the type would be the same for every record. A :ulid attribute
|
|
6
|
+
# without an explicit default gets one that generates per record instead.
|
|
7
|
+
module AttributeDefault
|
|
8
|
+
def attribute(name, type = nil, **options)
|
|
9
|
+
type = resolve_type_name(type, **options.except(:default)) if type == :ulid
|
|
10
|
+
if type.is_a?(Type) && type.generates_default? && !options.key?(:default)
|
|
11
|
+
options[:default] = -> { type.generate }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecordULID
|
|
4
|
+
# The :ulid attribute type: a string column holding a ULID, optionally prefixed.
|
|
5
|
+
# A nil value becomes a fresh ULID unless the attribute is nullable or opted out
|
|
6
|
+
# with initial: false.
|
|
7
|
+
class Type < ActiveRecord::Type::String
|
|
8
|
+
attr_reader :prefix, :nullable, :initial
|
|
9
|
+
|
|
10
|
+
def initialize(prefix: nil, nullable: false, initial: true, **)
|
|
11
|
+
@prefix = prefix
|
|
12
|
+
@nullable = nullable
|
|
13
|
+
@initial = initial
|
|
14
|
+
super(**)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def generate
|
|
18
|
+
"#{prefix}#{ULID.generate}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def generates_default?
|
|
22
|
+
initial && !nullable
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def cast(value)
|
|
26
|
+
return value unless value.nil?
|
|
27
|
+
|
|
28
|
+
generate if generates_default?
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/activerecord/ulid.rb
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "ulid/version"
|
|
4
3
|
require "active_record"
|
|
5
4
|
require "ulid"
|
|
6
5
|
|
|
7
|
-
require_relative "ulid/
|
|
8
|
-
|
|
6
|
+
require_relative "ulid/version"
|
|
7
|
+
require_relative "ulid/type"
|
|
8
|
+
require_relative "ulid/attribute_default"
|
|
9
|
+
|
|
10
|
+
ActiveRecord::Type.register(:ulid, ActiveRecordULID::Type)
|
|
11
|
+
ActiveSupport.on_load(:active_record) { singleton_class.prepend(ActiveRecordULID::AttributeDefault) }
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activerecord-ulid
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activerecord
|
|
@@ -16,47 +15,51 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '8.1'
|
|
20
19
|
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '8.1'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: ulid
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - "~>"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '1.4'
|
|
34
33
|
type: :runtime
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - "~>"
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
41
|
-
description: '
|
|
39
|
+
version: '1.4'
|
|
40
|
+
description: 'A :ulid attribute type for Active Record: every record gets its own
|
|
41
|
+
ULID, optionally prefixed, generated when the record is built.'
|
|
42
42
|
email:
|
|
43
43
|
- terminale@gmail.com
|
|
44
44
|
executables: []
|
|
45
45
|
extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
|
47
47
|
files:
|
|
48
|
+
- CHANGELOG.md
|
|
49
|
+
- LICENSE.txt
|
|
50
|
+
- README.md
|
|
48
51
|
- lib/activerecord-ulid.rb
|
|
49
52
|
- lib/activerecord/ulid.rb
|
|
50
|
-
- lib/activerecord/ulid/
|
|
53
|
+
- lib/activerecord/ulid/attribute_default.rb
|
|
54
|
+
- lib/activerecord/ulid/type.rb
|
|
51
55
|
- lib/activerecord/ulid/version.rb
|
|
52
56
|
homepage: https://github.com/seuros/activerecord-ulid
|
|
53
57
|
licenses:
|
|
54
58
|
- MIT
|
|
55
59
|
metadata:
|
|
56
|
-
homepage_uri: https://github.com/seuros/activerecord-ulid
|
|
57
60
|
source_code_uri: https://github.com/seuros/activerecord-ulid
|
|
58
61
|
changelog_uri: https://github.com/seuros/activerecord-ulid/blob/master/CHANGELOG.md
|
|
59
|
-
|
|
62
|
+
rubygems_mfa_required: 'true'
|
|
60
63
|
rdoc_options: []
|
|
61
64
|
require_paths:
|
|
62
65
|
- lib
|
|
@@ -64,15 +67,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
64
67
|
requirements:
|
|
65
68
|
- - ">="
|
|
66
69
|
- !ruby/object:Gem::Version
|
|
67
|
-
version:
|
|
70
|
+
version: '4.0'
|
|
68
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
72
|
requirements:
|
|
70
73
|
- - ">="
|
|
71
74
|
- !ruby/object:Gem::Version
|
|
72
75
|
version: '0'
|
|
73
76
|
requirements: []
|
|
74
|
-
rubygems_version:
|
|
75
|
-
signing_key:
|
|
77
|
+
rubygems_version: 4.0.20
|
|
76
78
|
specification_version: 4
|
|
77
79
|
summary: Add ULID support to ActiveRecord
|
|
78
80
|
test_files: []
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class ULIDAttribute < ActiveRecord::Type::String
|
|
4
|
-
attr_reader :prefix,
|
|
5
|
-
:nullable,
|
|
6
|
-
:initial
|
|
7
|
-
|
|
8
|
-
def initialize(precision: nil, limit: nil, scale: nil, prefix: nil, nullable: false, initial: true)
|
|
9
|
-
@prefix = prefix
|
|
10
|
-
@initial = initial
|
|
11
|
-
@nullable = nullable
|
|
12
|
-
super()
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def cast(value)
|
|
16
|
-
return value if value.present?
|
|
17
|
-
return if value.nil? && nullable
|
|
18
|
-
|
|
19
|
-
value = ULID.generate if value.nil? && initial
|
|
20
|
-
return value if value.empty?
|
|
21
|
-
|
|
22
|
-
[prefix, value].compact.join
|
|
23
|
-
end
|
|
24
|
-
end
|