ulid-rails 1.1.1 → 2.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5af3537525b91e1c92af1b6ea6bdc8a5a872943a1cb5c8d895f50addb0ae7508
4
- data.tar.gz: 0032d5ad04a924b166bbcf10b1fa9d01a5222aa57175fca0b645dd5df05eee70
3
+ metadata.gz: 4fe6dfad2124c31cc7fbd3b744c8309d3abbd1fe8b3f0f737e87730d48e4a7ad
4
+ data.tar.gz: 5c94f88160609b46cb584c5e802e1a8fbfc3ae92e75e08277ca50c4f5693d24b
5
5
  SHA512:
6
- metadata.gz: 3d2855d4fd84b86d85191104aeee9e8574bf3a29bf095c4fec622c611db9c19694db182476d9bd89c2f78f98193c6b8f2fbadf4045fe6240a0d0f0091f30ff19
7
- data.tar.gz: 02ebc48788b7d39f5d1684802a2941b10e237d880885c63e311d76493d02f6a5956ba98b54c77c9d54014eec0306ac4df9f91d78324279c621623500bbaf002b
6
+ metadata.gz: 0f39ff3a403a16b42ea3ee18a82938b266998f936dabe319084d52dedc2a2260761b92ed3273b8b074c0ee95b30979eef30b41abfa8db73ea16b0436652f1b26
7
+ data.tar.gz: a836d749d63b71b66cc310262db3c3cd529b5378e17c3ef5c265503c695d446fdc954df36f9c74b828ab423620e707138877322db1546b1d6fc3f3e7c4b986aa
@@ -15,19 +15,10 @@ jobs:
15
15
  strategy:
16
16
  fail-fast: false
17
17
  matrix:
18
- activerecord-version: ["5.0", "5.1", "5.2", "6.0", "6.1", "7.0", "7.0.5"]
18
+ activerecord-version: ["6.0", "6.1", "7.0", "7.0.5"]
19
19
  ruby-version: ["2.7", "3.0", "3.1", "3.2"]
20
- exclude:
21
- - {activerecord-version: "5.0", ruby-version: "3.0"}
22
- - {activerecord-version: "5.0", ruby-version: "3.1"}
23
- - {activerecord-version: "5.0", ruby-version: "3.2"}
24
- - {activerecord-version: "5.1", ruby-version: "3.0"}
25
- - {activerecord-version: "5.1", ruby-version: "3.1"}
26
- - {activerecord-version: "5.1", ruby-version: "3.2"}
27
- - {activerecord-version: "5.2", ruby-version: "3.0"}
28
- - {activerecord-version: "5.2", ruby-version: "3.1"}
29
- - {activerecord-version: "5.2", ruby-version: "3.2"}
30
- - {activerecord-version: "6.0", ruby-version: "3.1"}
20
+ include:
21
+ - {activerecord-version: "5.2", ruby-version: "2.7"}
31
22
  steps:
32
23
  - uses: actions/checkout@v3
33
24
  - run: docker-compose run test
data/.rubocop.yml CHANGED
@@ -10,3 +10,4 @@ AllCops:
10
10
  NewCops: enable
11
11
  Exclude:
12
12
  - 'vendor/**/*'
13
+ TargetRubyVersion: "2.7"
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 2.0.0.pre.1
6
+
7
+ - Drop support for Rails 5.0 and Rails 5.1.
8
+ - Fix various issues when calling `#where` with non-String values, or multiple ULID values.
9
+
5
10
  ## 1.1.1
6
11
 
7
12
  - Drop support for ruby 2.6.
data/README.md CHANGED
@@ -162,13 +162,9 @@ $ docker-compose run -e AR_VERSION=6.1 test
162
162
  Or run tests locally, without docker-compose
163
163
 
164
164
  ```
165
- $ AR_VERSION=4.2 bundle update && AR_VERSION=4.2 bundle exec rake test
165
+ $ AR_VERSION=6.1 bundle update && AR_VERSION=6.1 bundle exec rake test
166
166
  ```
167
167
 
168
- ## Known issues
169
-
170
- - 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.
171
-
172
168
  ## License
173
169
 
174
170
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -8,46 +8,6 @@ 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
51
11
  end
52
12
  end
53
13
  end
@@ -0,0 +1,39 @@
1
+ require_relative "type"
2
+
3
+ module ULID
4
+ module Rails
5
+ class PostgresqlType < Type
6
+ class Data < ULID::Rails::Type::Data
7
+ def hex
8
+ hexed = super
9
+ return nil if hexed.nil?
10
+ [hexed].pack("H*")
11
+ end
12
+
13
+ alias_method :to_s, :hex
14
+ end
15
+
16
+ # Inspired by active_record/connection_adapters/postgresql/oid/bytea
17
+ def deserialize(value)
18
+ case value
19
+ when nil
20
+ nil
21
+ when ULID::Rails::Type::Data
22
+ super(value.value)
23
+ else
24
+ super(PG::Connection.unescape_bytea(value))
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def cast_string_to_ulid(value)
31
+ if value.is_a?(String) && value.length == 34 && value.start_with?("\\x")
32
+ Data.from_serialized(value[2..])
33
+ else
34
+ super(value, data_class: PostgresqlType::Data)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ require_relative "type"
2
+
3
+ module ULID
4
+ module Rails
5
+ class SqliteType < Type
6
+ class Data < ULID::Rails::Type::Data
7
+ alias_method :to_s, :hex
8
+ end
9
+
10
+ private
11
+
12
+ def cast_string_to_ulid(value)
13
+ if value.is_a?(String) && value.length == 32
14
+ Data.from_serialized(value)
15
+ else
16
+ super(value, data_class: SqliteType::Data)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,68 +1,78 @@
1
1
  require "active_model/type"
2
- require "ulid/rails/formatter"
3
- require "ulid/rails/validator"
2
+ require "base32/crockford"
4
3
  require "ulid/rails/errors"
5
4
 
6
5
  module ULID
7
6
  module Rails
8
7
  class Type < ActiveModel::Type::Binary
9
- class Data < ActiveModel::Type::Binary::Data
10
- alias_method :hex, :to_s
8
+ def assert_valid_value(value)
9
+ raise ArgumentError, "`#{value}` is not a ULID format" unless Data.valid_ulid?(value)
11
10
  end
12
11
 
13
- def initialize(formatter = Formatter, validator = Validator)
14
- @formatter = formatter
15
- @validator = validator
16
- super()
12
+ def cast(value)
13
+ return nil if value.nil?
14
+
15
+ str = value.is_a?(Data) ? value.value : value
16
+
17
+ cast_string_to_ulid(str).value
17
18
  end
18
19
 
19
- def assert_valid_value(value)
20
- raise ArgumentError, "`#{value}` is not a ULID format" unless @validator.is_valid?(value)
20
+ def serialize(value)
21
+ return value if value.is_a?(Data)
22
+ return Data.null unless value.is_a?(String)
23
+
24
+ cast_string_to_ulid(value)
21
25
  end
22
26
 
23
27
  def deserialize(value)
24
28
  return nil if value.nil?
25
29
 
26
- case adapter
27
- when "mysql2"
28
- if value.is_a?(Data)
29
- value = value.to_s
30
- elsif value.is_a?(String)
31
- value = value.unpack1("H*")
32
- end
33
- when "postgresql"
34
- if value.is_a?(Data)
35
- value = value.to_s
36
- value = value.unpack1("H*")
37
- end
38
- value = value[2..-1] if value.start_with?("\\x")
39
- when "sqlite3"
40
- if value.is_a?(Data)
41
- value = value.to_s
42
- end
43
- end
44
-
45
- value.length == 32 ? @formatter.format(value) : super
30
+ super
46
31
  end
47
32
 
48
- def serialize(value)
49
- return if value.nil?
33
+ private
34
+
35
+ def cast_string_to_ulid(value, data_class: Data)
36
+ raise ArgumentError if !value.is_a?(String)
50
37
 
51
- case adapter
52
- when "mysql2", "sqlite3"
53
- Data.new(@formatter.unformat(value))
54
- when "postgresql"
55
- Data.new([@formatter.unformat(value)].pack("H*"))
38
+ if data_class.valid_ulid?(value)
39
+ data_class.new(value)
40
+ else
41
+ data = value.unpack1("H*")
42
+ data_class.from_serialized(data)
56
43
  end
57
44
  end
58
45
 
59
- private
46
+ class Data < ActiveModel::Type::Binary::Data
47
+ def self.null
48
+ new(nil)
49
+ end
60
50
 
61
- def adapter
62
- if ::ActiveRecord::Base.respond_to?(:connection_db_config)
63
- ::ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
64
- else
65
- ::ActiveRecord::Base.connection_config[:adapter]
51
+ def self.from_serialized(data)
52
+ deserialized = Base32::Crockford.encode(data.hex).rjust(26, "0")
53
+ new(deserialized)
54
+ end
55
+
56
+ def self.valid_ulid?(str)
57
+ return true if str.nil?
58
+
59
+ str.length == 26 && Base32::Crockford.valid?(str)
60
+ end
61
+
62
+ def initialize(value)
63
+ @value = nil
64
+ super if self.class.valid_ulid?(value)
65
+ end
66
+
67
+ attr_reader :value
68
+
69
+ def hex
70
+ return nil if @value.nil?
71
+
72
+ hexed = Base32::Crockford.decode(@value).to_s(16).rjust(32, "0")
73
+ raise ArgumentError if hexed.length > 32
74
+
75
+ hexed
66
76
  end
67
77
  end
68
78
  end
@@ -1,5 +1,5 @@
1
1
  module ULID
2
2
  module Rails
3
- VERSION = "1.1.1"
3
+ VERSION = "2.0.0.pre.1"
4
4
  end
5
5
  end
data/lib/ulid/rails.rb CHANGED
@@ -5,6 +5,8 @@ require "ulid"
5
5
  require "base32/crockford"
6
6
  require "ulid/rails/version"
7
7
  require "ulid/rails/type"
8
+ require "ulid/rails/postgresql_type"
9
+ require "ulid/rails/sqlite_type"
8
10
  require "ulid/rails/patch"
9
11
 
10
12
  module ULID
@@ -13,7 +15,7 @@ module ULID
13
15
 
14
16
  class_methods do
15
17
  def ulid(column_name, auto_generate: false)
16
- attribute column_name, ULID::Rails::Type.new
18
+ attribute column_name, :ulid
17
19
 
18
20
  if auto_generate
19
21
  before_create do
@@ -44,15 +46,10 @@ module ULID
44
46
  end
45
47
  end
46
48
 
47
- ActiveModel::Type.register(:ulid, ULID::Rails::Type)
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
+ ActiveRecord::Type.register(:ulid, ULID::Rails::Type, override: false)
50
+ ActiveRecord::Type.register(:ulid, ULID::Rails::PostgresqlType, adapter: :postgresql)
51
+ ActiveRecord::Type.register(:ulid, ULID::Rails::SqliteType, adapter: :sqlite)
52
+ ActiveRecord::Type.register(:ulid, ULID::Rails::SqliteType, adapter: :sqlite3)
53
+ ActiveRecord::ConnectionAdapters::TableDefinition.include(Patch::Migrations)
57
54
  end
58
55
  end
data/ulid-rails.gemspec CHANGED
@@ -32,10 +32,11 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.add_dependency "ulid", "~> 1.0"
34
34
  spec.add_dependency "base32-crockford", "~> 0.1"
35
- spec.add_dependency "activesupport", ">= 5.0"
36
- spec.add_dependency "activemodel", ">= 5.0"
37
- spec.add_dependency "activerecord", ">= 5.0"
35
+ spec.add_dependency "activesupport", ">= 5.2"
36
+ spec.add_dependency "activemodel", ">= 5.2"
37
+ spec.add_dependency "activerecord", ">= 5.2"
38
38
  spec.add_development_dependency "bundler"
39
+ spec.add_development_dependency "pry-byebug"
39
40
  spec.add_development_dependency "rake"
40
41
  spec.add_development_dependency "minitest", "~> 5.0"
41
42
  spec.add_development_dependency "rubocop-minitest"
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.1.1
4
+ version: 2.0.0.pre.1
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: 2023-05-25 00:00:00.000000000 Z
12
+ date: 2023-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ulid
@@ -45,42 +45,42 @@ dependencies:
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: '5.0'
48
+ version: '5.2'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
- version: '5.0'
55
+ version: '5.2'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: activemodel
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: '5.0'
62
+ version: '5.2'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: '5.0'
69
+ version: '5.2'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: activerecord
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
75
75
  - !ruby/object:Gem::Version
76
- version: '5.0'
76
+ version: '5.2'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: '5.0'
83
+ version: '5.2'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: bundler
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry-byebug
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
98
112
  - !ruby/object:Gem::Dependency
99
113
  name: rake
100
114
  requirement: !ruby/object:Gem::Requirement
@@ -174,8 +188,6 @@ files:
174
188
  - bin/run_tests
175
189
  - bin/setup
176
190
  - docker-compose.yml
177
- - gemfiles/5.0.gemfile
178
- - gemfiles/5.1.gemfile
179
191
  - gemfiles/5.2.gemfile
180
192
  - gemfiles/6.0.gemfile
181
193
  - gemfiles/6.1.gemfile
@@ -183,10 +195,10 @@ files:
183
195
  - gemfiles/7.0.gemfile
184
196
  - lib/ulid/rails.rb
185
197
  - lib/ulid/rails/errors.rb
186
- - lib/ulid/rails/formatter.rb
187
198
  - lib/ulid/rails/patch.rb
199
+ - lib/ulid/rails/postgresql_type.rb
200
+ - lib/ulid/rails/sqlite_type.rb
188
201
  - lib/ulid/rails/type.rb
189
- - lib/ulid/rails/validator.rb
190
202
  - lib/ulid/rails/version.rb
191
203
  - ulid-rails.gemspec
192
204
  homepage: https://github.com/k2nr/ulid-rails/
@@ -208,9 +220,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
208
220
  version: 2.5.0
209
221
  required_rubygems_version: !ruby/object:Gem::Requirement
210
222
  requirements:
211
- - - ">="
223
+ - - ">"
212
224
  - !ruby/object:Gem::Version
213
- version: '0'
225
+ version: 1.3.1
214
226
  requirements: []
215
227
  rubygems_version: 3.4.12
216
228
  signing_key:
data/gemfiles/5.0.gemfile DELETED
@@ -1,10 +0,0 @@
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"
8
- gem "sqlite3", "~> 1.3.6"
9
- gem "mysql2", ">= 0.3.18", "< 0.6.0"
10
- gem "pg", ">= 0.18", "< 2.0"
data/gemfiles/5.1.gemfile DELETED
@@ -1,10 +0,0 @@
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"
8
- gem "sqlite3", "~> 1.3", ">= 1.3.6"
9
- gem "mysql2", ">= 0.3.18", "< 0.6.0"
10
- gem "pg", ">= 0.18", "< 2.0"
@@ -1,16 +0,0 @@
1
- require "base32/crockford"
2
-
3
- module ULID
4
- module Rails
5
- module Formatter
6
- def self.format(v)
7
- sanitized = v.delete("-").hex
8
- Base32::Crockford.encode(sanitized).rjust(26, "0")
9
- end
10
-
11
- def self.unformat(v)
12
- Base32::Crockford.decode(v).to_s(16).rjust(32, "0")
13
- end
14
- end
15
- end
16
- end
@@ -1,12 +0,0 @@
1
- require "base32/crockford"
2
-
3
- module ULID
4
- module Rails
5
- module Validator
6
- def self.is_valid?(v)
7
- return true if v.nil?
8
- v.length == 26 && Base32::Crockford.valid?(v)
9
- end
10
- end
11
- end
12
- end