Sha256: 7b7d1c874120786bd542b22c5f878e241f34117fc07fa0e93dd63333084fd55e

Contents?: true

Size: 1.97 KB

Versions: 20

Compression:

Stored size: 1.97 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, :singularity
          alias_method_chain :merge_joins, :consistent_conditions
          alias_method_chain :merge_joins, :merged_duplicates
        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_singularity(*args)
        joins = merge_joins_without_singularity(*args)
        joins.collect { |j| j.is_a?(String) ? j.split("  ") : j }.flatten.uniq
      end

      # This method ensures that the order of the conditions in the joins are the same.
      # The strings of the joins MUST be exactly the same for AR to remove the duplicates.
      # AR is not consistent in this approach, resulting in duplicate joins errors when
      # combining scopes.
      def merge_joins_with_consistent_conditions(*args)
        joins = merge_joins_without_consistent_conditions(*args)
        joins.collect do |j|
          if j.is_a?(String) && (j =~ / (AND|OR) /i).nil?
            j.gsub(/(.*) ON (.*) = (.*)/) do |m|
              join, cond1, cond2 = $1, $2, $3
              sorted = [cond1.gsub(/\(|\)/, ""), cond2.gsub(/\(|\)/, "")].sort
              "#{join} ON #{sorted[0]} = #{sorted[1]}"
            end
          else
            j
          end
        end.uniq
      end


      def merge_joins_with_merged_duplicates(*args)
        args << "" if !Thread.current["searchlogic_delegation"]
        joins = merge_joins_without_merged_duplicates(*args)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
searchlogic-2.5.19 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.18 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.17 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.16 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.15 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.14 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.13 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.12 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.11 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.10 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.9 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.8 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.7 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.6 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.5 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.4 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.3 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.2 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.1 lib/searchlogic/active_record/consistency.rb
searchlogic-2.5.0 lib/searchlogic/active_record/consistency.rb