namespaced model support

This commit is contained in:
Dylan Knutson
2025-07-16 18:31:28 +00:00
parent fe0f7b9bbe
commit f3990e2654
2 changed files with 38 additions and 1 deletions

View File

@@ -251,7 +251,7 @@ module HasAuxTable
# Get the current class for the association # Get the current class for the association
main_class = self main_class = self
main_association_name = T.must(main_class.name&.underscore&.to_sym) main_association_name = foreign_key.to_s.delete_suffix("_id").to_sym
# Create the auxiliary model class # Create the auxiliary model class
aux_class = aux_class =

View File

@@ -73,6 +73,20 @@ RSpec.describe HasAuxTable do
} }
end end
end end
create_base_table :utensils do |t|
t.string :name
t.string :material
t.timestamps
t.create_aux :fork do |t|
t.integer :num_tongs
end
t.create_aux :spoon do |t|
t.string :curvature
end
end
end end
class Vehicle < ActiveRecord::Base class Vehicle < ActiveRecord::Base
@@ -109,6 +123,20 @@ RSpec.describe HasAuxTable do
aux_table :passenger aux_table :passenger
belongs_to :boat, inverse_of: :passengers belongs_to :boat, inverse_of: :passengers
end end
module Kitchen
class Utensil < ActiveRecord::Base
include HasAuxTable
end
class Fork < Utensil
aux_table :fork
end
class Spoon < Utensil
aux_table :spoon
end
end
end end
# Car class will be defined after schema setup # Car class will be defined after schema setup
@@ -711,4 +739,13 @@ RSpec.describe HasAuxTable do
end end
end end
end end
describe "namespaced models" do
it "works with namespaced models" do
fork1 =
Kitchen::Fork.create!(name: "Fork", material: "metal", num_tongs: 3)
expect(fork1.material).to eq("metal")
expect(fork1.num_tongs).to eq(3)
end
end
end end