support_table_data 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c7ebdaec82fe24f35650ee6906331233abaf106ec8627e92c003ad4c08692a1
4
- data.tar.gz: 95ec232e9ecb03ae9300b0f799c0424beaa7d7c564591ddc54e9dec0b45479b4
3
+ metadata.gz: 23cac76e97a2a99d97d7c5fba3c79c43d1a3391e1b035eea2e52cb223f211c5b
4
+ data.tar.gz: d73302038185694e8dd55f55c291f71fbd20f792e02d2a8eff6f1c2bee92f37f
5
5
  SHA512:
6
- metadata.gz: 6688998b029d42da0e82d5046386c033e8472ba49f869d3a573ca339c169bae3b5801c23d9018a9e4472427035e2a61883207919935f822c2115f98d4efd45b1
7
- data.tar.gz: '02398f8ed62df1a2c035aeab8b00ed453a1185c9e03717eb98af60aee429bc7d523343b1f5ea346bfa072a85ac6b5e29839137713e6f096def8d01f093547d76'
6
+ metadata.gz: c4c317e39dd17d1bba310af7067e534a9c8fb63b85625d6d6eef36dc06d8de4d2427cdc08b450721ee99fb027cf4b76f5e7a51fdf0ef488a90bf6a29b0b15b36
7
+ data.tar.gz: 7510b5ec898006085aa9686d179ff0a50c6a87d74c26db63c82fbc284e46357111169cfe60aecacf27b8db4c3881ce2c3024acf890ead31d959eaa5a860ff0a8
data/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.3.0
8
+
9
+ ### Added
10
+
11
+ - Added `support_table_dependency` method to explicitly define support table dependencies that cannot be inferred from model associations.
12
+
13
+ ## 1.2.4
14
+
15
+ ### Fixed
16
+
17
+ - Fixed issue with `sync_all!` finding obsolete classes that are no longer defined as support tables in development or test environments.
18
+
7
19
  ## 1.2.3
8
20
 
9
21
  ### Fixed
data/README.md CHANGED
@@ -57,6 +57,8 @@ You cannot update the value of the key attribute in a record in the data file. I
57
57
 
58
58
  You can specify data files as relative paths. This can be done by setting the `SupportTableData.data_directory` value. You can override this value for a model by setting the `support_table_data_directory` attribute on its class. In a Rails application, `SupportTableData.data_directory` will be automatically set to `db/support_tables/`. Otherwise, relative file paths will be resolved from the current working directory. You must define the directory to load relative files from before loading your model classes.
59
59
 
60
+ **Note**: If you're using CSV files and Ruby 3.4 or higher, you'll need to include the `csv` gem in your Gemfile since it was removed from the standard library in Ruby 3.4.
61
+
60
62
  ### Named Instances
61
63
 
62
64
  You can also automatically define helper methods to load instances and determine if they match specific values. This allows you to add more natural ways of referencing specific records.
@@ -212,7 +214,35 @@ Loading data is done inside a database transaction. No changes will be persisted
212
214
 
213
215
  You can synchronize the data in all models by calling `SupportTableData.sync_all!`. This method will discover all ActiveRecord models that include `SupportTableData` and synchronize each of them. (Note that there can be issues discovering all support table models in a Rails application if eager loading is turned off.) The discovery mechanism will try to detect unloaded classes by looking at the file names in the support table data directory so it's best to stick to standard Rails naming conventions for your data files.
214
216
 
215
- The load order for models will resolve any dependencies between models. So if one model has a `belongs_to` association with another model, then the belongs to model will be loaded first.
217
+ The load order for models will resolve any dependencies between models. So if one model has a `belongs_to` association with another model, then the belongs to model will be loaded first. You can also explicitly define dependencies with the `support_table_dependency` method. If you have a join table between support tables that creates a circular dependency, then you will need to define which model to load first.
218
+
219
+ ```ruby
220
+ class Widget < ApplicationRecord
221
+ include SupportTableData
222
+
223
+ add_support_table_data "widgets.yml"
224
+
225
+ has_many :thing_widgets
226
+ has_many :things, through: :thing_widgets
227
+ end
228
+
229
+ class Thing < ApplicationRecord
230
+ include SupportTableData
231
+
232
+ add_support_table_data "things.yml"
233
+
234
+ has_many :thing_widgets
235
+ has_many :widgets, through: :thing_widgets
236
+
237
+ # The Thing model is responsible for loading the thing_widgets join table by means of the widget_names=
238
+ # setter method. We need to define the depdenency to ensure widgets are loaded first.
239
+ support_table_dependency "Widget"
240
+
241
+ def widget_names=(widget_names)
242
+ self.widgets = Widget.where(name: widget_names)
243
+ end
244
+ end
245
+ ```
216
246
 
217
247
  You need to call `SupportTableData.sync_all!` when deploying your application. This gem includes a rake task `support_table_data:sync` that is suitable for hooking into deploy scripts. An easy way to hook it into a Rails application is by enhancing the `db:migrate` task so that the sync task runs immediately after database migrations are run. You can do this by adding code to a Rakefile in your application's `lib/tasks` directory:
218
248
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
1
+ 1.3.0
@@ -15,6 +15,7 @@ module SupportTableData
15
15
  @support_table_attribute_helpers = {}
16
16
  @support_table_instance_names = {}
17
17
  @support_table_instance_keys = nil
18
+ @support_table_dependencies = []
18
19
 
19
20
  # Define the attribute used as the key of the hash in the data files.
20
21
  # This should be a value that never changes. By default the key attribute will be the id.
@@ -205,6 +206,20 @@ module SupportTableData
205
206
  @protected_keys.include?(instance[key_attribute].to_s)
206
207
  end
207
208
 
209
+ # Explicitly define other support tables that this model depends on. A support table depends
210
+ # on another support table it needs to reference data on that table when loading its own data.
211
+ # Normally this is handled automatically by looking at the belongs_to associations on the model.
212
+ # In some cases, though, you may need to explicitly define the relationship. For instance, if
213
+ # there's a join table between two associations with the data poplulated from one support table's
214
+ # data file by referencing values maintained by the other support table. In this case,
215
+ # you need to define the dependency so that the tables are loaded in the correct order.
216
+ #
217
+ # @param class_names [String] List of class names that this support table depends on.
218
+ # @return [void]
219
+ def support_table_dependency(*class_names)
220
+ @support_table_dependencies += class_names.flatten.collect(&:to_s)
221
+ end
222
+
208
223
  private
209
224
 
210
225
  def define_support_table_named_instances
@@ -380,6 +395,7 @@ module SupportTableData
380
395
  active_record_classes = ActiveRecord::Base.descendants.reject { |klass| klass.name.nil? }
381
396
  active_record_classes.sort_by(&:name).each do |klass|
382
397
  next unless klass.include?(SupportTableData)
398
+ next unless klass.instance_variable_defined?(:@support_table_data_files) && klass.instance_variable_get(:@support_table_data_files).is_a?(Array)
383
399
  next if klass.abstract_class?
384
400
  next if classes.include?(klass)
385
401
  classes << klass
@@ -404,13 +420,17 @@ module SupportTableData
404
420
  #
405
421
  # @return [Array<Class>]
406
422
  def support_table_dependencies(klass)
407
- dependencies = []
423
+ dependencies = klass.instance_variable_get(:@support_table_dependencies).collect(&:constantize)
408
424
 
409
425
  klass.reflections.values.each do |reflection|
410
426
  next if reflection.polymorphic?
411
427
  next unless reflection.klass.include?(SupportTableData)
412
428
  next if reflection.klass <= klass
413
429
  next unless reflection.belongs_to? || reflection.through_reflection?
430
+ next if dependencies.include?(reflection.klass)
431
+
432
+ explicit_dependencies = reflection.klass.instance_variable_get(:@support_table_dependencies)
433
+ next if explicit_dependencies&.include?(klass.name)
414
434
 
415
435
  dependencies << reflection.klass
416
436
  rescue => e
@@ -9,6 +9,12 @@ Gem::Specification.new do |spec|
9
9
  spec.homepage = "https://github.com/bdurand/support_table_data"
10
10
  spec.license = "MIT"
11
11
 
12
+ spec.metadata = {
13
+ "homepage_uri" => spec.homepage,
14
+ "source_code_uri" => spec.homepage,
15
+ "changelog_uri" => "#{spec.homepage}/blob/master/CHANGELOG.md"
16
+ }
17
+
12
18
  # Specify which files should be added to the gem when it is released.
13
19
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
14
20
  ignore_files = %w[
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: support_table_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-23 00:00:00.000000000 Z
11
+ date: 2025-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -56,7 +56,10 @@ files:
56
56
  homepage: https://github.com/bdurand/support_table_data
57
57
  licenses:
58
58
  - MIT
59
- metadata: {}
59
+ metadata:
60
+ homepage_uri: https://github.com/bdurand/support_table_data
61
+ source_code_uri: https://github.com/bdurand/support_table_data
62
+ changelog_uri: https://github.com/bdurand/support_table_data/blob/master/CHANGELOG.md
60
63
  post_install_message:
61
64
  rdoc_options: []
62
65
  require_paths: