rails_type_id 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b002a4e9136bfe9b886e13b8aaea8962f98b6ccf94e64dc875423c985fab6f8c
4
- data.tar.gz: 7e245dcb3726845ddc8617cb91f2b6e79510baff72348d27124255855d79b3e6
3
+ metadata.gz: c0781fbf1b175f2edfe7b66fc82d03d09f08762d084cfc4e0681831d39d6f90b
4
+ data.tar.gz: 162be97b603d6b6480dcf27461bcf19541f43b30a5c65fcab5b1b46de1c04bd4
5
5
  SHA512:
6
- metadata.gz: eb7fb1c5a1953d4b6ce56aa3771b18001220bcb383413abf17157d4cd7644cc505ac6c8cefe30902d246cf15b297fffa631db674ef33a07852c9189d3a08ac19
7
- data.tar.gz: d534f95fbbec9934890497da6b2c9b0a4e0d82189b66c8e176a57758d5c9353336147b6136343b1cd8ee18388be655b631d336452e9a0fd5528b8dd9100178df
6
+ metadata.gz: 7a2d68e71437f6c62f2c183aaf749a4fddfba9ea61c6708f66c433dfa708ebfd2218ea2bfc74d05b1349cf2f65bbe1cc55b6434688cb5a6d2cb4c7defe823b94
7
+ data.tar.gz: bbd6f63fd939e57f0dbb46280e249f1d2ed8a929aeb9f1541e49c732bd742b7f81f624a4bf6ed51ca399fe733064a2d58e1a8fbc4778796e1f89915fdeb4c59f
data/README.md CHANGED
@@ -12,8 +12,13 @@ bundle add rails_type_id
12
12
 
13
13
  ## Usage
14
14
 
15
+ ### Database requirements
16
+
17
+ - ActiveRecord models should have an `id` field that is either a string-like type (`TEXT`, `VARCHAR`) .
18
+ See [Migration](#migrating-existing-ids) if you have an existing `id` field.
19
+
15
20
  ### Declaring on models
16
- Add `RailsTypeId::Concern` to the model. This model's `id` field should have either a String or UUID database column type.
21
+ Add `RailsTypeId::Concern` to the model. This model's `id` field should have a string-like database column type.
17
22
 
18
23
  ```ruby
19
24
  # app/models/my_model.rb
@@ -33,9 +38,54 @@ Model instances will have a `type_id` field of type `TypeID`.
33
38
 
34
39
  ```ruby
35
40
  my_model = MyModel.create!(..)
36
- my_model.id #=> "019867fe-560f-7941-a7ed-8472639c7ace"
41
+ my_model.id #=> "mm_01k1kzwngff50tfvc4e9hsrype"
37
42
  my_model.type_id #=> #<TypeID mm_01k1kzwngff50tfvc4e9hsrype>
38
- my_model.type_id.to_s #=> mm_01k1kzwngff50tfvc4e9hsrype
43
+ ```
44
+
45
+ ### Testing
46
+
47
+ `lib/rails_type_id/test_helpers.rb` contains some helper methods for writing wholesome tests.
48
+
49
+ ### Migrating existing IDs
50
+
51
+ For models with an existing Rails `id` field (usually an auto-incrementing integer), you'll need to
52
+ migrate these to either a string-like column type. Below is an example migration for SQLite
53
+ using a `text` type for a Users model that has an associated Session.
54
+
55
+ ```ruby
56
+ class MigrateUserToUUID < ActiveRecord::Migration[8.0]
57
+ def change
58
+ add_column :users, :uuid, :text, null: true
59
+ add_column :sessions, :user_uuid, :text, null: true
60
+
61
+ Users.find_each do |u|
62
+ u.update(uuid: SecureRandom.uuid_v7)
63
+ end
64
+ Session.find_each do |s|
65
+ s.update(user_uuid: s.user.uuid)
66
+ end
67
+ change_column_null :users, :uuid, false
68
+ change_column_null :sessions, :user_uuid, false
69
+
70
+ remove_foreign_key :sessions, :users
71
+
72
+ rename_column :users, :id, :integer_id
73
+ rename_column :users, :uuid, :id
74
+
75
+ rename_column :sessions, :user_id, :integer_user_id
76
+ rename_column :sessions, :user_uuid, :user_id
77
+ change_column_null :session, :integer_user_id, true
78
+
79
+ execute "ALTER TABLE users DROP CONSTRAINT users_pkey;"
80
+ execute "ALTER_TABLE users ADD PRIMARY KEY (id);"
81
+
82
+ execute "ALTER TABLE ONLY users ALTER COLUMN integer_id DROP DEFAULT"
83
+ change_column_null :users, :integer_id, true
84
+ execute "DROP SEQUENCE IF EXISTS users_id_seq"
85
+
86
+ add_foreign_key :sessions, :users
87
+ end
88
+ end
39
89
  ```
40
90
 
41
91
  ## Development
@@ -42,27 +42,21 @@ module RailsTypeId
42
42
  included do
43
43
  attribute :id # Postgres UUID field
44
44
 
45
- before_create :generate_uuid_v7
45
+ before_create :generate_type_id
46
46
  validates_with Validator
47
47
 
48
48
  # Returns the TypeID for the model
49
49
  # @return [TypeID]
50
50
  define_method :type_id do
51
51
  Helpers.validate_type_id_prefix!(self.class.type_id_prefix)
52
- TypeID.from_uuid(self.class.type_id_prefix, id)
52
+ TypeID.from_string(id)
53
53
  end
54
54
 
55
- # If `id` is unset, generates a new UUID v7 and sets it
55
+ # If `id` is unset, generates a new UUID v7 TypeID and sets the `id` field
56
56
  # @return [void]
57
- define_method :generate_uuid_v7 do
58
- case self.class.attribute_types["id"].type
59
- when :uuid, :string
60
- self.id ||= SecureRandom.uuid_v7
61
- end
62
- end
63
-
64
- define_method :to_param do
65
- type_id.to_s
57
+ define_method :generate_type_id do
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
66
60
  end
67
61
  end
68
62
 
@@ -88,9 +82,8 @@ module RailsTypeId
88
82
  klasses = lookup_model(type_id)
89
83
  return if klasses.nil?
90
84
 
91
- id = type_id # TODO: parse out the uuid part
92
85
  klasses.each do |klass|
93
- result = klass.find_by(id: id)
86
+ result = klass.find_by(id: type_id)
94
87
  return result unless result.nil?
95
88
  end
96
89
 
@@ -98,23 +91,14 @@ module RailsTypeId
98
91
  end
99
92
 
100
93
  def lookup_model(type_id)
101
- prefix = get_prefix(type_id)
94
+ prefix = TypeID.from_string(type_id).prefix
102
95
  return if prefix.nil?
103
96
 
104
97
  prefix_map[prefix]
105
98
  end
106
99
 
107
- def get_prefix(id)
108
- prefix, = parse_id(id)
109
- prefix
110
- end
111
-
112
100
  private
113
101
 
114
- def parse_id(id)
115
- id.split("_")
116
- end
117
-
118
102
  def prefix_map
119
103
  Rails.application.eager_load!
120
104
  # This has to be group_by and not index_by because we can have multiple
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RailsTypeId
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
@@ -34,11 +34,6 @@ module Tapioca
34
34
  "type_id",
35
35
  return_type: "TypeID"
36
36
  )
37
-
38
- methods_mod.create_method(
39
- "to_param",
40
- return_type: "String"
41
- )
42
37
  end
43
38
  klass.create_include(RBI_MODULE_NAME)
44
39
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu