ulid-rails 1.0.0 → 1.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 +4 -4
- data/.github/workflows/lint.yml +10 -6
- data/.github/workflows/test.yml +19 -10
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +0 -7
- data/README.md +49 -7
- data/gemfiles/5.0.gemfile +7 -3
- data/gemfiles/5.1.gemfile +7 -3
- data/gemfiles/5.2.gemfile +8 -6
- data/gemfiles/6.0.gemfile +8 -6
- data/gemfiles/6.1.gemfile +5 -3
- data/gemfiles/7.0.gemfile +10 -0
- data/lib/ulid/rails/patch.rb +40 -0
- data/lib/ulid/rails/version.rb +1 -1
- data/lib/ulid/rails.rb +8 -0
- data/ulid-rails.gemspec +2 -1
- metadata +21 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f122ca0476d8b8c06f1f4bf08219bdf653d0ba890a3cdb6d9d5d99dfec353424
|
4
|
+
data.tar.gz: 0d1d58ea8a26637db28421610971049cbc9730201c39f9d9444757a65a249a4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c37272d98ec13e144f2aa8c704d3db3140bf672cf3f6d7e76931acc7ab008b0a51cc91a51c402a8dbc14d73da3ce31092ace18e99efcf31b9ad4ff4c5fd6cb16
|
7
|
+
data.tar.gz: d7bcabb538e9f4a588b7d28b79459c06e44c9bfaeffdcaf5e563823908cdad5bcf7de3cd9fd2da20b21062415686e40f5df0904b53e38b9ac367084021616a19
|
data/.github/workflows/lint.yml
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
---
|
2
2
|
name: Linting
|
3
|
-
|
3
|
+
|
4
|
+
on:
|
5
|
+
pull_request:
|
6
|
+
push:
|
7
|
+
branches:
|
8
|
+
- master
|
9
|
+
|
4
10
|
jobs:
|
5
11
|
standardrb:
|
6
|
-
env:
|
7
|
-
AR_VERSION: "5.2"
|
8
12
|
runs-on: ubuntu-latest
|
9
13
|
steps:
|
10
|
-
- uses: actions/checkout@
|
14
|
+
- uses: actions/checkout@v3
|
11
15
|
- uses: ruby/setup-ruby@v1
|
12
16
|
with:
|
13
|
-
ruby-version: 3.
|
17
|
+
ruby-version: "3.1"
|
14
18
|
bundler-cache: true
|
15
|
-
- run: bundle exec
|
19
|
+
- run: bundle exec rubocop
|
data/.github/workflows/test.yml
CHANGED
@@ -1,21 +1,30 @@
|
|
1
1
|
---
|
2
2
|
name: Tests
|
3
|
-
|
3
|
+
|
4
|
+
on:
|
5
|
+
pull_request:
|
6
|
+
push:
|
7
|
+
branches:
|
8
|
+
- master
|
9
|
+
|
4
10
|
jobs:
|
5
11
|
tests:
|
6
12
|
runs-on: ubuntu-latest
|
7
13
|
strategy:
|
14
|
+
fail-fast: false
|
8
15
|
matrix:
|
9
|
-
activerecord-version: ["5.0", "5.1", "5.2", "6.0", "6.1"]
|
10
|
-
ruby-version: ["2.6", "2.7", "3.0"]
|
16
|
+
activerecord-version: ["5.0", "5.1", "5.2", "6.0", "6.1", "7.0"]
|
17
|
+
ruby-version: ["2.6", "2.7", "3.0", "3.1"]
|
11
18
|
exclude:
|
12
|
-
- activerecord-version: "5.0"
|
13
|
-
|
14
|
-
- activerecord-version: "5.1"
|
15
|
-
|
16
|
-
- activerecord-version: "5.2"
|
17
|
-
|
19
|
+
- {activerecord-version: "5.0", ruby-version: "3.0"}
|
20
|
+
- {activerecord-version: "5.0", ruby-version: "3.1"}
|
21
|
+
- {activerecord-version: "5.1", ruby-version: "3.0"}
|
22
|
+
- {activerecord-version: "5.1", ruby-version: "3.1"}
|
23
|
+
- {activerecord-version: "5.2", ruby-version: "3.0"}
|
24
|
+
- {activerecord-version: "5.2", ruby-version: "3.1"}
|
25
|
+
- {activerecord-version: "6.0", ruby-version: "3.1"}
|
26
|
+
- {activerecord-version: "7.0", ruby-version: "2.6"}
|
18
27
|
steps:
|
19
|
-
- uses: actions/checkout@
|
28
|
+
- uses: actions/checkout@v3
|
20
29
|
- name: Test ActiveRecord ${{ matrix.activerecord-version }} and Ruby ${{ matrix.ruby-version }}
|
21
30
|
run: RUBY_VERSION=${{ matrix.ruby-version }} docker-compose run -e AR_VERSION=${{ matrix.activerecord-version }} test
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# ulid-rails CHANGELOG
|
2
2
|
|
3
|
+
## 1.1.0
|
4
|
+
|
5
|
+
- Fix eager loading with limit/offset on models that have ulid primary key. The fix only applies to ActiveRecord 5.2, 6 and 7 (#38).
|
6
|
+
- Add support for Ruby 3.1.
|
7
|
+
- Add support for ActiveRecord 7.
|
8
|
+
|
3
9
|
## 1.0.0
|
4
10
|
|
5
11
|
- Drop support for Rails 4.2.
|
@@ -11,6 +17,7 @@
|
|
11
17
|
## 0.6.0
|
12
18
|
|
13
19
|
- Add support for Rails 4.2, 5.0 and 5.1.
|
20
|
+
- Known Issue: AREL expressions incorrectly serialize ULID values in Rails 4.2 (#27).
|
14
21
|
|
15
22
|
## 0.5.0
|
16
23
|
|
data/Gemfile
CHANGED
@@ -1,9 +1,2 @@
|
|
1
|
-
source "https://rubygems.org"
|
2
|
-
|
3
|
-
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in ulid-rails.gemspec
|
6
|
-
gemspec
|
7
|
-
|
8
1
|
version = ENV["AR_VERSION"] || "6.0"
|
9
2
|
eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__)
|
data/README.md
CHANGED
@@ -2,24 +2,28 @@
|
|
2
2
|
|
3
3
|
This gem makes it possible to use [ULID](https://github.com/ulid/spec) for DB in a Ruby on Rails app.
|
4
4
|
|
5
|
-
|
6
5
|
## Installation
|
7
6
|
|
8
|
-
|
9
7
|
```ruby
|
10
8
|
gem 'ulid-rails'
|
11
9
|
```
|
12
10
|
|
13
11
|
And then execute:
|
14
12
|
|
15
|
-
|
13
|
+
```
|
14
|
+
$ bundle
|
15
|
+
```
|
16
16
|
|
17
17
|
Or install it yourself as:
|
18
18
|
|
19
|
-
|
19
|
+
```
|
20
|
+
$ gem install ulid-rails
|
21
|
+
```
|
20
22
|
|
21
23
|
## Usage
|
22
24
|
|
25
|
+
First, load up the gem with `require 'ulid/rails'`.
|
26
|
+
|
23
27
|
### Migrations
|
24
28
|
|
25
29
|
Specify `id: false` to `create_table` and add `id` column as 16-byte binary type.
|
@@ -27,13 +31,12 @@ Specify `id: false` to `create_table` and add `id` column as 16-byte binary type
|
|
27
31
|
```ruby
|
28
32
|
def change
|
29
33
|
create_table :users, id: false do |t|
|
30
|
-
t.binary :id, limit: 16,
|
34
|
+
t.binary :id, limit: 16, auto_generate: true
|
31
35
|
# ...
|
32
36
|
end
|
33
37
|
end
|
34
38
|
```
|
35
39
|
|
36
|
-
|
37
40
|
### Model Changes
|
38
41
|
|
39
42
|
Just add the below lines to your models.
|
@@ -72,7 +75,7 @@ A virtual column is useful if you want to add index on the timestamp column or w
|
|
72
75
|
|
73
76
|
```ruby
|
74
77
|
create_table :users, id: false do |t|
|
75
|
-
t.binary :id, limit: 16,
|
78
|
+
t.binary :id, limit: 16, auto_generate: true
|
76
79
|
t.datetime :updated_at
|
77
80
|
t.virtual_ulid_timestamp :created_at, :id
|
78
81
|
end
|
@@ -103,6 +106,41 @@ You need to specicfy `type` option
|
|
103
106
|
end
|
104
107
|
```
|
105
108
|
|
109
|
+
### Many to many associations
|
110
|
+
|
111
|
+
Please note that this library doesn't work properly with `has_and_belongs_to_many` associations.
|
112
|
+
|
113
|
+
Our recommendation is to be explicit and instead use the `has_many, through: join_class` association.
|
114
|
+
Notice that for it to work properly you must specify the `has_many` to the join class in the main classes of the association,
|
115
|
+
and your join class must have `belongs_to` main classes defined as shown in the example below:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
class User < ActiveRecord::Base
|
119
|
+
include ULID::Rails
|
120
|
+
ulid :id, auto_generate: true
|
121
|
+
|
122
|
+
has_many :user_articles
|
123
|
+
has_many :articles, through: :user_articles
|
124
|
+
end
|
125
|
+
|
126
|
+
class UserArticle < ActiveRecord::Base
|
127
|
+
include ULID::Rails
|
128
|
+
ulid :id, auto_generate: true
|
129
|
+
ulid :user_id
|
130
|
+
ulid :article_id
|
131
|
+
|
132
|
+
belongs_to :user
|
133
|
+
belongs_to :article
|
134
|
+
end
|
135
|
+
|
136
|
+
class Article < ActiveRecord::Base
|
137
|
+
include ULID::Rails
|
138
|
+
ulid :id, auto_generate: true
|
139
|
+
|
140
|
+
has_many :user_articles
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
106
144
|
## Development
|
107
145
|
|
108
146
|
### Run tests
|
@@ -125,6 +163,10 @@ Or run tests locally, without docker-compose
|
|
125
163
|
$ AR_VERSION=4.2 bundle update && AR_VERSION=4.2 bundle exec rake test
|
126
164
|
```
|
127
165
|
|
166
|
+
## Known issues
|
167
|
+
|
168
|
+
- ActiveRecord 5.0 and 5.1 do not work properly with some data association loading methods. For example, eager loading with limit/offset on a model that has a ulid ID. Refer to test cases that are skiped for AR 5.0 and 5.1.
|
169
|
+
|
128
170
|
## License
|
129
171
|
|
130
172
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/gemfiles/5.0.gemfile
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec path: ".."
|
4
|
+
|
5
|
+
gem "activesupport", "~> 5.0.0"
|
6
|
+
gem "activemodel", "~> 5.0.0"
|
7
|
+
gem "activerecord", "~> 5.0.0"
|
4
8
|
gem "sqlite3", "~> 1.3.6"
|
5
9
|
gem "mysql2", ">= 0.3.18", "< 0.6.0"
|
6
10
|
gem "pg", ">= 0.18", "< 2.0"
|
data/gemfiles/5.1.gemfile
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec path: ".."
|
4
|
+
|
5
|
+
gem "activesupport", "~> 5.1.0"
|
6
|
+
gem "activemodel", "~> 5.1.0"
|
7
|
+
gem "activerecord", "~> 5.1.0"
|
4
8
|
gem "sqlite3", "~> 1.3", ">= 1.3.6"
|
5
9
|
gem "mysql2", ">= 0.3.18", "< 0.6.0"
|
6
10
|
gem "pg", ">= 0.18", "< 2.0"
|
data/gemfiles/5.2.gemfile
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "
|
6
|
-
gem "
|
7
|
-
gem "
|
8
|
-
gem "
|
3
|
+
gemspec path: ".."
|
4
|
+
|
5
|
+
gem "activesupport", "~> 5.2.0"
|
6
|
+
gem "activemodel", "~> 5.2.0"
|
7
|
+
gem "activerecord", "~> 5.2.0"
|
8
|
+
gem "sqlite3", "~> 1.3", ">= 1.3.6"
|
9
|
+
gem "mysql2", ">= 0.4.4", "< 0.6.0"
|
10
|
+
gem "pg", ">= 0.18", "< 2.0"
|
data/gemfiles/6.0.gemfile
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "
|
6
|
-
gem "
|
7
|
-
gem "
|
8
|
-
gem "
|
3
|
+
gemspec path: ".."
|
4
|
+
|
5
|
+
gem "activesupport", "~> 6.0.0"
|
6
|
+
gem "activemodel", "~> 6.0.0"
|
7
|
+
gem "activerecord", "~> 6.0.0"
|
8
|
+
gem "sqlite3", "~> 1.4"
|
9
|
+
gem "mysql2", ">= 0.4.4"
|
10
|
+
gem "pg", ">= 0.18", "< 2.0"
|
data/gemfiles/6.1.gemfile
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
|
3
|
+
gemspec path: ".."
|
4
|
+
|
3
5
|
gem "activesupport", "~> 6.1.0"
|
4
6
|
gem "activemodel", "~> 6.1.0"
|
5
7
|
gem "activerecord", "~> 6.1.0"
|
6
|
-
gem "sqlite3", "~> 1.4
|
7
|
-
gem "mysql2"
|
8
|
-
gem "pg"
|
8
|
+
gem "sqlite3", "~> 1.4"
|
9
|
+
gem "mysql2", "~> 0.5"
|
10
|
+
gem "pg", "~> 1.1"
|
data/lib/ulid/rails/patch.rb
CHANGED
@@ -8,6 +8,46 @@ module ULID
|
|
8
8
|
as: "FROM_UNIXTIME(CONV(HEX(#{ulid_column_name} >> 80), 16, 10) / 1000.0)"
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
module FinderMethods
|
13
|
+
def limited_ids_for(relation)
|
14
|
+
id_rows = super
|
15
|
+
if klass.attribute_types[primary_key].is_a? ULID::Rails::Type
|
16
|
+
id_rows.map do |id|
|
17
|
+
klass.attribute_types[primary_key].deserialize id
|
18
|
+
end
|
19
|
+
else
|
20
|
+
id_rows
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module SchemaStatements
|
26
|
+
def distinct_relation_for_primary_key(relation) # :nodoc:
|
27
|
+
values = columns_for_distinct(
|
28
|
+
visitor.compile(relation.table[relation.primary_key]),
|
29
|
+
relation.order_values
|
30
|
+
)
|
31
|
+
|
32
|
+
limited = relation.reselect(values).distinct!
|
33
|
+
limited_ids = select_rows(limited.arel, "SQL").map(&:last)
|
34
|
+
|
35
|
+
if relation.klass.attribute_types[relation.primary_key].is_a? ULID::Rails::Type
|
36
|
+
limited_ids.map! do |id|
|
37
|
+
relation.klass.attribute_types[relation.primary_key].deserialize id
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
if limited_ids.empty?
|
42
|
+
relation.none!
|
43
|
+
else
|
44
|
+
relation.where!(relation.primary_key => limited_ids)
|
45
|
+
end
|
46
|
+
|
47
|
+
relation.limit_value = relation.offset_value = nil
|
48
|
+
relation
|
49
|
+
end
|
50
|
+
end
|
11
51
|
end
|
12
52
|
end
|
13
53
|
end
|
data/lib/ulid/rails/version.rb
CHANGED
data/lib/ulid/rails.rb
CHANGED
@@ -46,5 +46,13 @@ module ULID
|
|
46
46
|
|
47
47
|
ActiveModel::Type.register(:ulid, ULID::Rails::Type)
|
48
48
|
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Patch::Migrations
|
49
|
+
case ActiveRecord::VERSION::MAJOR
|
50
|
+
when 5
|
51
|
+
ActiveRecord::FinderMethods.prepend(Patch::FinderMethods) unless ActiveRecord::VERSION::MINOR < 2
|
52
|
+
when 6
|
53
|
+
ActiveRecord::FinderMethods.prepend(Patch::FinderMethods)
|
54
|
+
when 7
|
55
|
+
ActiveRecord::ConnectionAdapters::SchemaStatements.prepend(Patch::SchemaStatements)
|
56
|
+
end
|
49
57
|
end
|
50
58
|
end
|
data/ulid-rails.gemspec
CHANGED
@@ -38,5 +38,6 @@ Gem::Specification.new do |spec|
|
|
38
38
|
spec.add_development_dependency "bundler"
|
39
39
|
spec.add_development_dependency "rake"
|
40
40
|
spec.add_development_dependency "minitest", "~> 5.0"
|
41
|
-
spec.add_development_dependency "
|
41
|
+
spec.add_development_dependency "rubocop-minitest"
|
42
|
+
spec.add_development_dependency "standard", "~> 1.16.0"
|
42
43
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ulid-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kazunori Kajihiro
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-12-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ulid
|
@@ -123,20 +123,34 @@ dependencies:
|
|
123
123
|
- - "~>"
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '5.0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: rubocop-minitest
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
126
140
|
- !ruby/object:Gem::Dependency
|
127
141
|
name: standard
|
128
142
|
requirement: !ruby/object:Gem::Requirement
|
129
143
|
requirements:
|
130
144
|
- - "~>"
|
131
145
|
- !ruby/object:Gem::Version
|
132
|
-
version: 1.
|
146
|
+
version: 1.16.0
|
133
147
|
type: :development
|
134
148
|
prerelease: false
|
135
149
|
version_requirements: !ruby/object:Gem::Requirement
|
136
150
|
requirements:
|
137
151
|
- - "~>"
|
138
152
|
- !ruby/object:Gem::Version
|
139
|
-
version: 1.
|
153
|
+
version: 1.16.0
|
140
154
|
description: ULID for rails
|
141
155
|
email:
|
142
156
|
- kazunori.kajihiro@gmail.com
|
@@ -149,6 +163,7 @@ files:
|
|
149
163
|
- ".github/workflows/lint.yml"
|
150
164
|
- ".github/workflows/test.yml"
|
151
165
|
- ".gitignore"
|
166
|
+
- ".rubocop.yml"
|
152
167
|
- ".standard.yml"
|
153
168
|
- CHANGELOG.md
|
154
169
|
- Gemfile
|
@@ -164,6 +179,7 @@ files:
|
|
164
179
|
- gemfiles/5.2.gemfile
|
165
180
|
- gemfiles/6.0.gemfile
|
166
181
|
- gemfiles/6.1.gemfile
|
182
|
+
- gemfiles/7.0.gemfile
|
167
183
|
- lib/ulid/rails.rb
|
168
184
|
- lib/ulid/rails/errors.rb
|
169
185
|
- lib/ulid/rails/formatter.rb
|
@@ -195,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
211
|
- !ruby/object:Gem::Version
|
196
212
|
version: '0'
|
197
213
|
requirements: []
|
198
|
-
rubygems_version: 3.
|
214
|
+
rubygems_version: 3.1.6
|
199
215
|
signing_key:
|
200
216
|
specification_version: 4
|
201
217
|
summary: ULID for rails
|