invoke initialize blocks
This commit is contained in:
@@ -262,11 +262,11 @@ module HasAuxTable
|
|||||||
sig { params(aux_config: AuxTableConfig).void }
|
sig { params(aux_config: AuxTableConfig).void }
|
||||||
def setup_initialize_hook!(aux_config)
|
def setup_initialize_hook!(aux_config)
|
||||||
initialize_method = self.instance_method(:initialize)
|
initialize_method = self.instance_method(:initialize)
|
||||||
self.define_method(:initialize) do |args|
|
self.define_method(:initialize) do |args, **kwargs, &block|
|
||||||
T.bind(self, ActiveRecord::Base)
|
T.bind(self, ActiveRecord::Base)
|
||||||
aux_args, main_args =
|
aux_args, main_args =
|
||||||
args.partition { |k, _| aux_config.is_aux_column?(k) }.map(&:to_h)
|
args.partition { |k, _| aux_config.is_aux_column?(k) }.map(&:to_h)
|
||||||
initialize_method.bind(self).call(main_args)
|
initialize_method.bind(self).call(main_args, **kwargs, &block)
|
||||||
aux_config.assign_aux_attributes(self, aux_args)
|
aux_config.assign_aux_attributes(self, aux_args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ RSpec.describe HasAuxTable do
|
|||||||
t.timestamps
|
t.timestamps
|
||||||
|
|
||||||
t.create_aux :driver do |t|
|
t.create_aux :driver do |t|
|
||||||
|
t.integer :license_number
|
||||||
t.references :car,
|
t.references :car,
|
||||||
null: false,
|
null: false,
|
||||||
foreign_key: {
|
foreign_key: {
|
||||||
@@ -693,6 +694,17 @@ RSpec.describe HasAuxTable do
|
|||||||
expect(driver.car.engine_size).to eq(1.5)
|
expect(driver.car.engine_size).to eq(1.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "executes the hooks when creating through the association" do
|
||||||
|
driver =
|
||||||
|
@car
|
||||||
|
.drivers
|
||||||
|
.create!(name: "John Doe") do |driver|
|
||||||
|
driver.license_number = 123_456
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(driver.license_number).to eq(123_456)
|
||||||
|
end
|
||||||
|
|
||||||
it "can create a driver directly" do
|
it "can create a driver directly" do
|
||||||
driver = Driver.create!(car: @car, name: "John Doe")
|
driver = Driver.create!(car: @car, name: "John Doe")
|
||||||
expect(driver.car).to eq(@car)
|
expect(driver.car).to eq(@car)
|
||||||
|
|||||||
Reference in New Issue
Block a user