external_id 0.1.1 → 0.1.2
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/README.md +49 -3
- data/lib/external_id/concerns/with_external_id.rb +3 -3
- data/lib/external_id/configuration.rb +2 -1
- data/lib/external_id/models/{external_id.rb → record.rb} +3 -3
- data/lib/external_id/railtie.rb +6 -0
- data/lib/external_id/version.rb +1 -1
- data/lib/external_id.rb +1 -1
- data/lib/generators/external_id/install/templates/external_ids_factory.rb +1 -1
- data/lib/generators/external_id/install/templates/initializer.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1b9df2ebbd5ffd553c97841c5d7b4b66f5a4c5ad03e6c6514ce81d05f8fcc476
|
|
4
|
+
data.tar.gz: f591813e0d6ac93edb75491131543fd52ea0ccf10e71e95bc46ddec6cfe5fcc6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 39983d6348cecc46842550d55a688b85dc32adc865c2e946b0d0eb4a2a1f71fa3db8cdaf637963be9e1dcd805ea167aa493bce0184c000440b3ff97e19ea812e
|
|
7
|
+
data.tar.gz: a043ce4201d839ad871040314eab48517426a8931cbc2d046faddef1dbe85be57d6987f2aeb6ef96d0b412d42a3ab8fdcfe58b9c7571f27eaca9b40082ce3731
|
data/README.md
CHANGED
|
@@ -63,6 +63,11 @@ ExternalId.configure do |config|
|
|
|
63
63
|
|
|
64
64
|
# Optional: Use UUID for primary keys (default: true)
|
|
65
65
|
# config.use_uuid = true
|
|
66
|
+
|
|
67
|
+
# Optional: Enable auditing with the audited gem (if loaded)
|
|
68
|
+
# Default: true
|
|
69
|
+
# Set to false if you don't want ExternalId records to be audited
|
|
70
|
+
# config.enable_auditing = true
|
|
66
71
|
end
|
|
67
72
|
```
|
|
68
73
|
|
|
@@ -132,7 +137,7 @@ customer_without_eid.external_id.blank? # => true
|
|
|
132
137
|
customer = Customer.find(123)
|
|
133
138
|
|
|
134
139
|
# Access the external_id record directly
|
|
135
|
-
customer.eid # => ExternalId::
|
|
140
|
+
customer.eid # => ExternalId::Record instance or nil
|
|
136
141
|
|
|
137
142
|
# The association is dependent: :destroy
|
|
138
143
|
# Deleting the customer will also delete the external_id
|
|
@@ -203,13 +208,13 @@ class ApplicationRecord < ActiveRecord::Base
|
|
|
203
208
|
end
|
|
204
209
|
|
|
205
210
|
# Add custom scopes
|
|
206
|
-
class ExternalId::
|
|
211
|
+
class ExternalId::Record
|
|
207
212
|
scope :raynet, -> { where(provider: 'raynet') }
|
|
208
213
|
scope :customers, -> { where(resource_type: 'Customer') }
|
|
209
214
|
end
|
|
210
215
|
|
|
211
216
|
# Use them
|
|
212
|
-
ExternalId::
|
|
217
|
+
ExternalId::Record.raynet.customers
|
|
213
218
|
```
|
|
214
219
|
|
|
215
220
|
### Handling multiple providers
|
|
@@ -228,6 +233,47 @@ customer.add_external_id(provider: 'raynet', id: 'R-99999') # => ActiveRecord::
|
|
|
228
233
|
# Note: Currently limited to one external_id per resource
|
|
229
234
|
```
|
|
230
235
|
|
|
236
|
+
### Auditing with the audited gem
|
|
237
|
+
|
|
238
|
+
If you have the `audited` gem loaded in your application, the `ExternalId` model will automatically track all changes to external ID records. This includes tracking changes to:
|
|
239
|
+
- `provider` attribute
|
|
240
|
+
- `external_id` attribute
|
|
241
|
+
- `resource` polymorphic association
|
|
242
|
+
|
|
243
|
+
Auditing is enabled by default when the audited gem is present. You can disable it in your initializer:
|
|
244
|
+
|
|
245
|
+
```ruby
|
|
246
|
+
ExternalId.configure do |config|
|
|
247
|
+
config.enable_auditing = false
|
|
248
|
+
end
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
To access the audit history of an external ID record:
|
|
252
|
+
|
|
253
|
+
```ruby
|
|
254
|
+
external_id_record = ExternalId::Record.find(some_id)
|
|
255
|
+
|
|
256
|
+
# Get all audits for this record
|
|
257
|
+
external_id_record.audits
|
|
258
|
+
|
|
259
|
+
# Get the last audit
|
|
260
|
+
external_id_record.audits.last
|
|
261
|
+
|
|
262
|
+
# Access audit details
|
|
263
|
+
audit = external_id_record.audits.last
|
|
264
|
+
audit.action # => "create", "update", or "destroy"
|
|
265
|
+
audit.audited_changes # => Hash of changed attributes
|
|
266
|
+
audit.created_at # => When the change occurred
|
|
267
|
+
audit.user # => The user who made the change (if set)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Note: Make sure you've run the audited gem's generator to create the `audits` table:
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
rails generate audited:install
|
|
274
|
+
rails db:migrate
|
|
275
|
+
```
|
|
276
|
+
|
|
231
277
|
## Development
|
|
232
278
|
|
|
233
279
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -5,7 +5,7 @@ module ExternalId
|
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
|
-
has_one :eid, class_name: 'ExternalId::
|
|
8
|
+
has_one :eid, class_name: 'ExternalId::Record', as: :resource, dependent: :destroy
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
class_methods do
|
|
@@ -16,7 +16,7 @@ module ExternalId
|
|
|
16
16
|
"Provider must be one of #{providers.keys}, was '#{provider}'"
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
::ExternalId::
|
|
19
|
+
::ExternalId::Record.find_by(provider: provider, external_id: external_id, resource_type: name)&.resource
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
@@ -27,7 +27,7 @@ module ExternalId
|
|
|
27
27
|
|
|
28
28
|
eid = external_id_object.is_a?(::ExternalId::Value) ? external_id_object : ::ExternalId::Value.new(provider: provider, id: id)
|
|
29
29
|
|
|
30
|
-
::ExternalId::
|
|
30
|
+
::ExternalId::Record.find_or_create_by!(provider: eid.provider, external_id: eid.id, resource: self)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def external_id
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
module ExternalId
|
|
4
4
|
class Configuration
|
|
5
|
-
attr_accessor :providers, :base_class, :use_uuid
|
|
5
|
+
attr_accessor :providers, :base_class, :use_uuid, :enable_auditing
|
|
6
6
|
|
|
7
7
|
def initialize
|
|
8
8
|
@providers = {}
|
|
9
9
|
@base_class = 'ActiveRecord::Base'
|
|
10
10
|
@use_uuid = true
|
|
11
|
+
@enable_auditing = true
|
|
11
12
|
end
|
|
12
13
|
|
|
13
14
|
def providers=(value)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module ExternalId
|
|
4
|
-
class
|
|
4
|
+
class Record < ActiveRecord::Base
|
|
5
5
|
self.table_name = 'external_ids'
|
|
6
6
|
|
|
7
7
|
belongs_to :resource, polymorphic: true
|
|
@@ -36,10 +36,10 @@ module ExternalId
|
|
|
36
36
|
# Call setup when the class is loaded
|
|
37
37
|
def self.inherited(subclass)
|
|
38
38
|
super
|
|
39
|
-
subclass.setup_enum! if subclass == ExternalId::
|
|
39
|
+
subclass.setup_enum! if subclass == ExternalId::Record
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
# Setup enum after configuration
|
|
45
|
-
ExternalId::
|
|
45
|
+
ExternalId::Record.setup_enum! if defined?(Rails) && Rails.application
|
data/lib/external_id/railtie.rb
CHANGED
|
@@ -6,6 +6,12 @@ module ExternalId
|
|
|
6
6
|
ActiveSupport.on_load(:active_record) do
|
|
7
7
|
# Make the concern available to all ActiveRecord models
|
|
8
8
|
# Users will still need to explicitly include it
|
|
9
|
+
|
|
10
|
+
# Enable auditing if the audited gem is loaded and configured
|
|
11
|
+
# This runs after initializers so user configuration is available
|
|
12
|
+
if defined?(Audited) && ::ExternalId.configuration.enable_auditing
|
|
13
|
+
::ExternalId::Record.audited
|
|
14
|
+
end
|
|
9
15
|
end
|
|
10
16
|
end
|
|
11
17
|
end
|
data/lib/external_id/version.rb
CHANGED
data/lib/external_id.rb
CHANGED
|
@@ -6,7 +6,7 @@ require 'active_support'
|
|
|
6
6
|
require_relative 'external_id/version'
|
|
7
7
|
require_relative 'external_id/configuration'
|
|
8
8
|
require_relative 'external_id/models/value'
|
|
9
|
-
require_relative 'external_id/models/
|
|
9
|
+
require_relative 'external_id/models/record'
|
|
10
10
|
require_relative 'external_id/concerns/with_external_id'
|
|
11
11
|
|
|
12
12
|
module ExternalId
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
FactoryBot.define do
|
|
4
|
-
factory :external_id, class: 'ExternalId::
|
|
4
|
+
factory :external_id, class: 'ExternalId::Record' do
|
|
5
5
|
provider { 'default_provider' }
|
|
6
6
|
sequence(:external_id) { |n| "ext-#{n}" }
|
|
7
7
|
# association :resource, factory: :your_model
|
|
@@ -20,4 +20,9 @@ ExternalId.configure do |config|
|
|
|
20
20
|
# Use UUID for primary keys (recommended for distributed systems)
|
|
21
21
|
# Default: true
|
|
22
22
|
# config.use_uuid = true
|
|
23
|
+
|
|
24
|
+
# Enable auditing with the audited gem (if loaded)
|
|
25
|
+
# Default: true
|
|
26
|
+
# Set to false if you don't want ExternalId records to be audited
|
|
27
|
+
# config.enable_auditing = true
|
|
23
28
|
end
|
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.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tomáš Landovský
|
|
@@ -143,7 +143,7 @@ files:
|
|
|
143
143
|
- lib/external_id.rb
|
|
144
144
|
- lib/external_id/concerns/with_external_id.rb
|
|
145
145
|
- lib/external_id/configuration.rb
|
|
146
|
-
- lib/external_id/models/
|
|
146
|
+
- lib/external_id/models/record.rb
|
|
147
147
|
- lib/external_id/models/value.rb
|
|
148
148
|
- lib/external_id/railtie.rb
|
|
149
149
|
- lib/external_id/version.rb
|