Sha256: d3fc6d8fa57d6b1664a152b2f8889baf467d1a64e354a45a4c6010637d7e9d4c

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

module ActiveRecord
  module Associations
    class AssociationScope
      silence_warnings do
        def next_chain_scope(scope, table, reflection, association_klass, foreign_table, next_reflection)
          join_keys = reflection.join_keys(association_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 = transform_value(next_reflection.klass.base_class.name)
            scope = scope.where(table.name => { reflection.type => value })
          end

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

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

          # CPK
          if key.kind_of?(Array) || foreign_key.kind_of?(Array)
            predicate = cpk_join_predicate(table, key, owner, foreign_key)
            scope = scope.where(predicate)
          else
            value = transform_value(owner[foreign_key])
            scope = scope.where(table.name => { key => value })
          end

          if reflection.type
            polymorphic_type = transform_value(owner.class.base_class.name)
            scope = scope.where(table.name => { reflection.type => polymorphic_type })
          end

          scope
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
composite_primary_keys-9.0.4 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-9.0.2 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-9.0.1 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-9.0.0 lib/composite_primary_keys/associations/association_scope.rb
composite_primary_keys-9.0.0.beta1 lib/composite_primary_keys/associations/association_scope.rb