From 9698001a0a96455e5692c7aa30201e76bb661828 Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Thu, 23 May 2024 21:18:26 -0700 Subject: [PATCH] Added handling for `has_many through` associations to load the dependent through associations first. --- CHANGELOG.md | 1 + lib/support_table_data.rb | 13 +++++++++---- spec/data/shades.yml | 5 +++++ spec/spec_helper.rb | 20 +++++++++++++++++++- spec/support_table_data_spec.rb | 2 +- 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 spec/data/shades.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cb7b54..ef9acc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `named_instance` method to load a named instance from the database. - Added class method `named_instance_data` to return attributes from the data files for a named instance. +- Added handling for `has_many through` associations to load the dependent through associations first. ## 1.1.2 diff --git a/lib/support_table_data.rb b/lib/support_table_data.rb index 15f60d6..d008335 100644 --- a/lib/support_table_data.rb +++ b/lib/support_table_data.rb @@ -398,11 +398,16 @@ def support_table_classes(*extra_classes) # @return [Array] def support_table_dependencies(klass) dependencies = [] - klass.reflections.values.select(&:belongs_to?).each do |reflection| - if reflection.klass.include?(SupportTableData) && !(reflection.klass <= klass) - dependencies << reflection.klass - end + + klass.reflections.values.each do |reflection| + next if reflection.polymorphic? + next unless reflection.klass.include?(SupportTableData) + next if reflection.klass <= klass + next unless reflection.belongs_to? || reflection.through_reflection? + + dependencies << reflection.klass end + dependencies end end diff --git a/spec/data/shades.yml b/spec/data/shades.yml new file mode 100644 index 0000000..9b88241 --- /dev/null +++ b/spec/data/shades.yml @@ -0,0 +1,5 @@ +light: + name: Light + +dark: + name: Dark diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index befb4d4..4da7859 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,9 +27,14 @@ t.integer :parent_id end + connection.create_table(:shades) do |t| + t.string :name + end + connection.create_table(:things) do |t| t.string :name t.integer :color_id + t.integer :shade_id end connection.create_table(:invalids) do |t| @@ -49,11 +54,13 @@ class Color < ActiveRecord::Base belongs_to :group belongs_to :hue + has_many :things + has_many :shades, through: :things validates_uniqueness_of :name def group_name=(value) - self.group = Group.find_by!(name: value) + self.group = Group.named_instance(value) end def hue_name=(value) @@ -97,8 +104,19 @@ def parent_name=(value) end end +class Shade < ActiveRecord::Base + include SupportTableData + + self.support_table_key_attribute = :name + + add_support_table_data "shades.yml" + + validates_uniqueness_of :name +end + class Thing < ActiveRecord::Base belongs_to :color + belongs_to :shade end class Invalid < ActiveRecord::Base diff --git a/spec/support_table_data_spec.rb b/spec/support_table_data_spec.rb index 7f5b3a3..1510c64 100644 --- a/spec/support_table_data_spec.rb +++ b/spec/support_table_data_spec.rb @@ -188,7 +188,7 @@ describe "support_table_classes" do it "gets a list of all loaded support table classes with belongs to dependencies listed first" do - expect(SupportTableData.support_table_classes).to eq [Group, Hue, Color, Invalid] + expect(SupportTableData.support_table_classes).to eq [Group, Hue, Shade, Color, Invalid] end end