Sha256: e2b4725284e33a889bf8cfa3057af616d0334e40759c7b1e193ec2df1a5e75aa

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

module ActiveRecord
  module Associations
    class AssociationScope

      def next_chain_scope(scope, table, reflection, tracker, assoc_klass, foreign_table, next_reflection)
        join_keys = reflection.join_keys(assoc_klass)
        key = join_keys.key
        foreign_key = join_keys.foreign_key

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

        if reflection.type
          value    = next_reflection.klass.base_class.name
          bind_val = bind scope, table.table_name, reflection.type, value, tracker
          scope    = scope.where(table[reflection.type].eq(bind_val))
        end

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

      def last_chain_scope(scope, table, reflection, owner, tracker, assoc_klass)
        join_keys = reflection.join_keys(assoc_klass)
        key = join_keys.key
        foreign_key = join_keys.foreign_key

        if key.kind_of?(Array) || foreign_key.kind_of?(Array)
          predicate = cpk_join_predicate(table, key, owner, foreign_key)
          scope = scope.where(predicate)
        else
          bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
          scope    = scope.where(table[key].eq(bind_val))
        end

        if reflection.type
          value    = owner.class.base_class.name
          bind_val = bind scope, table.table_name, reflection.type, value, tracker

          scope.where(table[reflection.type].eq(bind_val))
        else
          scope
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
composite_primary_keys-8.1.5 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-8.1.4 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-8.1.3 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-8.1.2 lib/composite_primary_keys/associations/association_scope.rb