Sha256: 4b6cdcdc8718386ede197c82d4a75005711560a1c96bbb3bfc5dd52d71e4f2c5

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Searchlogic
  module ActiveRecord
    # Active Record is pretty inconsistent with how their SQL is constructed. This
    # method attempts to close the gap between the various inconsistencies.
    module Consistency
      def self.included(klass)
        klass.class_eval do
          alias_method_chain :merge_joins, :searchlogic
        end
      end
      
      # In AR multiple joins are sometimes in a single join query, and other times they
      # are not. The merge_joins method in AR should account for this, but it doesn't.
      # This fixes that problem. This way there is one join per string, which allows
      # the merge_joins method to delete duplicates.
      def merge_joins_with_searchlogic(*args)
        joins = merge_joins_without_searchlogic(*args)
        joins = joins.collect { |j| j.is_a?(String) ? j.split("  ") : j }.flatten.uniq
        joins = joins.collect do |j|
          if j.is_a?(String)
            j.gsub(/(.*) ON (.*) = (.*)/) do |m|
              sorted = [$2,$3].sort
              "#{$1} ON #{sorted[0]} = #{sorted[1]}"
            end
          else
            j
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
binarylogic-searchlogic-2.3.4 lib/searchlogic/active_record/consistency.rb
searchlogic-2.3.4 lib/searchlogic/active_record/consistency.rb