Sha256: 97f0906856e4a797661fbbca089f2827645d64ac2332f52b743ce44a93ee63ae

Contents?: true

Size: 964 Bytes

Versions: 7

Compression:

Stored size: 964 Bytes

Contents

module ActiveRecord
  class Relation

    def qry_options(*args)
      DirectiveRecord::Relation.new(self).qry_options(*args)
    end

    def to_qry(*args)
      klass.to_qry qry_options(*args)
    end

    def qry(*args)
      klass.qry qry_options(*args)
    end

    alias :original_count :count

    def count(column_name = nil, options = {})
      if !loaded? && (column_name == :all) && (options == {})
        associations = klass.reflections.keys.collect(&:to_s)

        contains_possible_paths = qry_options.any? do |key, value|
          if value.is_a?(Array)
            value.any? do |val|
              val.to_s.scan(/(?:^|[^\.])([a-z_]+)\.[a-z_]+/).flatten.any? do |string|
                associations.include?(string)
              end
            end
          end
        end

        if contains_possible_paths
          return qry("COUNT(DISTINCT id)")[0][0]
        end
      end
      original_count column_name, options
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
directiverecord-0.1.22 lib/directive_record/gem_ext/active_record/relation.rb
directiverecord-0.1.21 lib/directive_record/gem_ext/active_record/relation.rb
directiverecord-0.1.20 lib/directive_record/gem_ext/active_record/relation.rb
directiverecord-0.1.19 lib/directive_record/gem_ext/active_record/relation.rb
directiverecord-0.1.18 lib/directive_record/gem_ext/active_record/relation.rb
directiverecord-0.1.17 lib/directive_record/gem_ext/active_record/relation.rb
directiverecord-0.1.16 lib/directive_record/gem_ext/active_record/relation.rb