lib/left_joins.rb in left_joins-1.0.0 vs lib/left_joins.rb in left_joins-1.0.1

- old
+ new

@@ -2,11 +2,11 @@ require 'active_record' require 'active_record/relation' module ActiveRecord::QueryMethods IS_RAILS3_FLAG = Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0') - if not method_defined?(:check_if_method_has_arguments!) + if IS_RAILS3_FLAG def check_if_method_has_arguments!(method_name, args) if args.blank? raise ArgumentError, "The method .#{method_name}() must contain arguments." end end @@ -64,9 +64,31 @@ else alias_method :make_constraints_without_hooking_join_type, :make_constraints def make_constraints(*args, join_type) join_type = Thread.current.thread_variable_get :left_joins_join_type || join_type return make_constraints_without_hooking_join_type(*args, join_type) + end + end + end + + module ActiveRecord::Calculations + def perform_calculation(operation, column_name, options = {}) + operation = operation.to_s.downcase + + # If #count is used with #distinct (i.e. `relation.distinct.count`) it is + # considered distinct. + distinct = IS_RAILS3_FLAG ? options[:distinct] || self.uniq_value : self.distinct_value + + if operation == "count" + column_name ||= select_for_count + column_name = primary_key if column_name == :all && distinct + distinct = nil if column_name =~ /\s*DISTINCT[\s(]+/i + end + + if group_values.any? + execute_grouped_calculation(operation, column_name, distinct) + else + execute_simple_calculation(operation, column_name, distinct) end end end end end