refactor methods
This commit is contained in:
@@ -48,6 +48,9 @@ module HasAuxTable
|
|||||||
@aux_table_configs[aux_name] = aux_config = generate_aux_config(aux_name)
|
@aux_table_configs[aux_name] = aux_config = generate_aux_config(aux_name)
|
||||||
setup_attribute_types_hook!(aux_config)
|
setup_attribute_types_hook!(aux_config)
|
||||||
setup_schema_loading_hook!(aux_config)
|
setup_schema_loading_hook!(aux_config)
|
||||||
|
setup_initialize_hook!(aux_config)
|
||||||
|
setup_save_hook!(aux_config)
|
||||||
|
setup_reload_hook!(aux_config)
|
||||||
setup_relation_extensions!(aux_config)
|
setup_relation_extensions!(aux_config)
|
||||||
setup_attribute_getter_setter_hooks!(aux_config)
|
setup_attribute_getter_setter_hooks!(aux_config)
|
||||||
|
|
||||||
@@ -231,44 +234,6 @@ module HasAuxTable
|
|||||||
aux_config.define_aux_attribute_delegate(:"#{column_name}=")
|
aux_config.define_aux_attribute_delegate(:"#{column_name}=")
|
||||||
end
|
end
|
||||||
|
|
||||||
%i[save save!].each do |method_name|
|
|
||||||
save_method = self.instance_method(method_name)
|
|
||||||
self.define_method(method_name) do |*args, **kwargs|
|
|
||||||
T.bind(self, ActiveRecord::Base)
|
|
||||||
result = save_method.bind(self).call(*args, **kwargs)
|
|
||||||
result &&=
|
|
||||||
self
|
|
||||||
.association(aux_config.aux_association_name)
|
|
||||||
.target
|
|
||||||
.send(method_name, *args, **kwargs)
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
initialize_method = self.instance_method(:initialize)
|
|
||||||
self.define_method(:initialize) do |args|
|
|
||||||
T.bind(self, ActiveRecord::Base)
|
|
||||||
aux_args, main_args =
|
|
||||||
args.partition { |k, _| aux_config.is_aux_column?(k) }.map(&:to_h)
|
|
||||||
initialize_method.bind(self).call(main_args)
|
|
||||||
aux_config.assign_aux_attributes(self, aux_args)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.define_method(:reload) do |*args|
|
|
||||||
T.bind(self, ActiveRecord::Base)
|
|
||||||
aux_model = aux_config.ensure_aux_target(self)
|
|
||||||
fresh_model = self.class.find(id)
|
|
||||||
@attributes = fresh_model.instance_variable_get(:@attributes)
|
|
||||||
aux_model.instance_variable_set(
|
|
||||||
:@attributes,
|
|
||||||
fresh_model
|
|
||||||
.association(aux_config.aux_association_name)
|
|
||||||
.target
|
|
||||||
.instance_variable_get(:@attributes)
|
|
||||||
)
|
|
||||||
self
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -294,6 +259,53 @@ module HasAuxTable
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(aux_config: AuxTableConfig).void }
|
||||||
|
def setup_initialize_hook!(aux_config)
|
||||||
|
initialize_method = self.instance_method(:initialize)
|
||||||
|
self.define_method(:initialize) do |args|
|
||||||
|
T.bind(self, ActiveRecord::Base)
|
||||||
|
aux_args, main_args =
|
||||||
|
args.partition { |k, _| aux_config.is_aux_column?(k) }.map(&:to_h)
|
||||||
|
initialize_method.bind(self).call(main_args)
|
||||||
|
aux_config.assign_aux_attributes(self, aux_args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
sig { params(aux_config: AuxTableConfig).void }
|
||||||
|
def setup_save_hook!(aux_config)
|
||||||
|
%i[save save!].each do |method_name|
|
||||||
|
save_method = self.instance_method(method_name)
|
||||||
|
self.define_method(method_name) do |*args, **kwargs|
|
||||||
|
T.bind(self, ActiveRecord::Base)
|
||||||
|
result = save_method.bind(self).call(*args, **kwargs)
|
||||||
|
result &&=
|
||||||
|
self
|
||||||
|
.association(aux_config.aux_association_name)
|
||||||
|
.target
|
||||||
|
.send(method_name, *args, **kwargs)
|
||||||
|
result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
sig { params(aux_config: AuxTableConfig).void }
|
||||||
|
def setup_reload_hook!(aux_config)
|
||||||
|
self.define_method(:reload) do |*args|
|
||||||
|
T.bind(self, ActiveRecord::Base)
|
||||||
|
aux_model = aux_config.ensure_aux_target(self)
|
||||||
|
fresh_model = self.class.find(id)
|
||||||
|
@attributes = fresh_model.instance_variable_get(:@attributes)
|
||||||
|
aux_model.instance_variable_set(
|
||||||
|
:@attributes,
|
||||||
|
fresh_model
|
||||||
|
.association(aux_config.aux_association_name)
|
||||||
|
.target
|
||||||
|
.instance_variable_get(:@attributes)
|
||||||
|
)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
sig do
|
sig do
|
||||||
params(
|
params(
|
||||||
aux_table_name: Symbol,
|
aux_table_name: Symbol,
|
||||||
|
|||||||
Reference in New Issue
Block a user