This commit is contained in:
Dylan Knutson
2025-07-18 21:39:51 +00:00
parent 04f6f78730
commit a526fb54f1
6 changed files with 82 additions and 3 deletions

View File

@@ -57,6 +57,7 @@ module HasAuxTable
setup_attributes_hook!(config)
setup_relation_extensions!(config)
setup_attribute_getter_setter_hooks!(config)
setup_enum_hook!(config)
config
end
@@ -198,9 +199,8 @@ module HasAuxTable
sig { params(load_schema_method: Method, config: AuxTableConfig).void }
def aux_config_load_schema!(load_schema_method, config)
# first, load the main and aux table schemas like normal
result = load_schema_method.call
config.load_aux_schema
result = load_schema_method.call
aux_table_name = config.aux_table_name
check_for_overlapping_columns!(
@@ -356,6 +356,49 @@ module HasAuxTable
"Auxiliary table columns must not overlap with main table columns."
end
end
sig { params(config: AuxTableConfig).void }
def setup_enum_hook!(config)
self.define_singleton_method(
:enum
) do |name = nil, values = nil, **options, &block|
name = name.to_s
config.aux.klass.enum(name, values, **options, &block)
self.defined_enums.merge!(config.aux.klass.defined_enums)
# define methods on the main class that delegate to the aux class
config
.aux
.klass
.send(:_enum_methods_module)
.instance_methods
.each do |method_name|
self
._enum_methods_module
.define_method(method_name) do |*args, **kwargs, &block|
raise "not implemented"
end
end
define_singleton_method(name.pluralize) do
config.aux.klass.send(name.pluralize)
end
values.keys.each do |value_name|
value_name = [options[:prefix], value_name, options[:suffix]].reject(
&:blank?
).join("_")
[
value_name,
"#{value_name}?",
"#{value_name}!",
"#{value_name}="
].each do |method_name|
config.define_aux_attribute_delegate(method_name.to_sym)
end
end
end
end
end
mixes_in_class_methods(ClassMethods)
end

View File

@@ -121,7 +121,7 @@ module HasAuxTable
const :foreign_key, KeyType
const :primary_key, KeyType
sig { void }
sig { returns(T.untyped) }
def load_aux_schema
aux_class.load_schema
end