Sha256: e94ae1daf458040e69ac125a99009e388be58defd1086abdfa98be7e7395da7a

Contents?: true

Size: 964 Bytes

Versions: 2

Compression:

Stored size: 964 Bytes

Contents

module ActiveRecord
  module Calculations
    alias :execute_simple_calculation_ar :execute_simple_calculation
    def execute_simple_calculation(operation, column_name, distinct)
      # CPK
      if column_name.kind_of?(Array)
        execute_simple_calculation_cpk(operation, column_name, distinct)
      else
        execute_simple_calculation_ar(operation, column_name, distinct)
      end
    end

    def execute_simple_calculation_cpk(operation, column_name, distinct)
      projection = self.primary_keys.map do |key|
        attribute = arel_table[key]
        self.arel.visitor.accept(attribute)
      end.join(', ')

      relation = self.clone
      relation.select_values = ["DISTINCT #{projection}"]

      table = Arel::Table.new('dummy').project('count(*)')
      relation = table.from(relation.arel, "foobar")
      type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
composite_primary_keys-3.1.6 lib/composite_primary_keys/calculations.rb
composite_primary_keys-3.1.5 lib/composite_primary_keys/calculations.rb