allow redefinition of methods

This commit is contained in:
Dylan Knutson
2025-07-26 00:32:12 +00:00
parent 6df1fe8053
commit 8f610b8fa7
3 changed files with 85 additions and 11 deletions

View File

@@ -1182,4 +1182,57 @@ RSpec.describe HasAuxTable do
expect(patient.doctors.count).to eq(1)
end
end
describe "allowing redefining of methods" do
it "allows method redefining with `allow_method_redefinition`" do
ActiveRecord::Schema.define do
create_base_table :test_model2s do |t|
t.string :on_base
t.create_aux :specific do |t|
t.string :on_aux
end
end
end
class TestModel2 < ActiveRecord::Base
include HasAuxTable
def on_base
"on_base #{super} #{id}"
end
end
expect {
class TestModel2A < TestModel2
aux_table :specific, allow_redefining: :on_base
def on_base
"2a_on_base_override #{super}"
end
def on_aux
"2a_on_aux_override #{super}"
end
end
}.not_to raise_error
expect {
class TestModel2B < TestModel2
aux_table :specific, allow_redefining: :on_base
end
}.not_to raise_error
base_model = TestModel2.create!(on_base: "base")
expect(base_model.on_base).to eq("on_base base #{base_model.id}")
specific_a = TestModel2A.create!(on_base: "2a_base", on_aux: "2a_aux")
expect(specific_a.on_base).to eq(
"2a_on_base_override on_base 2a_base #{specific_a.id}"
)
expect(specific_a.on_aux).to eq("2a_on_aux_override 2a_aux")
specific_b = TestModel2B.create!(on_base: "2b_base", on_aux: "2b_aux")
expect(specific_b.on_base).to eq("on_base 2b_base #{specific_b.id}")
expect(specific_b.on_aux).to eq("2b_aux")
end
end
end