Sha256: 4cca8afa79b93b4b11e8bf9ca0a672a11c3414b813590af0149b5f7713ba91ac

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

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}"]

      manager = Arel::SelectManager.new(arel_engine)
      manager.project("DISTINCT #{projection}")
      manager.from(arel_table)
      
      query = Arel::Table.new('dummy').project('count(*)')
      query = query.from("(#{manager.to_sql}) AS subquery")
      type_cast_calculated_value(@klass.connection.select_value(query.to_sql), column_for(column_name), operation)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
composite_primary_keys-3.1.11 lib/composite_primary_keys/calculations.rb
composite_primary_keys-3.1.10 lib/composite_primary_keys/calculations.rb
composite_primary_keys-3.1.9 lib/composite_primary_keys/calculations.rb
composite_primary_keys-3.1.8 lib/composite_primary_keys/calculations.rb
composite_primary_keys-3.1.7 lib/composite_primary_keys/calculations.rb