Sha256: 2770c62722fcc40b4d9078fc06e81bb55fecc28e594991ea6407d9f305eaecc1

Contents?: true

Size: 1.69 KB

Versions: 38

Compression:

Stored size: 1.69 KB

Contents

module Neo4j
  module ActiveNode
    module Query
      module QueryProxyEagerLoading
        def each(node = true, rel = nil, &block)
          return super if with_associations_spec.size.zero?

          query_from_association_spec.pluck(identity, with_associations_return_clause).map do |record, eager_data|
            eager_data.each_with_index do |eager_records, index|
              record.association_proxy(with_associations_spec[index]).cache_result(eager_records)
            end

            block.call(record)
          end
        end

        def with_associations_spec
          @with_associations_spec ||= []
        end

        def with_associations_return_clause
          '[' + with_associations_spec.map { |n| "collect(#{n})" }.join(',') + ']'
        end

        def with_associations(*spec)
          invalid_association_names = spec.reject do |association_name|
            model.associations[association_name]
          end

          if invalid_association_names.size > 0
            fail "Invalid associations: #{invalid_association_names.join(', ')}"
          end

          new_link.tap do |new_query_proxy|
            new_spec = new_query_proxy.with_associations_spec + spec
            new_query_proxy.with_associations_spec.replace(new_spec)
          end
        end

        private

        def query_from_association_spec
          with_associations_spec.inject(query_as(identity).return(identity)) do |query, association_name|
            association = model.associations[association_name]

            query.optional_match("#{identity}#{association.arrow_cypher}#{association_name}")
              .where(association.target_where_clause)
          end
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
neo4j-6.0.0 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.rc.4 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.rc.3 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.rc.2 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.rc.1 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.12 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-5.2.14 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.11 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.9 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-5.2.13 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-5.2.12 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.8 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.7 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-5.2.11 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.5 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.4 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-5.2.10 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.3 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.2 lib/neo4j/active_node/query/query_proxy_eager_loading.rb
neo4j-6.0.0.alpha.1 lib/neo4j/active_node/query/query_proxy_eager_loading.rb