Sha256: 1c99d71174ad0caf5f6538c9d7c7a6fb719a8738b69a54e0fed8efd011256e66

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module ActiveRecord
  module Associations
    class AssociationScope
      def self.get_bind_values(owner, chain)
        binds = []
        last_reflection = chain.last

        # CPK
        # binds << last_reflection.join_id_for(owner)
        values = last_reflection.join_id_for(owner)
        binds += Array(values)

        if last_reflection.type
          binds << owner.class.polymorphic_name
        end

        chain.each_cons(2).each do |reflection, next_reflection|
          if reflection.type
            binds << next_reflection.klass.polymorphic_name
          end
        end
        binds
      end

      def last_chain_scope(scope, reflection, owner)
        key = reflection.join_primary_key
        foreign_key = reflection.join_foreign_key

        table = reflection.aliased_table

        # CPK
        # value = transform_value(owner[foreign_key])
        # scope = apply_scope(scope, table, key, value)
        Array(key).zip(Array(foreign_key)).each do |a_join_key, a_foreign_key|
          value = transform_value(owner[a_foreign_key])
          scope = apply_scope(scope, table, a_join_key, value)
        end

        if reflection.type
          polymorphic_type = transform_value(owner.class.polymorphic_name)
          scope = apply_scope(scope, table, reflection.type, polymorphic_type)
        end

        scope
      end

      def next_chain_scope(scope, reflection, next_reflection)
        key = reflection.join_primary_key
        foreign_key = reflection.join_foreign_key

        table = reflection.aliased_table
        foreign_table = next_reflection.aliased_table

        # CPK
        # constraint = table[key].eq(foreign_table[foreign_key])
        constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)

        if reflection.type
          value = transform_value(next_reflection.klass.polymorphic_name)
          scope = apply_scope(scope, table, reflection.type, value)
        end

        scope.joins!(join(foreign_table, constraint))
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
composite_primary_keys-14.0.9 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-14.0.8 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-13.0.8 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-14.0.7 lib/composite_primary_keys/associations/association_scope.rb