ulid-rails 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/.gitignore +1 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile +3 -0
- data/README.md +38 -8
- data/gemfiles/5.2.gemfile +4 -0
- data/gemfiles/6.0.gemfile +4 -0
- data/lib/ulid/rails.rb +13 -8
- data/lib/ulid/rails/formatter.rb +1 -9
- data/lib/ulid/rails/type.rb +2 -0
- data/lib/ulid/rails/version.rb +1 -1
- metadata +5 -3
- data/Gemfile.lock +0 -49
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d66abad8321b174f9037f061da19837f539c4d42d01d328f267710023a7fc1d
|
4
|
+
data.tar.gz: df85e60420b30edf90ad5790cf1c70c338950e8b062363c10c29309cff03ecfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab2ffb0c44b887852eb56a6fe0b4b93f9881e06f6a8ce6fe6322f28debb90a1b922d7aaff15ecc4296171addd7e73f34c094d7eefde0d451d5554011e821e7e6
|
7
|
+
data.tar.gz: c32c054dd8f14849f8b96d8ed0b6f83eb6be4006bf2c4c87cf81da381ae902f2a9e36d638a1bb317630c29f853b2aa5ad355e77fdb78de3793a25ed80f12b6d7
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# ulid-rails CHANGELOG
|
2
|
+
## 0.2
|
3
|
+
|
4
|
+
### Breaking Changes
|
5
|
+
|
6
|
+
- `primary_key` option's default has been changed to `false`. Now you need to specify `primary_key: true` for primary keys: `ulid :id, primary_key: true`
|
7
|
+
|
8
|
+
### New Features
|
9
|
+
|
10
|
+
- Added `auto_generate` option
|
11
|
+
|
12
|
+
### Bug Fixes
|
13
|
+
|
14
|
+
- Auto-generate was on for foreign keys
|
15
|
+
- Encoded ULID was not 0-padded #1
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -51,7 +51,23 @@ Just add the below lines to your models.
|
|
51
51
|
```ruby
|
52
52
|
class MyModel < ApplicationRecord
|
53
53
|
include ULID::Rails
|
54
|
-
ulid :id # The argument is the
|
54
|
+
ulid :id, primary_key: true # The first argument is the ULID column name
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
### Extract timestamp
|
59
|
+
|
60
|
+
Since ULID includes milli seconds precision timestamp, you don't need to store `created_at`.
|
61
|
+
`ulid-rails` provides a helper method that defines timestamp method which extract timestamp from ULID column.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class MyModel < ApplicationRecord
|
65
|
+
include ULID::Rails
|
66
|
+
ulid :id, primary_key: true # The first argument is the ULID column name
|
67
|
+
|
68
|
+
# defines `created_at` method which extract timestamp value from id column.
|
69
|
+
# This way you don't need physical `created_at` column.
|
70
|
+
ulid_extract_timestamp :id, :created_at
|
55
71
|
end
|
56
72
|
```
|
57
73
|
|
@@ -59,20 +75,34 @@ end
|
|
59
75
|
|
60
76
|
**MySQL Only (for now)**
|
61
77
|
|
62
|
-
|
63
|
-
|
78
|
+
You can define a "virtual column" in MySQL DB that acts same as a physical column.
|
64
79
|
Defining the virtual `created_at` is kind of comlicated so this gem provides a helper method for it.
|
65
80
|
|
81
|
+
A virtual column is useful if you want to add index on the timestamp column or want to execute raw SQL with created_at.
|
82
|
+
|
66
83
|
```ruby
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
84
|
+
create_table :users, id: false do |t|
|
85
|
+
t.binary :id, limit: 16, primary_key: true
|
86
|
+
t.datetime :updated_at
|
87
|
+
t.virtual_ulid_timestamp :created_at, :id
|
88
|
+
end
|
72
89
|
```
|
73
90
|
|
74
91
|
`virtual_ulid_timestamp` takes two arguments, the first one is the name of the column name (typically, `created_at`) and the second one is the ULID column that creation timestamp is extracted from.
|
75
92
|
|
93
|
+
### Auto-generate ULID
|
94
|
+
|
95
|
+
If `primary_key` is `true`, ULID is auto-generated before create by default.
|
96
|
+
You can enable or disable auto-generation with `auto_generate` option.
|
97
|
+
|
98
|
+
```
|
99
|
+
class Model < ApplicationRecord
|
100
|
+
ulid :id, primary_key: true # primary key. auto-generate enabled
|
101
|
+
ulid :foreign_key # auto-generate disabled
|
102
|
+
ulid :ulid, auto_generate: true # non primary, auto-generate enabled
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
76
106
|
## FAQ
|
77
107
|
|
78
108
|
### Foreign Keys
|
data/lib/ulid/rails.rb
CHANGED
@@ -12,25 +12,30 @@ module ULID
|
|
12
12
|
extend ActiveSupport::Concern
|
13
13
|
|
14
14
|
class_methods do
|
15
|
-
def ulid(column_name
|
15
|
+
def ulid(column_name, primary_key: false, auto_generate: nil)
|
16
16
|
attribute column_name, ULID::Rails::Type.new
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
auto_generate = auto_generate.nil? ? primary_key : false
|
19
|
+
if auto_generate
|
20
|
+
before_create do
|
21
|
+
send("#{column_name}=", ULID.generate) if send(column_name).nil?
|
22
|
+
end
|
20
23
|
end
|
21
24
|
|
22
|
-
|
23
|
-
define_method
|
25
|
+
def ulid_extract_timestamp(ulid_column, timestamp_column = :created_at)
|
26
|
+
define_method timestamp_column do
|
24
27
|
at = super() rescue nil
|
25
|
-
if !at && (id_val = send(
|
28
|
+
if !at && (id_val = send(ulid_column))
|
26
29
|
Time.zone.at((Base32::Crockford.decode(id_val) >> 80) / 1000.0)
|
27
30
|
else
|
28
31
|
at
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
32
|
-
|
33
|
-
|
35
|
+
if timestamp_column.to_s == "created_at"
|
36
|
+
define_singleton_method(:timestamp_attributes_for_create) do
|
37
|
+
[]
|
38
|
+
end
|
34
39
|
end
|
35
40
|
end
|
36
41
|
end
|
data/lib/ulid/rails/formatter.rb
CHANGED
@@ -4,20 +4,12 @@ module ULID
|
|
4
4
|
module Rails
|
5
5
|
module Formatter
|
6
6
|
def self.format(v)
|
7
|
-
|
8
|
-
Base32::Crockford.encode(v.hex)
|
9
|
-
else
|
10
|
-
v
|
11
|
-
end
|
7
|
+
v.length == 32 ? Base32::Crockford.encode(v.hex).rjust(26, "0") : v
|
12
8
|
end
|
13
9
|
|
14
10
|
def self.unformat(v)
|
15
11
|
Base32::Crockford.decode(v).to_s(16).rjust(32, "0")
|
16
12
|
end
|
17
|
-
|
18
|
-
def self.regexp
|
19
|
-
/\A[[:alnum:]]{25}\z/
|
20
|
-
end
|
21
13
|
end
|
22
14
|
end
|
23
15
|
end
|
data/lib/ulid/rails/type.rb
CHANGED
data/lib/ulid/rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ulid-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kazunori Kajihiro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ulid
|
@@ -131,13 +131,15 @@ extra_rdoc_files: []
|
|
131
131
|
files:
|
132
132
|
- ".gitignore"
|
133
133
|
- ".travis.yml"
|
134
|
+
- CHANGELOG.md
|
134
135
|
- Gemfile
|
135
|
-
- Gemfile.lock
|
136
136
|
- LICENSE.txt
|
137
137
|
- README.md
|
138
138
|
- Rakefile
|
139
139
|
- bin/console
|
140
140
|
- bin/setup
|
141
|
+
- gemfiles/5.2.gemfile
|
142
|
+
- gemfiles/6.0.gemfile
|
141
143
|
- lib/ulid/rails.rb
|
142
144
|
- lib/ulid/rails/formatter.rb
|
143
145
|
- lib/ulid/rails/patch.rb
|
data/Gemfile.lock
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
ulid-rails (0.1.0)
|
5
|
-
activemodel (>= 5.0)
|
6
|
-
activerecord (>= 5.0)
|
7
|
-
activesupport (>= 5.0)
|
8
|
-
base32-crockford (~> 0.1)
|
9
|
-
ulid (~> 1.0)
|
10
|
-
|
11
|
-
GEM
|
12
|
-
remote: https://rubygems.org/
|
13
|
-
specs:
|
14
|
-
activemodel (5.2.1)
|
15
|
-
activesupport (= 5.2.1)
|
16
|
-
activerecord (5.2.1)
|
17
|
-
activemodel (= 5.2.1)
|
18
|
-
activesupport (= 5.2.1)
|
19
|
-
arel (>= 9.0)
|
20
|
-
activesupport (5.2.1)
|
21
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
22
|
-
i18n (>= 0.7, < 2)
|
23
|
-
minitest (~> 5.1)
|
24
|
-
tzinfo (~> 1.1)
|
25
|
-
arel (9.0.0)
|
26
|
-
base32-crockford (0.1.0)
|
27
|
-
concurrent-ruby (1.0.5)
|
28
|
-
i18n (1.1.0)
|
29
|
-
concurrent-ruby (~> 1.0)
|
30
|
-
minitest (5.11.3)
|
31
|
-
rake (10.5.0)
|
32
|
-
sysrandom (1.0.5)
|
33
|
-
thread_safe (0.3.6)
|
34
|
-
tzinfo (1.2.5)
|
35
|
-
thread_safe (~> 0.1)
|
36
|
-
ulid (1.0.0)
|
37
|
-
sysrandom (>= 1.0.0, < 2.0)
|
38
|
-
|
39
|
-
PLATFORMS
|
40
|
-
ruby
|
41
|
-
|
42
|
-
DEPENDENCIES
|
43
|
-
bundler (~> 1.16)
|
44
|
-
minitest (~> 5.0)
|
45
|
-
rake (~> 10.0)
|
46
|
-
ulid-rails!
|
47
|
-
|
48
|
-
BUNDLED WITH
|
49
|
-
1.16.4
|