rails_type_id 0.2.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0781fbf1b175f2edfe7b66fc82d03d09f08762d084cfc4e0681831d39d6f90b
4
- data.tar.gz: 162be97b603d6b6480dcf27461bcf19541f43b30a5c65fcab5b1b46de1c04bd4
3
+ metadata.gz: baa657bbbdd396f057fdc5480b2b2dc873cfb07b411304bbfbd27e0b6c58d5e4
4
+ data.tar.gz: 5950890d9a9d3399be3076602c103b4f4a5d03c38aea144e57e145d615507de9
5
5
  SHA512:
6
- metadata.gz: 7a2d68e71437f6c62f2c183aaf749a4fddfba9ea61c6708f66c433dfa708ebfd2218ea2bfc74d05b1349cf2f65bbe1cc55b6434688cb5a6d2cb4c7defe823b94
7
- data.tar.gz: bbd6f63fd939e57f0dbb46280e249f1d2ed8a929aeb9f1541e49c732bd742b7f81f624a4bf6ed51ca399fe733064a2d58e1a8fbc4778796e1f89915fdeb4c59f
6
+ metadata.gz: 0161ab95f16f8ee6a66973cb7ba72ab0b6b64cc4d0ce929feb6984d7974adad1e33ed70f0e4f91431fd435627d2a565359958728d1e17bd9deaae758d8011bc5
7
+ data.tar.gz: 130a375af749c03edca9dc240aa287f627630c041fe573a33de7bc81f8efaf63b59dfdacef5a6a2586bd811af8da778569a7bde395790401fb9de4291d1cd327
data/README.md CHANGED
@@ -59,7 +59,7 @@ class MigrateUserToUUID < ActiveRecord::Migration[8.0]
59
59
  add_column :sessions, :user_uuid, :text, null: true
60
60
 
61
61
  Users.find_each do |u|
62
- u.update(uuid: SecureRandom.uuid_v7)
62
+ u.update(uuid: RailsTypeId::Concern::Helpers.generate_type_id("user"))
63
63
  end
64
64
  Session.find_each do |s|
65
65
  s.update(user_uuid: s.user.uuid)
@@ -13,8 +13,6 @@ module RailsTypeId
13
13
  extend ::ActiveSupport::Concern
14
14
  include ::ActiveModel::Validations::Callbacks
15
15
 
16
- class InvalidTypeIdPrefix < StandardError; end
17
-
18
16
  # Checks validity of the ActiveRecord model instances
19
17
  class Validator < ActiveModel::Validator
20
18
  def validate(record)
@@ -31,7 +29,9 @@ module RailsTypeId
31
29
  end
32
30
 
33
31
  def type_id_prefix
34
- @_type_id_prefix
32
+ @_type_id_prefix ||
33
+ # Handle inheritance by bubbling up if type_id_prefix defined on superclass
34
+ (superclass.type_id_prefix if superclass.respond_to?(:type_id_prefix))
35
35
  end
36
36
 
37
37
  def from_controller_id_param(type_id_str)
@@ -56,57 +56,7 @@ module RailsTypeId
56
56
  # @return [void]
57
57
  define_method :generate_type_id do
58
58
  Helpers.validate_type_id_prefix!(self.class.type_id_prefix)
59
- self.id ||= TypeID.from_uuid(self.class.type_id_prefix, SecureRandom.uuid_v7).to_s
60
- end
61
- end
62
-
63
- # Internal helper methods
64
- class Helpers
65
- extend T::Sig
66
-
67
- class << self
68
- def validate_type_id_prefix(prefix)
69
- return "type_id_prefix cannot be nil" if prefix.nil?
70
-
71
- return nil if prefix.match(/\A[a-z]{1,10}\z/)
72
-
73
- "type_id_prefix must be lowercase alphabetic (a-z) with length >= 1, <= 10"
74
- end
75
-
76
- def validate_type_id_prefix!(prefix)
77
- result = validate_type_id_prefix(prefix)
78
- raise InvalidTypeIdPrefix, result if result.present?
79
- end
80
-
81
- def lookup_type_id(type_id)
82
- klasses = lookup_model(type_id)
83
- return if klasses.nil?
84
-
85
- klasses.each do |klass|
86
- result = klass.find_by(id: type_id)
87
- return result unless result.nil?
88
- end
89
-
90
- nil
91
- end
92
-
93
- def lookup_model(type_id)
94
- prefix = TypeID.from_string(type_id).prefix
95
- return if prefix.nil?
96
-
97
- prefix_map[prefix]
98
- end
99
-
100
- private
101
-
102
- def prefix_map
103
- Rails.application.eager_load!
104
- # This has to be group_by and not index_by because we can have multiple
105
- # models that use the same prefix (like Settings)
106
- @prefix_map ||= ActiveRecord::Base.descendants
107
- .select { |klass| klass.respond_to?(:type_id_prefix) }
108
- .group_by(&:type_id_prefix)
109
- end
59
+ self.id ||= Helpers.generate_type_id(self.class.type_id_prefix).to_s
110
60
  end
111
61
  end
112
62
  end
@@ -0,0 +1,71 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+
6
+ module RailsTypeId
7
+ class InvalidTypeIdPrefix < StandardError; end
8
+
9
+ # Helper methods for interacting with type IDs
10
+ class Helpers
11
+ class << self
12
+ extend T::Sig
13
+
14
+ sig { params(prefix: String).returns(TypeID) }
15
+ def generate_type_id(prefix)
16
+ TypeID.from_uuid(prefix, SecureRandom.uuid_v7)
17
+ end
18
+
19
+ sig { params(prefix: T.nilable(String)).returns(T.nilable(String)) }
20
+ def validate_type_id_prefix(prefix)
21
+ return "type_id_prefix cannot be nil" if prefix.nil?
22
+
23
+ return nil if prefix.match(/\A[a-z]{1,10}\z/)
24
+
25
+ "type_id_prefix must be lowercase alphabetic (a-z) with length >= 1, <= 10"
26
+ end
27
+
28
+ sig { params(prefix: T.nilable(String)).void }
29
+ def validate_type_id_prefix!(prefix)
30
+ result = validate_type_id_prefix(prefix)
31
+ raise InvalidTypeIdPrefix, result if result.present?
32
+ end
33
+
34
+ sig { params(type_id: String).returns(T.nilable(ActiveRecord::Base)) }
35
+ def lookup_type_id(type_id)
36
+ klasses = lookup_model(type_id)
37
+ return if klasses.nil?
38
+
39
+ klasses.each do |klass|
40
+ result = klass.find_by(id: type_id)
41
+ return result unless result.nil?
42
+ end
43
+
44
+ nil
45
+ end
46
+
47
+ sig { params(type_id: String).returns(T.nilable(T::Array[T.untyped])) }
48
+ def lookup_model(type_id)
49
+ prefix = TypeID.from_string(type_id).prefix
50
+ return if prefix.nil?
51
+
52
+ prefix_map[prefix]
53
+ end
54
+
55
+ private
56
+
57
+ sig { returns(T::Hash[String, T::Array[T.untyped]]) }
58
+ def prefix_map
59
+ Rails.application.eager_load!
60
+ # This has to be group_by and not index_by because we can have multiple
61
+ # models that use the same prefix (like Settings)
62
+ @prefix_map ||= T.let(
63
+ ActiveRecord::Base.descendants
64
+ .select { |klass| klass.respond_to?(:type_id_prefix) }
65
+ .group_by(&:type_id_prefix),
66
+ T.nilable(T::Hash[String, T::Array[T.untyped]])
67
+ )
68
+ end
69
+ end
70
+ end
71
+ end
@@ -2,5 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "version"
5
+ require_relative "helpers"
5
6
  require_relative "concern"
6
7
  require_relative "test_helper"
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RailsTypeId
5
- VERSION = "0.2.0"
5
+ VERSION = "0.4.0"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_type_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu
@@ -66,6 +66,7 @@ files:
66
66
  - Rakefile
67
67
  - lib/rails_type_id.rb
68
68
  - lib/rails_type_id/concern.rb
69
+ - lib/rails_type_id/helpers.rb
69
70
  - lib/rails_type_id/require.rb
70
71
  - lib/rails_type_id/test_helper.rb
71
72
  - lib/rails_type_id/version.rb