structured_store 1.0.1 → 1.1.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: 98749f2601c0f1a634bfe3b4765766b30f406466946cbd302a04017ba31eb5ae
4
- data.tar.gz: f3037e8181f55ab3268f200d28378f2523aca48cc635e13d727e399554db1a62
3
+ metadata.gz: e03aa6f80b366b7dbb2b3312426a4d7da960e4e96d1840523b061a6de20cf17a
4
+ data.tar.gz: 24455f358faff6932c5d78d8dc7963f687a35e888b2bafb798d9dae1ca3fe52b
5
5
  SHA512:
6
- metadata.gz: 380929ef3aca9f2fd7fb4db9500e73c2a127ecc3d1a6f951b20b39097afa60e49164e060866a9fd77abb567f7b1847a32e0c9f00f3a49f3daa0e09d28d2f3bf4
7
- data.tar.gz: aa71ce6db686a4a3ba156ec9e59b942ebe6abc77195fc527c4021abcd840d88e04228085ceaa8d8cbc7475dadf16ac561d58006f94bb189bd90a6d067d271aa5
6
+ metadata.gz: b76620f3c30d6161424accbd88baede2a265040ab5f3dec5d564a981fad9002eae10e4cef3cb1e86c70b1d311e3d338b12773730e535296c0e794ed0316767ae
7
+ data.tar.gz: 5bbb7b690c4490be4948401b36434a67893cb7fcbb7d149dc96245a7f718210676ec5242eff9902acd6d0bddc6c7453abb29571553a7045000a6d3a2df928858
data/README.md CHANGED
@@ -107,7 +107,7 @@ class UserPreference < ApplicationRecord
107
107
  end
108
108
  ```
109
109
 
110
- Generate and run a migration for your model:
110
+ Generate and run a migration for your model (for advanced configuration options like custom foreign keys, see section 9):
111
111
 
112
112
  ```ruby
113
113
  # db/migrate/xxx_create_user_preferences.rb
@@ -425,13 +425,93 @@ all_versions = StructuredStore::VersionedSchema.where(name: "UserPreferences")
425
425
  .order(:version)
426
426
  ```
427
427
 
428
- ### 9. Configurable Store Columns
428
+ ### 9. Custom Schema Names and Association Options
429
+
430
+ StructuredStore provides flexibility in configuring your schema associations. The `structured_store` method accepts any `belongs_to` options via the `**belongs_to_options` parameter:
431
+
432
+ ```ruby
433
+ class Product < ApplicationRecord
434
+ include StructuredStore::Storable
435
+
436
+ # Default naming: creates belongs_to :config_versioned_schema
437
+ # with foreign_key: 'structured_store_config_versioned_schema_id'
438
+ # and class_name: 'StructuredStore::VersionedSchema'
439
+ structured_store :config
440
+
441
+ # Custom schema name: creates belongs_to :product_configuration
442
+ # with foreign_key: 'structured_store_product_configuration_id'
443
+ structured_store :settings, schema_name: 'product_configuration'
444
+
445
+ # Custom foreign key: creates belongs_to :metadata_versioned_schema
446
+ # with foreign_key: 'custom_metadata_fk'
447
+ structured_store :metadata, foreign_key: 'custom_metadata_fk'
448
+
449
+ # Custom class name for using a different schema model
450
+ structured_store :advanced_config, class_name: 'CustomSchema'
451
+
452
+ # Multiple custom options
453
+ structured_store :options,
454
+ schema_name: 'product_options',
455
+ foreign_key: 'options_schema_id',
456
+ class_name: 'ProductSchema'
457
+ end
458
+ ```
459
+
460
+ #### Using Custom Schema Models
461
+
462
+ You can associate with custom schema models that have non-conventional primary keys:
463
+
464
+ ```ruby
465
+ # Custom schema model with non-standard primary key
466
+ class CustomSchema < ApplicationRecord
467
+ self.primary_key = 'schema_key'
468
+ end
469
+
470
+ class Product < ApplicationRecord
471
+ include StructuredStore::Storable
472
+
473
+ # Associate with CustomSchema using its custom primary key
474
+ structured_store :settings,
475
+ schema_name: 'custom_schema',
476
+ class_name: 'CustomSchema',
477
+ foreign_key: 'custom_schema_key',
478
+ primary_key: 'schema_key'
479
+ end
480
+ ```
481
+
482
+ #### Additional belongs_to Options
483
+
484
+ Since all keyword arguments are passed through to `belongs_to`, you can use any standard Rails association options:
485
+
486
+ ```ruby
487
+ class Product < ApplicationRecord
488
+ include StructuredStore::Storable
489
+
490
+ # Make the association optional
491
+ structured_store :config, optional: true
492
+
493
+ # Specify inverse_of
494
+ structured_store :settings,
495
+ class_name: 'CustomSchema',
496
+ inverse_of: :products
497
+
498
+ # Enable touch
499
+ structured_store :metadata, touch: true
500
+ end
501
+ ```
502
+
503
+ For complete working examples, see:
504
+
505
+ - `test/dummy/app/models/custom_foreign_key_record.rb` - Custom foreign key example
506
+ - `test/dummy/app/models/custom_primary_key_record.rb` - Custom primary key and class name example
507
+
508
+ ### 10. Configurable Store Columns
429
509
 
430
510
  StructuredStore supports configurable store columns, allowing you to use alternative column names for a single store (e.g., `depot` instead of `store`) or configure multiple store columns within the same model. This enables you to organize different types of structured data separately while maintaining proper schema versioning.
431
511
 
432
512
  For detailed information on configuring single custom stores and multiple stores, see the [Custom Stores documentation](docs/custom_stores.md).
433
513
 
434
- ### 10. Advanced Usage: Custom Reference Resolvers
514
+ ### 11. Advanced Usage: Custom Reference Resolvers
435
515
 
436
516
  StructuredStore includes a resolver system for handling JSON schema references. You can create custom resolvers by extending `StructuredStore::RefResolvers::Base`:
437
517
 
@@ -463,6 +543,7 @@ CustomResolver.register
463
543
  ```
464
544
 
465
545
  **Available instance variables in your resolver:**
546
+
466
547
  - `property_schema` - The property's JSON schema hash
467
548
  - `parent_schema` - The parent SchemaInspector for looking up definitions
468
549
  - `property_name` - The property name (for error messages)
@@ -41,6 +41,13 @@ module StructuredStore
41
41
  # @param column_name [String, Symbol] The name of the store column in your model
42
42
  # @param schema_name [String, Symbol, nil] Optional custom name for the schema association
43
43
  # If not provided, defaults to "#{column_name}_versioned_schema"
44
+ # @param belongs_to_options [Hash] Options to pass to the belongs_to association
45
+ # @option belongs_to_options [String, Symbol] :foreign_key Custom foreign key for the belongs_to association
46
+ # If not provided, defaults to "structured_store_#{schema_name}_id"
47
+ # @option belongs_to_options [String, Symbol] :primary_key Custom primary key for the belongs_to association
48
+ # If not provided, uses the default primary key of the associated model
49
+ # @option belongs_to_options [String] :class_name Custom class name for the belongs_to association
50
+ # If not provided, defaults to 'StructuredStore::VersionedSchema'
44
51
  #
45
52
  # @example Simple store configuration
46
53
  # structured_store :preferences
@@ -51,11 +58,27 @@ module StructuredStore
51
58
  # structured_store :config, schema_name: 'product_configuration'
52
59
  # # Creates: belongs_to :product_configuration
53
60
  # # Helper method: product_configuration_json_schema
54
- def structured_store(column_name, schema_name: nil)
61
+ #
62
+ # @example Custom foreign key
63
+ # structured_store :settings, foreign_key: 'custom_schema_id'
64
+ # # Creates: belongs_to with foreign_key: 'custom_schema_id'
65
+ #
66
+ # @example Custom primary key
67
+ # structured_store :settings, primary_key: 'custom_pk'
68
+ # # Creates: belongs_to with primary_key: 'custom_pk'
69
+ #
70
+ # @example Custom class name
71
+ # structured_store :settings, class_name: 'CustomSchema'
72
+ # # Creates: belongs_to with class_name: 'CustomSchema'
73
+ def structured_store(column_name, schema_name: nil, **belongs_to_options)
55
74
  column_name = column_name.to_s
56
75
  schema_name ||= "#{column_name}_versioned_schema"
57
76
  schema_name = schema_name.to_s
58
77
 
78
+ # Set defaults for belongs_to options if not provided
79
+ belongs_to_options[:foreign_key] ||= "structured_store_#{schema_name}_id"
80
+ belongs_to_options[:class_name] ||= 'StructuredStore::VersionedSchema'
81
+
59
82
  # Add configuration for this column
60
83
  self._structured_store_configurations = _structured_store_configurations + [{
61
84
  column_name: column_name,
@@ -63,9 +86,7 @@ module StructuredStore
63
86
  }]
64
87
 
65
88
  # Define the belongs_to association immediately
66
- belongs_to schema_name.to_sym, # rubocop:disable Rails/InverseOf
67
- class_name: 'StructuredStore::VersionedSchema',
68
- foreign_key: "structured_store_#{schema_name}_id"
89
+ belongs_to schema_name.to_sym, **belongs_to_options
69
90
 
70
91
  # Define helper method to get schema for this specific store
71
92
  define_method "#{column_name}_json_schema" do
@@ -1,3 +1,3 @@
1
1
  module StructuredStore
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structured_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Gentry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-11-12 00:00:00.000000000 Z
11
+ date: 2025-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_schemer