cache bundle install at devcontainer build step
This commit is contained in:
@@ -35,6 +35,20 @@ module HasAuxTable
|
||||
|
||||
include RelationExtensions
|
||||
|
||||
sig do
|
||||
params(column_name: T.any(String, Symbol)).returns(
|
||||
T.nilable(AuxTableConfig)
|
||||
)
|
||||
end
|
||||
def aux_table_for(column_name)
|
||||
@aux_table_configs ||=
|
||||
T.let({}, T.nilable(T::Hash[Symbol, AuxTableConfig]))
|
||||
|
||||
@aux_table_configs.values.find do |config|
|
||||
config.aux.is_column?(column_name)
|
||||
end
|
||||
end
|
||||
|
||||
# Main DSL method for defining auxiliary tables
|
||||
sig { params(aux_name: T.any(String, Symbol)).returns(AuxTableConfig) }
|
||||
def aux_table(aux_name)
|
||||
@@ -243,7 +257,9 @@ module HasAuxTable
|
||||
T.bind(self, ActiveRecord::Base)
|
||||
if config.aux.column_names.include?(name.to_s)
|
||||
target = config.aux_model_for(self)
|
||||
T.unsafe(target).send(method_name, name, *args, **kwargs, &block)
|
||||
ret =
|
||||
T.unsafe(target).send(method_name, name, *args, **kwargs, &block)
|
||||
ret
|
||||
else
|
||||
T.unsafe(method).bind(self).call(name, *args, **kwargs, &block)
|
||||
end
|
||||
|
||||
@@ -194,22 +194,6 @@ module HasAuxTable
|
||||
end
|
||||
end
|
||||
|
||||
sig do
|
||||
params(
|
||||
relation: T.any(ActiveRecord::Relation, T.class_of(ActiveRecord::Base)),
|
||||
conditions: T::Hash[String, T.untyped]
|
||||
).returns(ActiveRecord::Relation)
|
||||
end
|
||||
def apply_split_conditions!(relation, conditions)
|
||||
main_conditions, aux_conditions =
|
||||
self.aux.partition_by_columns(conditions)
|
||||
relation = relation.where(main_conditions) if main_conditions.any?
|
||||
if aux_conditions.any?
|
||||
relation = relation.where(aux_association_name => aux_conditions)
|
||||
end
|
||||
relation
|
||||
end
|
||||
|
||||
sig do
|
||||
params(conditions: T::Hash[String, T.untyped]).returns(
|
||||
T::Hash[String, T.untyped]
|
||||
|
||||
@@ -1,6 +1,68 @@
|
||||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "active_record/associations/association_scope"
|
||||
|
||||
class ActiveRecord::Associations::AssociationScope
|
||||
def get_chain(reflection, association, tracker)
|
||||
name = reflection.name
|
||||
chain =
|
||||
T.let(
|
||||
[
|
||||
ActiveRecord::Reflection::RuntimeReflection.new(
|
||||
reflection,
|
||||
association
|
||||
)
|
||||
],
|
||||
T.untyped
|
||||
)
|
||||
|
||||
reflection
|
||||
.chain
|
||||
.drop(1)
|
||||
.each do |refl|
|
||||
refl_klass = T.cast(refl.klass, T.class_of(ActiveRecord::Base))
|
||||
if refl_klass.is_a?(HasAuxTable::ClassMethods) &&
|
||||
(aux_config = refl_klass.aux_table_for(refl.foreign_key))
|
||||
aliased_table = aux_config.aux.klass.arel_table
|
||||
chain << ReflectionProxy.new(refl, aliased_table)
|
||||
else
|
||||
aliased_table =
|
||||
tracker.aliased_table_for(refl.klass.arel_table) do
|
||||
refl.alias_candidate(name)
|
||||
end
|
||||
chain << ReflectionProxy.new(refl, aliased_table)
|
||||
end
|
||||
end
|
||||
chain
|
||||
end
|
||||
|
||||
def scope(association)
|
||||
klass = association.klass
|
||||
reflection = association.reflection
|
||||
scope = klass.unscoped
|
||||
owner = association.owner
|
||||
chain = get_chain(reflection, association, scope.alias_tracker)
|
||||
|
||||
scope.extending! reflection.extensions
|
||||
scope = add_constraints(scope, owner, chain)
|
||||
scope.limit!(1) unless reflection.collection?
|
||||
chain.each do |refl|
|
||||
klass = refl.klass
|
||||
next unless klass.is_a?(HasAuxTable::ClassMethods)
|
||||
next unless aux_config = klass.aux_table_for(refl.join_primary_key)
|
||||
aux_table = aux_config.aux.klass.table_name
|
||||
main_table = aux_config.main.klass.table_name
|
||||
fkey = "'#{aux_table}'.'#{aux_config.foreign_key}'"
|
||||
pkey = "'#{main_table}'.'#{aux_config.primary_key}'"
|
||||
scope.joins!("INNER JOIN '#{main_table}' ON #{fkey} = #{pkey}")
|
||||
end if association.is_a?(
|
||||
ActiveRecord::Associations::HasManyThroughAssociation
|
||||
)
|
||||
scope
|
||||
end
|
||||
end
|
||||
|
||||
module HasAuxTable
|
||||
module RelationExtensions
|
||||
extend T::Sig
|
||||
@@ -73,11 +135,7 @@ module HasAuxTable
|
||||
:bind_attribute,
|
||||
true
|
||||
) do |original, name, value, &block|
|
||||
if aux_config.aux.is_column?(name)
|
||||
aux_config.aux_bind_attribute(name, value, &block)
|
||||
else
|
||||
original.call(name, value, &block)
|
||||
end
|
||||
aux_config.aux_bind_attribute(name, value, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user