namespaced model support
This commit is contained in:
@@ -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 =
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user