Sha256: 8d43471b10f73bff2a22026d72456c845b4b793d3c0b72bb84a31f882afff437

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

module ActiveRecord
  module Precount
    module JoinDependencyExtension
      class CountTable < Associations::JoinDependency::Aliases::Table
        def column_aliases
          columns.map { |column| table[column.name].count.as Arel.sql column.alias }
        end
      end

      def aliases
        Associations::JoinDependency::Aliases.new join_root.each_with_index.map { |join_part,i|
          if join_part.is_a?(Associations::JoinDependency::JoinAssociation) && join_part.reflection.macro == :count_loader
            # select COUNT(primary_key)
            column_name = join_part.reflection.klass.primary_key
            column = Associations::JoinDependency::Aliases::Column.new column_name, "t#{i}_r0"
            CountTable.new(join_part, [column])
          else
            # original aliases' internal function
            columns = join_part.column_names.each_with_index.map { |column_name,j|
              Associations::JoinDependency::Aliases::Column.new column_name, "t#{i}_r#{j}"
            }
            Associations::JoinDependency::Aliases::Table.new(join_part, columns)
          end
        }
      end

      private

      # instantiate only count_loader and pass others to super
      def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
        normal_children = []

        parent.children.each do |node|
          if node.reflection.macro != :count_loader
            normal_children << node
            next
          end

          key = aliases.column_alias(node, node.primary_key)
          ar_parent.association(node.reflection.name).target = row[key].to_i
        end
        return if normal_children.blank?

        normal_parent = Associations::JoinDependency::JoinBase.new(parent.base_klass, normal_children)
        super
      end
    end
  end

  Associations::JoinDependency.prepend(Precount::JoinDependencyExtension)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
activerecord-precount-0.7.0 lib/active_record/precount/join_dependency_extension.rb
activerecord-precount-0.7.0.beta1 lib/active_record/precount/join_dependency_extension.rb
activerecord-precount-0.6.3 lib/active_record/precount/join_dependency_extension.rb
activerecord-precount-0.6.2 lib/active_record/precount/join_dependency_extension.rb
activerecord-precount-0.6.1 lib/active_record/precount/join_dependency_extension.rb