external_id 0.1.0 → 0.1.1

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: c7f37fe1f61ae7cc606c98a516bcbf92158799383751ecfa602b3b29c32a7da2
4
- data.tar.gz: 71a51fe88cebf3fe95516cd87c4a173d111925cdf1da6fcf3c5a8caddebc299e
3
+ metadata.gz: 8daf1349bdbc49e832de6c584e66e582d8eea993725e831f8c550c698dcad153
4
+ data.tar.gz: 003bd6b70857cf9457b1a45513b73682d7939e18aa03dbbc9b91e96bbddd35cb
5
5
  SHA512:
6
- metadata.gz: 911f2c68eff82fc94075e67f295f6c9a49324013ea9636c845705bc4d9bf8c07282d432d7cf691ca37c579b4d256a20397a7a0ef3c69abcbb1999a28d4f5ba80
7
- data.tar.gz: 24c2e9299bb4658cda2dc0a0c0d0a805036181cf0f89096074f3fb60b831251e47be98b1e74aa97cd1e8ca250e678fecb171912d691baf5a20892d8dd8100883
6
+ metadata.gz: 52c9fef8215b01a7741c0b559d0f034d314acc85056a4d4882697c445a29ed801edcfb53ad31fc2f4115a184f78b1faef68503bebf5c0b1b607f82ca908268b4
7
+ data.tar.gz: 35bed9ab8dad13ee768cc270767396171049526226be2889ea854d23c139f26e77e8a913755fb34b9d56627c64ae47d65ed1222408e03ed945d95ea446fbb756
data/CHANGELOG.md CHANGED
@@ -1,11 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2025-12-02
4
+ - add installer for external_id factory
5
+ - fix: copy migration to correct path
6
+
3
7
  ## [0.1.0] - 2025-12-02
4
8
 
5
9
  - Initial release
6
10
  - Polymorphic external ID associations for Rails models
7
11
  - `ExternalId` model for database-backed storage
8
- - `ExternalIdValue` value object for clean data handling
12
+ - `ExternalId::Value` value object for clean data handling
9
13
  - `WithExternalId` concern for easy model integration
10
14
  - Configurable providers with enum support
11
15
  - UUID support for distributed systems
data/README.md CHANGED
@@ -88,8 +88,8 @@ customer = Customer.find(123)
88
88
  # Option 1: Using keyword arguments
89
89
  customer.add_external_id(provider: 'raynet', id: 'R-12345')
90
90
 
91
- # Option 2: Using an ExternalIdValue object
92
- external_id = ExternalId::ExternalIdValue.new(provider: 'salesforce', id: 'SF-67890')
91
+ # Option 2: Using an ExternalId::Value object
92
+ external_id = ExternalId::Value.new(provider: 'salesforce', id: 'SF-67890')
93
93
  customer.add_external_id(external_id)
94
94
  ```
95
95
 
@@ -111,7 +111,7 @@ Customer.find_by_external_id('unknown', '123') # => ArgumentError
111
111
  ```ruby
112
112
  customer = Customer.find(123)
113
113
 
114
- # Returns an ExternalIdValue object
114
+ # Returns an ExternalId::Value object
115
115
  external_id = customer.external_id
116
116
 
117
117
  if external_id.present?
@@ -157,13 +157,13 @@ add_index :external_ids, [:provider, :resource_type, :resource_id],
157
157
 
158
158
  This ensures that each resource can only have one external ID per provider.
159
159
 
160
- ## ExternalIdValue
160
+ ## ExternalId::Value
161
161
 
162
- The `ExternalIdValue` class is a value object that provides a clean interface for working with external IDs:
162
+ The `ExternalId::Value` class is a value object that provides a clean interface for working with external IDs:
163
163
 
164
164
  ```ruby
165
165
  # Create a value object
166
- eid = ExternalId::ExternalIdValue.new(provider: 'raynet', id: '12345')
166
+ eid = ExternalId::Value.new(provider: 'raynet', id: '12345')
167
167
 
168
168
  # Check presence
169
169
  eid.present? # => true
@@ -179,14 +179,14 @@ eid.to_hash # => { provider: 'raynet', id: '12345' }
179
179
  eid.to_a # => ['raynet', '12345']
180
180
 
181
181
  # Create from array
182
- ExternalId::ExternalIdValue.from_array(['raynet', '12345'])
182
+ ExternalId::Value.from_array(['raynet', '12345'])
183
183
 
184
184
  # Create blank value
185
- ExternalId::ExternalIdValue.blank # => blank instance
185
+ ExternalId::Value.blank # => blank instance
186
186
 
187
187
  # Comparison
188
- eid1 = ExternalId::ExternalIdValue.new(provider: 'raynet', id: '123')
189
- eid2 = ExternalId::ExternalIdValue.new(provider: 'raynet', id: '123')
188
+ eid1 = ExternalId::Value.new(provider: 'raynet', id: '123')
189
+ eid2 = ExternalId::Value.new(provider: 'raynet', id: '123')
190
190
  eid1 == eid2 # => true
191
191
  ```
192
192
 
@@ -22,16 +22,16 @@ module ExternalId
22
22
 
23
23
  def add_external_id(external_id_object = nil, provider: nil, id: nil)
24
24
  if external_id_object.blank? && provider.blank? && id.blank?
25
- raise 'Either ExternalIdValue or provider and id are required'
25
+ raise 'Either ExternalId::Value or provider and id are required'
26
26
  end
27
27
 
28
- eid = external_id_object.is_a?(::ExternalId::ExternalIdValue) ? external_id_object : ::ExternalId::ExternalIdValue.new(provider: provider, id: id)
28
+ eid = external_id_object.is_a?(::ExternalId::Value) ? external_id_object : ::ExternalId::Value.new(provider: provider, id: id)
29
29
 
30
30
  ::ExternalId::ExternalId.find_or_create_by!(provider: eid.provider, external_id: eid.id, resource: self)
31
31
  end
32
32
 
33
33
  def external_id
34
- ::ExternalId::ExternalIdValue.from_model(eid)
34
+ ::ExternalId::Value.from_model(eid)
35
35
  end
36
36
  end
37
37
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ExternalId
4
- # ExternalId value object
5
- class ExternalIdValue
4
+ # ID Value value object
5
+ class Value
6
6
  include ActiveModel::Model
7
7
  include ActiveModel::Attributes
8
8
 
@@ -48,7 +48,7 @@ module ExternalId
48
48
  end
49
49
 
50
50
  def ==(other)
51
- return false unless other.is_a?(ExternalIdValue)
51
+ return false unless other.is_a?(::ExternalId::Value)
52
52
 
53
53
  provider == other.provider && id == other.id
54
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ExternalId
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/external_id.rb CHANGED
@@ -5,7 +5,7 @@ require 'active_support'
5
5
 
6
6
  require_relative 'external_id/version'
7
7
  require_relative 'external_id/configuration'
8
- require_relative 'external_id/models/external_id_value'
8
+ require_relative 'external_id/models/value'
9
9
  require_relative 'external_id/models/external_id'
10
10
  require_relative 'external_id/concerns/with_external_id'
11
11
 
@@ -11,24 +11,28 @@ Next steps:
11
11
  rails db:migrate
12
12
 
13
13
  3. Include the concern in your models:
14
-
14
+
15
15
  class Customer < ApplicationRecord
16
16
  include ExternalId::WithExternalId
17
17
  end
18
18
 
19
19
  4. Start using external IDs in your application:
20
-
20
+
21
21
  # Add an external ID to a record
22
22
  customer.add_external_id(provider: 'raynet', id: '12345')
23
-
23
+
24
24
  # Find a record by external ID
25
25
  customer = Customer.find_by_external_id('raynet', '12345')
26
-
26
+
27
27
  # Get the external ID value object
28
28
  external_id = customer.external_id
29
29
  external_id.provider # => 'raynet'
30
30
  external_id.id # => '12345'
31
31
 
32
+ 5. If FactoryBot was detected, a factory file was created in your
33
+ test factories directory. Customize the :external_id factory
34
+ to link to your models.
35
+
32
36
  For more information, see: https://github.com/yourusername/external-id
33
37
 
34
38
  ===============================================================================
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ factory :external_id, class: 'ExternalId::ExternalId' do
5
+ provider { 'default_provider' }
6
+ sequence(:external_id) { |n| "ext-#{n}" }
7
+ # association :resource, factory: :your_model
8
+ end
9
+ end
@@ -2,10 +2,10 @@
2
2
 
3
3
  class CreateExternalIds < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
4
4
  def change
5
- create_table :external_ids<%= ', id: :uuid' if ExternalId.configuration.use_uuid %> do |t|
5
+ create_table :external_ids<%= ', id: :uuid' if @use_uuid %> do |t|
6
6
  t.string :provider, null: false, index: true
7
7
  t.string :external_id, null: false
8
- t.references :resource, polymorphic: true, null: false, index: true<%= ', type: :uuid' if ExternalId.configuration.use_uuid %>
8
+ t.references :resource, polymorphic: true, null: false, index: true<%= ', type: :uuid' if @use_uuid %>
9
9
 
10
10
  t.timestamps
11
11
  end
@@ -16,8 +16,21 @@ module ExternalId
16
16
  template 'initializer.rb', 'config/initializers/external_id.rb'
17
17
  end
18
18
 
19
- def create_migration
20
- migration_template 'migration.rb.tt', File.join(db_migrate_path, 'create_external_ids.rb')
19
+ def copy_migration
20
+ @use_uuid = ExternalId.configuration.use_uuid rescue false
21
+ migration_template 'migration.rb.tt', 'db/migrate/create_external_ids.rb'
22
+ end
23
+
24
+ def copy_factory
25
+ return unless factory_bot_available?
26
+
27
+ test_dir = detect_test_directory
28
+ return unless test_dir
29
+
30
+ factories_dir = File.join(test_dir, 'factories')
31
+ empty_directory factories_dir unless File.directory?(factories_dir)
32
+
33
+ template 'external_ids_factory.rb', File.join(factories_dir, 'external_ids.rb')
21
34
  end
22
35
 
23
36
  def show_readme
@@ -26,13 +39,30 @@ module ExternalId
26
39
 
27
40
  private
28
41
 
29
- def db_migrate_path
30
- if defined?(Rails.application) && Rails.application.config.paths['db/migrate']
31
- Rails.application.config.paths['db/migrate'].to_a.first
32
- else
33
- 'db/migrate'
42
+ def factory_bot_available?
43
+ return @factory_bot_available if defined?(@factory_bot_available)
44
+
45
+ @factory_bot_available = begin
46
+ # Check Gemfile for factory_bot
47
+ gemfile_path = File.join(destination_root, 'Gemfile')
48
+ if File.exist?(gemfile_path)
49
+ gemfile_content = File.read(gemfile_path)
50
+ gemfile_content.match?(/factory_bot|factory_girl/)
51
+ else
52
+ false
53
+ end
34
54
  end
35
55
  end
56
+
57
+ def detect_test_directory
58
+ # Check common test directory names in order of preference
59
+ %w[spec test rspec].each do |dir|
60
+ path = File.join(destination_root, dir)
61
+ return dir if File.directory?(path)
62
+ end
63
+
64
+ nil
65
+ end
36
66
  end
37
67
  end
38
68
  end
data/sig/external_id.rbs CHANGED
@@ -45,16 +45,16 @@ module ExternalId
45
45
  end
46
46
 
47
47
  # Value object for external IDs
48
- class ExternalIdValue
48
+ class Value
49
49
  include ActiveModel::Model
50
50
  include ActiveModel::Attributes
51
51
 
52
52
  attr_accessor provider: String?
53
53
  attr_accessor id: String?
54
54
 
55
- def self.blank: () -> ExternalIdValue
56
- def self.from_model: (ExternalId?) -> ExternalIdValue
57
- def self.from_array: (Array[String?]?) -> ExternalIdValue
55
+ def self.blank: () -> Value
56
+ def self.from_model: (ExternalId?) -> Value
57
+ def self.from_array: (Array[String?]?) -> Value
58
58
 
59
59
  def initialize: (?provider: String? | Symbol?, ?id: String?) -> void
60
60
  def present?: () -> bool
@@ -98,8 +98,8 @@ module ExternalId
98
98
  # Instance methods available to including classes
99
99
  def eid: () -> ExternalId?
100
100
  def eid=: (ExternalId?) -> ExternalId?
101
- def add_external_id: (?ExternalIdValue?, ?provider: String? | Symbol?, ?id: String?) -> ExternalId
102
- def external_id: () -> ExternalIdValue
101
+ def add_external_id: (?Value?, ?provider: String? | Symbol?, ?id: String?) -> ExternalId
102
+ def external_id: () -> Value
103
103
 
104
104
  # Class methods added to including classes
105
105
  module ClassMethods
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: external_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomáš Landovský
@@ -144,10 +144,11 @@ files:
144
144
  - lib/external_id/concerns/with_external_id.rb
145
145
  - lib/external_id/configuration.rb
146
146
  - lib/external_id/models/external_id.rb
147
- - lib/external_id/models/external_id_value.rb
147
+ - lib/external_id/models/value.rb
148
148
  - lib/external_id/railtie.rb
149
149
  - lib/external_id/version.rb
150
150
  - lib/generators/external_id/install/templates/README
151
+ - lib/generators/external_id/install/templates/external_ids_factory.rb
151
152
  - lib/generators/external_id/install/templates/initializer.rb
152
153
  - lib/generators/external_id/install/templates/migration.rb.tt
153
154
  - lib/generators/external_id/install_generator.rb