Skip to content

Commit

Permalink
Merge pull request #7 from bdurand/handle_has_many_through_load_order
Browse files Browse the repository at this point in the history
Added handling for through associations
  • Loading branch information
bdurand authored May 24, 2024
2 parents 743d3b9 + 9698001 commit 557bfd8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions lib/support_table_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,16 @@ def support_table_classes(*extra_classes)
# @return [Array<Class>]
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
Expand Down
5 changes: 5 additions & 0 deletions spec/data/shades.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
light:
name: Light

dark:
name: Dark
20 changes: 19 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/support_table_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 557bfd8

Please sign in to comment.