[wip] loading optimizations
This commit is contained in:
@@ -13,6 +13,26 @@ module HasAuxTable
|
||||
column_names.include?(name.to_s)
|
||||
end
|
||||
|
||||
sig { params(name: String).returns(T::Boolean) }
|
||||
def is_primary_key?(name)
|
||||
primary_keys.include?(name.to_sym)
|
||||
end
|
||||
|
||||
sig { params(name: String).returns(T::Boolean) }
|
||||
def is_type_key?(name)
|
||||
type_key == name.to_s
|
||||
end
|
||||
|
||||
sig { returns(T.nilable(String)) }
|
||||
def type_key
|
||||
self.klass.inheritance_column
|
||||
end
|
||||
|
||||
sig { returns(Arel::Table) }
|
||||
def table
|
||||
self.klass.arel_table
|
||||
end
|
||||
|
||||
sig { returns(T::Array[Symbol]) }
|
||||
def primary_keys
|
||||
@primary_keys ||=
|
||||
|
||||
@@ -118,6 +118,58 @@ module HasAuxTable
|
||||
ActiveRecord::Associations::CollectionProxy
|
||||
)
|
||||
|
||||
pluck_method = relation_class.instance_method(:pluck)
|
||||
relation_class.send(:define_method, :pluck) do |column_names|
|
||||
T.bind(self, ActiveRecord::Relation)
|
||||
|
||||
filtered_attributes =
|
||||
self.where_clause.extract_attributes.select do |attr|
|
||||
column_name = attr.name
|
||||
if attr.relation == aux_config.main.table
|
||||
# if it's on the main table, ignore if it if's the primary key or type key
|
||||
next false if aux_config.main.is_primary_key?(column_name)
|
||||
next false if aux_config.main.is_type_key?(column_name)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
all_on_aux_table =
|
||||
filtered_attributes.all? do |attr|
|
||||
column_name = attr.name
|
||||
|
||||
# if it's a field on the aux table, then it can be plucked
|
||||
if attr.relation == aux_config.aux.table
|
||||
puts "optimize: #{column_name} is on #{attr.relation.name}"
|
||||
next true
|
||||
end
|
||||
|
||||
# if it's on the main table, ignore if it if's the primary key or type key
|
||||
if attr.relation == aux_config.main.table
|
||||
if aux_config.main.is_primary_key?(column_name)
|
||||
puts "optimize: #{column_name} is primary key on #{aux_config.main.table.name}"
|
||||
next true
|
||||
end
|
||||
if aux_config.main.is_type_key?(column_name)
|
||||
puts "optimize: #{column_name} is type key on #{aux_config.main.table.name}"
|
||||
next true
|
||||
end
|
||||
end
|
||||
|
||||
puts "skip optimization: #{column_name} is on #{attr.relation.name}"
|
||||
false
|
||||
end
|
||||
|
||||
if all_on_aux_table
|
||||
Kernel.puts "pluck is only for aux columns: #{column_names}"
|
||||
binding.pry
|
||||
aux_relation = aux_config.aux.klass.where(where_clause)
|
||||
aux_relation.pluck(*column_names)
|
||||
else
|
||||
Kernel.puts "pluck proxied to original: #{column_names}"
|
||||
pluck_method.bind(self).call(*column_names)
|
||||
end
|
||||
end
|
||||
|
||||
[
|
||||
[relation_class, :build_where_clause],
|
||||
[collection_proxy_class, :where]
|
||||
|
||||
@@ -31,8 +31,12 @@ module HasAuxTable
|
||||
)
|
||||
|
||||
target.send(define_method, method_name) do |*args, **kwargs, &block|
|
||||
method = is_instance_method ? target_method.bind(self) : target_method
|
||||
T.unsafe(hook_block).call(method, *args, **kwargs, &block)
|
||||
if is_instance_method
|
||||
method = target_method.bind(self)
|
||||
T.unsafe(hook_block).call(method, *args, **kwargs, &block)
|
||||
else
|
||||
T.unsafe(hook_block).call(target_method, *args, **kwargs, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user